Author: Ondra Chaloupka Level: Intermediate Technologies: JTA, JDBC
This examle demostrates usage of jdbc transactional driver in a standalone application.
JDK8 expected.
You can spot here three different approaches to work with Narayana jdbc transactional driver (the implementation can be found at https://github.com/jbosstm/narayana/tree/main/ArjunaJTA/jdbc).
You can see here how to register an XADataSource to the driver, how to get managed connection and then how to work with such connection to be correctly handled in Narayana. Additionally you can check settings needed for recovery works.
See the approaches at classes
- src/main/java/io/narayana/DriverProvidedXADataSource.java
- src/main/java/io/narayana/DriverDirectRecoverable.java
- src/main/java/io/narayana/DriverIndirectRecoverable.java
The settings of recovery could be seen in recovery util class (Recovery settings is moved out of the connection showcases as it's independent on connection settings and is expected that will be done once globally for the whole application.)
You can run tests and check the behaviour in case of commit, rollback and recovery scenarios just by
mvn test
To get information from the TRACE log for example to check how recovery works, you can run with added java util logging properties file.
mvn test -Dtest=TransactionalDriverTest#transactionDriverDirectRecoverableRecovery\
-Djava.util.logging.config.file=src/main/resources/logging.properties 2>&1 | tee my.log
The tests are prepared are run with H2 database. If you are interested in setup for PostgreSQL database you need to manually(!) change the code. Or in case you can find inspiration of settings needed for PostgreSQL here.
The steps to run with PostgreSQL are:
- go to class
DBUtils.java
and change variable 'isH2' to false - go to class
DriverDirectRecoverable.java
and change- variable
jdbcUrl1
to use value of properties file stringds1.pg.properties
- variable
jdbcUrl2
to use value of properties file stringds2.pg.properties
- variable
- go to descriptor file
recovery-basicxa-test1.xml
and change the user and password appropriately - go to descriptor file
recovery-jdbcxa-test1.xml
and change the user and password appropriately