Transactional cash settlement. Which one do I use?


I originally set up spring with xapool, but it turns out that's a dead project and seems to have lots of problems.

I switched to c3p0, but now I learn that the @Transactional annotations don't actually create transactions when used with c3p0. If I do the following it will insert the row into Foo even through an exception was thrown inside the method:

public class FooTst
    @PersistenceContext(unitName="accessControlDb") private EntityManager em;

    public void insertFoo() {
        em.createNativeQuery("INSERT INTO Foo (id) VALUES (:id)")
            .setParameter("id", System.currentTimeMillis() % Integer.MAX_VALUE )

        throw new RuntimeException("Foo");


This is strange because if I comment out the @Transactional annotation it will actually fail and complain about having a transaction set to rollback only:

java.lang.IllegalStateException: Cannot get Transaction for setRollbackOnly
    at org.objectweb.jotm.Current.setRollbackOnly(
    at org.hibernate.ejb.AbstractEntityManagerImpl.markAsRollback(
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(
    at org.hibernate.ejb.QueryImpl.executeUpdate(
    at com.ipass.rbac.svc.FooTst.insertFoo(
    at com.ipass.rbac.svc.SingleTst.testHasPriv(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at org.springframework.test.context.junit4.SpringTestMethod.invoke(
    at org.springframework.test.context.junit4.SpringMethodRoadie.runTestMethod(
    at org.springframework.test.context.junit4.SpringMethodRoadie$
    at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(
    at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(
    at org.junit.internal.runners.JUnit4ClassRunner$
    at org.junit.internal.runners.ClassRoadie.runUnprotected(
    at org.junit.internal.runners.ClassRoadie.runProtected(
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(

So, clearly it notices the @Transactional annotation. But, it doesn't actually set autocommit to off at the start of the method.

Here is how I have transactional stuff setup up in the applicationContext.xml. Is this correct? If not, what is this supposed to be?

<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager" ref="jotm"/>
    <property name="userTransaction" ref="jotm"/>
    <property name="allowCustomIsolationLevels" value="true"/>
<tx:annotation-driven  transaction-manager="txManager" proxy-target-class="false"/>

After a bunch of searching I found a connection pool called Bitronix, but their spring setup page describes stuff about JMS which doesn't even make any sense. What does JMS have to do with setting up a connection pool?

So I'm stuck. What am I actually supposed to do? I don't understand why the connection pool needs to support transactions. All connections support turning autocommit on and off, so I have no idea what the problem is here.

It took a lot of searching and experimentation, but I finally got things working. Here are my results:

  • enhydra xapool is a terrible connection pool. I won't enumerate the problems it caused because it doesn't matter. The latest version of that pool hasn't been updated since Dec 2006. It's a dead project.
  • I put c3p0 into my application context and got it working fairly easily. But, for some reason it just doesn't seem to support rollback even inside a single method. If I mark a method as @Transactional then do an insert into a table and then throw a RuntimeException (one that's definitely not declared in the throws list of the method because there is no throws list on the method) it will still keep the insert into that table. It will not roll back.
  • I was going to try Apache DBCP, but my searching turned up lots of complaints about it, so I didn't bother.
  • I tried Bitronix and had plenty of trouble getting it to work properly under Tomcat, but once I figured out the magic configuration it works beautifully. What follows is all the things you need to do to set it up properly.
  • I dabbled briefly with the Atomkos connection pool. It looks like it should be good, but I got Bitronix working first, so I didn't try using it much.

The configuration below works in standalone unit tests and under Tomcat. That was the major problem I had. Most of the examples I found about how to set up Spring with Bitronix assume that I'm using JBoss or some other full container.

The first bit of configuration is the part that sets up the Bitronix transaction manager.

<!-- Bitronix transaction manager -->
<bean id="btmConfig" factory-method="getConfiguration" class="">
    <property name="disableJmx" value="true" />
<bean id="btmManager" factory-method="getTransactionManager" class="" depends-on="btmConfig" destroy-method="shutdown"/>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager" ref="btmManager" />
    <property name="userTransaction" ref="btmManager" />
    <property name="allowCustomIsolationLevels" value="true" />
<tx:annotation-driven transaction-manager="transactionManager" />

The major difference between that code and the examples I found is the "disableJmx" property. It throws exceptions at runtime if you don't use JMX but leave it enabled.

The next bit of configuration is the connection pool data source. Note that the connection pool classname is not the normal oracle class "oracle.jdbc.driver.OracleDriver". It's an XA data source. I don't know what the equivalent class would be in other databases.

<bean id="dataSource" class="" init-method="init" destroy-method="close">
    <property name="uniqueName" value="dataSource-BTM" />
    <property name="minPoolSize" value="1" />
    <property name="maxPoolSize" value="4" />
    <property name="testQuery" value="SELECT 1 FROM dual" />
    <property name="driverProperties"><props>
        <prop key="URL">${jdbc.url}</prop>
        <prop key="user">${jdbc.username}</prop>
        <prop key="password">${jdbc.password}</prop>
    <property name="className" value="oracle.jdbc.xa.client.OracleXADataSource" />
    <property name="allowLocalTransactions" value="true" />

Note also that the uniqueName needs to be different than any other data sources you have configured.

The testQuery of course needs to be specific to the database that you are using. The driver properties are specific to the database class that I'm using. OracleXADataSource for some silly reason has different setter names for OracleDriver for the same value.

The allowLocalTransactions had to be set to true for me. I found recommendations NOT to set it to true online. But, that seems to be impossible. It just won't work if it's set to false. I am not sufficiently knowledgeable about these things to know why that is.

Lastly we need to configure the entity manager factory.

<util:map id="jpa_property_map">
    <entry key="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.BTMTransactionManagerLookup"/>
    <entry key="hibernate.current_session_context_class" value="jta"/>

<bean id="dataSource-emf" name="accessControlDb" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="persistenceXmlLocation" value="classpath*:META-INF/foo-persistence.xml" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true"/>
            <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"/>
    <property name="jpaPropertyMap" ref="jpa_property_map"/>
    <property name="jpaDialect"><bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/></property>

Note the dataSource property refers to the id of the dataSource I declared. The persistenceXmlLocation refers to a persistence xml file that exists in the classpath somewhere. The classpath*: indicates it may be in any jar. Without the * it won't find it if it's in a jar for some reason.

I found util:map to be a handy way to put the jpaPropertyMap values in one place so that I don't need to repeat them when I use multiple entity manager factories in one application context.

Note that the util:map above won't work unless you include the proper settings in the outer beans element. Here is the header of the xml file that I use:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
       xmlns:tx="" xmlns:util=""

Lastly, in order for Bitronix (or apparently any cpool which supports two phase commit) to work with Oracle you need to run the following grants as user SYS. (See and and

grant select on pending_trans$ to <someUsername>;
grant select on dba_2pc_pending to <someUsername>;
grant select on dba_pending_transactions to <someUsername>;
grant execute on dbms_system to <someUsername>;

Those grants need to be run for any user that a connection pool is set up for regardless of whether you actually do any modifying of anything. It apparently looks for those tables when a connection is established.

A few other misc issues:

  • You can't query tables which are remote synonyms in Oracle while inside a Spring @Transactional block while using Bitronix (you'll get an ORA-24777). Use materialized views or a separate EntityManager which directly points at the other DB instead.
  • For some reason the btmConfig in the applicationContext.xml has problems setting config values. Instead create a file. The config values you can use are found at . Some other config info for that file is at but I haven't used it.
  • Bitronix uses some local files to store transactional stuff. I don't know why, but I do know that if you have multiple webapps with local connection pools you will have problems because they will both try to access the same files. To fix this specify different values for and in the for each app.
  • Bitronix javadocs are at .

That's pretty much it. It's very fiddly to get it to work, but it's working now and I'm happy. I hope that all this helps others who are going through the same troubles I did to get this all to work.