Sample Applications
The following sample applications showing how to use XA with a variety of technologies.
XA Sample Application
This sample application uses the JBoss application server. It shows an example using User managed transactions. Although most people use JTA from within a Spring or EJB container rather than managing it themselves, this sample application is useful as a demonstration. The following snippet from our SimpleTX servlet shows a complete transaction.
Ehcache cache = cacheManager.getEhcache("xaCache");
UserTransaction ut = getUserTransaction();
printLine(servletResponse, "Hello...");
try {
ut.begin();
int index = serviceWithinTx(servletResponse, cache);
printLine(servletResponse, "Bye #" + index);
ut.commit();
} catch(Exception e) {
printLine(servletResponse,
"Caught a " + e.getClass() + "! Rolling Tx back");
if(!printStackTrace) {
PrintWriter s = servletResponse.getWriter();
e.printStackTrace(s);
s.flush();
}
rollbackTransaction(ut);
}
You can download the source code for the sample from
Terracotta Forge. The readme file explains how to set up the sample application.
XA Banking Application
This application represents a real-world scenario. A Web application reads <account transfer> messages from a queue and tries to execute the transfers.
With transaction mode enabled, failures are rolled back so that the cached account balance is always the same as the true balance summed from the database. This sample is a Spring-based web application running in a Jetty container. It has (embedded) the following components:
A message broker (ActiveMQ)
2 databases (embedded Derby XA instances)
2 caches (transactional Ehcache)
All XA Resources are managed by Atomikos Transaction Manager. Transaction demarcation is done using Spring AOP's @Transactional annotation. You can run it with: mvn clean jetty:run. Then point your browser at: http://localhost:9080. To see what happens without XA transactions: mvn clean jetty:run -Dxa=no
You can download the source code for the sample from
Terracotta Forge. The readme file explains how to set up the sample application.