Reputation: 30133
I want to use an in-memory database to test my application, but it needs to support XA distributed transactions. My first idea for an in-memory database was HSQLDB, but it appears not to support XA. Are there any?
Upvotes: 2
Views: 1612
Reputation: 10812
Both H2 database and HSQLDB support it, couldn't find any other java embedded databases even in 2019. But it's not correctly implemented in either of them: the transaction is rolled back as soon as the client disconnects. According to the X/Open spec, when a database prepares a transaction, its data should be persistently stored and must be available to be committed later.
In H2 the XA code is unmaintained.
If you want an XA-supporting database for your tests, you can use Postgres using the TestContainers project:
@ClassRule
public static PostgreSQLContainer container = new PostgreSQLContainer<>("postgres:12.1")
.withCommand("postgres -c max_prepared_transactions=10");
Then create a datasource like this:
PGXADataSource dataSource = new PGXADataSource();
dataSource.setURL(container.getJdbcUrl());
dataSource.setUser(container.getUsername());
dataSource.setPassword(container.getPassword());
dataSource.setDatabaseName(container.getDatabaseName());
Upvotes: 1