I recently had the worst time trying to get JTA transactions to play nice in Hibernate configured via Spring. There is a lot of information out there on bits-n-pieces of the individual technologies but nothing seemed to show the whole picture, so I would like to provide the community with a working example. The code is not “perfect” but it demonstrates how to get this stuff working in harmony. A junit test, mssql scripts, and all the configs are included. I also set this up to use a composite key, since the project I worked on used a great deal of them and info was not easy to come by.
One of the libraries I used called BTM was instrumental in getting this to work (thanks Ludovic!), you will find this a lot easier to use than JOTM. There is a huge gotcha when using XA transactions with MSSQL, you have to install a dll and run some scripts to get it enabled and that info may be found here. I’m not sure if you have to do anything special in MySQL but I doubt it
The example comes as a zipped Eclipse project available here.
I have zipped up the libraries in a separate file because they are nearly 9mb, if you want download the zip it is here; The full list is as follows:
antlr-2.7.6.jar aopalliance.jar asm-2.2.3.jar asm-commons-2.2.3.jar btm-1.3.jar cglib-nodep-2.1_3.jar commons-collections-3.2.1.jar commons-lang-2.4.jar dom4j-1.6.1.jar ejb3-persistence.jar geronimo-jta_1.0.1B_spec-1.0.1.jar hibernate-annotations.jar hibernate-commons-annotations.jar hibernate-entitymanager.jar hibernate-validator.jar hibernate3.jar javassist-3.4.GA.jar jcl-over-slf4j-1.5.2.jar jtds-1.2.2.jar jts1_0.jar jul-to-slf4j-1.5.2.jar log4j-over-slf4j-1.5.2.jar logback-classic-0.9.9.jar logback-core-0.9.9.jar slf4j-api-1.5.2.jar spring-beans-2.5.5.jar spring-context-2.5.5.jar spring-core-2.5.5.jar spring-jdbc-2.5.5.jar spring-orm-2.5.5.jar spring-test.jar spring-tx-2.5.5.jar sqljdbc.jar
The example allows you to use JTDS or MSJDBC, just un-comment the driver you want to use in the DataLoaderTest.xml file.
<property name="className" value="com.microsoft.sqlserver.jdbc.SQLServerXADataSource"/> <!-- <property name="className" value="net.sourceforge.jtds.jdbcx.JtdsDataSource"/> -->
For more detailed information on transactions in Hibernate go here