The Last Resource Commit (LRC) optimization (also known as Last Resource Gambit or Last Agent optimization) allows a single non-XA resource, such as a JMS server or a database, to participate in an XA transaction by carefully ordering the resource commit sequence.
Critical Safety Warnings
- Inconsistency Risk: There is a small chance of inconsistent results across participating resources if BTM crashes while a transaction is in-flight. This is a limitation of the LRC concept itself, not BTM.
- Single Resource Limit: You can have at most one non-XA resource participating via LRC in a single transaction. Attempting to use a second emulating resource will cause BTM to throw an exception.
- Safety Note: Using LRC is 100% safe if you are only running transactions against a single JMS server. It is not 100% safe in multi-resource scenarios.
Implementation
To enable LRC, you must use bitronix.tm.resource.jms.lrc.LrcXAConnectionFactory as the XAConnectionFactory implementation within your connection factory configuration.
// Example of configuring an ActiveMQ datasource with LRC
PoolingConnectionFactory myConnectionFactory = new PoolingConnectionFactory();
myConnectionFactory.setClassName("bitronix.tm.resource.jms.lrc.LrcXAConnectionFactory");
myConnectionFactory.setUniqueName("amq-lrc");
myConnectionFactory.setMaxPoolSize(5);
myConnectionFactory.getDriverProperties().setProperty("connectionFactoryClassName", "org.apache.activemq.ActiveMQConnectionFactory");
myConnectionFactory.getDriverProperties().setProperty("properties.brokerUrl", "tcp://localhost:61616");