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
Tags: annotations, btm, hibernate, jpa, jta, jts, spring
Would Atomikos make any difference:
http://www.atomikos.com/Main/ProductsOverview.
I don’t have any experience with Atomikos. If BTM had not work out for me, Atomikos was next on my list of solutions.
Hi Paul,
I have tried to configure two dataSources for different databases but there is an error because the hibernate factory can’t find the second datasource: “Could not find datasource: jdbc/dsBackup”.
Have you ever tried to configure multiple database transactions?
Here is my spring configuration:
com.demo.entities.Book
com.demo.entities.Book
PROPAGATION_REQUIRED,-java.lang.Exception
It seems that the post did not accept the xml format. Please take a look to this url: http://tomware.homeip.net:8888/svn/public/trunk/JOTMSpringHibernateDemo/src/multipleDataBaseTestAppContext.xml
If you want the project source code, checkout the eclipse java project from http://tomware.homeip.net:8888/svn/public/trunk
Best regards.
I miss this information. SVN server login:
user: public
password: public
Hi again Paul.
I found a solution. It seems that hibernate was trying to get the resource from JNDI before it was available. So, in spring configuration I indicate that the hibernate factory bean depends on datasource bean and all works
Best regards