2011-05-29

JTA transactions with Hibernate, JBoss 6 and Spring 3

In this post I will show you how to use JTA transactions with JBoss 6 server, Spring 3 and Hibernate 3.

In JEE, JTA allows transactions to be distributed across multiple resources. You don't need a JEE server though to run JTA transactions, Tomcat will do (possibly with a bit more hassle).
I decided to use JBoss 6 because it is a popular JEE server - but I would assume you can successfully run the code on any server as long as the data source is set correctly (plus with minor tweaks of server dependent stuff).

(I also wrote a post on non-jta transactions with Spring 3, JBoss 6 and Hibernate 3.)

applicationContext.xml


This is our Spring config file.

<bean id="entityManagerFactory"
       class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
       <property name="persistenceUnitName" value="Dogs" />
   </bean>

   <bean id="dogsDao" class="me.m1key.springtx.dao.DogsDaoImpl" />

   <bean id="dogsBean" class="me.m1key.springtx.beans.DogsBean"
       scope="singleton">
       <property name="dogsDao" ref="dogsDao" />
   </bean>

   <tx:annotation-driven transaction-manager="myTransactionManager" />
   <tx:jta-transaction-manager />

   <bean id="myTransactionManager"
       class="org.springframework.transaction.jta.JtaTransactionManager" />

   <bean
       class="
       org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor " />
Look closely, notice the jta-transaction-manager annotation and JtaTransactionManager.

persistence.xml


This is our persistence.xml. It's a bit different from the non-jta version.

…
    <persistence-unit name="Dogs" transaction-type="JTA">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/DogsDS</jta-data-source>
       <class>me.m1key.springtx.entities.Dog</class>

       <properties>
           <property name="hibernate.transaction.manager_lookup_class"
               value="org.hibernate.transaction.JBossTransactionManagerLookup" />
           <property name="hibernate.show_sql" value="true" />
           <property name="hibernate.format_sql" value="true" />
           <property name="hibernate.hbm2ddl.auto" value="create" />
       </properties>
   </persistence-unit>
…
Notice the transaction-type attribute. It's JTA as opposed to RESOURCE_LOCAL in the case of non-JTA. You must also specify transaction manager lookup class which is server dependent.

DAO


@PersistenceContext
  private EntityManager em;

  @SuppressWarnings("unchecked")
  @Transactional(readOnly = true)
  public List<Dog> retrieveAllDogs() {
      return em.createQuery("from Dog").getResultList();
  }

That's it! To run this example you need a database server running. I'm using HSQLDB. You can download source code and there is a batch file there that runs this HSQLDB server. The application is to be deployed on JBoss 6.

You call the transactional operation by accessing http://localhost:8080/springtx-jta-0.0.1-SNAPSHOT/myurl.

pom.xml


For reference, the relevant parts of the POM file.

<dependencies>
       <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-core</artifactId>
           <version>3.6.0.Final</version>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-commons-annotations</artifactId>
           <version>3.2.0.Final</version>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.hibernate.javax.persistence</groupId>
           <artifactId>hibernate-jpa-2.0-api</artifactId>
           <version>1.0.0.Final</version>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-entitymanager</artifactId>
           <version>3.6.0.Final</version>
           <type>jar</type>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>hsqldb</groupId>
           <artifactId>hsqldb</artifactId>
           <version>1.8.0.7</version>
           <type>jar</type>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-core</artifactId>
           <version>3.0.3.RELEASE</version>
           <type>jar</type>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-aop</artifactId>
           <version>3.0.3.RELEASE</version>
           <type>jar</type>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-tx</artifactId>
           <version>3.0.3.RELEASE</version>
           <type>jar</type>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-api</artifactId>
           <version>1.5.8</version>
       </dependency>
       <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-simple</artifactId>
           <version>1.5.8</version>
       </dependency>
       <dependency>
           <groupId>commons-collections</groupId>
           <artifactId>commons-collections</artifactId>
           <version>3.1</version>
           <type>jar</type>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-test</artifactId>
           <version>3.0.3.RELEASE</version>
           <type>jar</type>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.8.1</version>
           <type>jar</type>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>3.0.3.RELEASE</version>
           <type>jar</type>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-orm</artifactId>
           <version>3.0.3.RELEASE</version>
           <type>jar</type>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>org.jboss.spec.javax.servlet</groupId>
           <artifactId>jboss-servlet-api_3.0_spec</artifactId>
           <version>1.0.0.Final</version>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>cglib</groupId>
           <artifactId>cglib</artifactId>
           <version>2.2</version>
       </dependency>
   </dependencies>
   <repositories>
       <repository>
           <id>r.j.o-groups-public</id>
           <url>https://repository.jboss.org/nexus/content/groups/public/</url>
       </repository>
   </repositories>

57 comments:

  1. Great post man , you have indeed covered the topic in great details, I have worked with Spring some time back and looking for some practical advice on that specially on AOP front and defining point-cut and real usage of those , would be great if you could write your experience on those.

    Thanks
    Top 20 Core Java Interview question

    ReplyDelete
  2. Hi Michal,

    Great post!

    A quick comment: If you are setting up a JTA-based persistence.xml, you may as well let JBoss do the PU deployment and retrieve it from JNDI (since JBoss will deploy the persistence unit anyway). You can also suppress the default deployment by renaming the PU definition file or by using jboss-ignore.txt. This is just so that you don't end up with the persistence unit created twice.

    ReplyDelete
  3. Hi Javin, thanks for visiting the blog. I did work with Spring AOP in a professional environment indeed. I may cover that in future.

    I already wrote some posts on plain AOP and I will probably focus on that now, if at all. It's a bit more powerful this way (plain AOP) than when used with Spring (but whether you need this extra power - manipulating class structure for instance - I'm not sure).

    ReplyDelete
  4. Hi Marius, thanks for your insight. I retrieve it from JNDI where? What exactly do I delegate to JBoss in this scenario?

    ReplyDelete
  5. Interesting read. I would trying out a similar setup with Glassfish as the JEE server and I assume the configuration would be similar. Do you have any idea about how are connections handled by spring in a distributed (JTA) transaction? I could see the spring class TransactionSynchronizationManager managing the connections as a thread local variable but not sure if this is the way spring guarantees same connection in a transaction

    ReplyDelete
  6. @amitstechblog

    In a JTA scenario, Spring is deferring resource (e.g. connection) management to the transaction service itself. This is why it is required that resources themselves are JTA-aware (e.g. managed datasources, JMS connection factories, managed persistence units/contexts or as in this example, a persistence unit that is JTA-synchronized - through the usage of the transaction lookup manager).

    ReplyDelete
  7. @Michal

    Here is a quick explanation of what I meant.

    1) Any Java EE module (WAR, EJB-JAR, EAR) that contains a META-INF/persistence.xml will cause the application server to create (and deploy) a persistence unit

    2) The persistence unit or a JTA-aware persistence context can be bound in JNDI - JBoss 5/6 support this in two separate ways:

    a) through specific properties in persistence.xml
    b) through the standard mechanism of resource reference registration - e.g. and in web.xml

    3) Spring applications can just access the managed persistence unit and persistence context through JNDI lookups, like here:

    http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/orm.html#orm-jpa-setup-jndi

    When I say 'delegate', I really mean that instead of instantiating a distinct EntityManagerFactory, a Spring application can simply reuse the one created by the application server).

    Unfortunately, the comment space is not very friendly to code snippets :), so perhaps I'll try post a separate example of how this would look like.

    ReplyDelete
  8. @Marius
    I am bit confused by your statement - "In a JTA scenario, Spring is deferring resource (e.g. connection) management to the transaction service itself. " Do you mean if I use Bitronix as the distributed transaction manager, bitronix would do the connection management? If that's the case does it also mean TransactionSynchronizationManager is not involved when the transactions are distributed?

    ReplyDelete
  9. Hey Michal,

    Sorry for the delay - unexpectedly busy weekend. I'll post something today (Monday).

    ReplyDelete
  10. As promised:

    http://second-kind-demon.blogspot.com/2011/06/spring-jpa-java-ee-jboss-deployment.html

    ReplyDelete
  11. hi michal..i am not using jta..i use hibernate instead for jboss..is it possible ?? i stuck with this error..as far as i noticed, i dont have any redundancy in my pom.xml dependency..

    Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in ServletContext resource [/WEB-INF/dao.xml]: Invocation of init method failed; nested exception is java.lang.LinkageError: loader constraint violation: when resolving field "logger" the class loader (instance of org/jboss/classloader/spi/base/BaseClassLoader) of the referring class, org/springframework/transaction/support/AbstractPlatformTransactionManager, and the class loader (instance of org/jboss/classloader/spi/base/BaseClassLoader) for the field's resolved type, org/springframework/transaction/support/AbstractPlatformTransactionManager, have different Class objects for that type
    11:24:53,098 INFO [STDOUT] at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:102)
    11:24:53,098 INFO [STDOUT] at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
    11:24:53,098 INFO [STDOUT] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1350)
    11:24:53,099 INFO [STDOUT] ... 94 more

    ReplyDelete
  12. Hi kuchai, it's possible not to use JTA.

    I think the error is caused by the fact that you're supplying an *out-of-date* implementation of something, and NOT that you're not supplying it at all.

    Thanks for visiting.

    ReplyDelete
  13. hi michal..
    is it seems that the transactionManager cant be created..by default jboss 6 carter for JTA implementation..might be that causes the prob? as i was creating the transactionManager bean like below for hibernate?

    ReplyDelete
  14. bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">

    </bean

    ReplyDelete
  15. Hi,

    If you don't want JTA: http://blog.m1key.me/2011/05/non-jta-transactions-with-hibernate.html

    ReplyDelete
  16. but that one need to provide entitymanager for the transactionManager..i am using session factory..

    ReplyDelete
  17. i upgraded my spring jar in pom.xml to use 3.0.5 release..still i got the same error:(

    ReplyDelete
  18. can we communicate through google mail instead :)..add me at kushairi22@gmail.com

    ReplyDelete
  19. can i just change the sessionfactory bean id below to entitymanager ?

    bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">




    hibernate.dialect=${hibernate.dialect}

    hibernate.query.substitutions=false
    hibernate.cache.use_second_level_cache=true
    hibernate.transaction.factory_class=org.hibernate.transaction.CMTTransactionFactory
    hibernate.transaction.manager_lookup_class=org.hibernate.transaction.JBossTransactionManagerLookup
    hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
    hibernate.temp.use_jdbc_metadata_defaults false
    hibernate.jdbc.batch_size=0



    </property

    ReplyDelete
  20. No, I don't think so. :) I think you need a proper setup of entity manager.

    ReplyDelete
  21. i using appfuse..by default there is some default jar using sessionfactory..i can override the files..but i not sure it may affect others or not..

    ReplyDelete
  22. the problem is only with the transactionManager...:(..

    ReplyDelete
  23. Does it mean you HAVE TO use session factory because something else demands it?

    ReplyDelete
  24. yup..i tried another way now..previously i install spring in jboss along with cxf jars using
    jbossws-cxf-bin-dist..but now i only install the cxf jars without the spring jars..all the spring jars i provide in my pom.xml similiar like u except for the hibernate jar where we need to put the scope as provided...
    BUT, jboss 6.0 Final is giving me class not found exception for the spring jar..seems that jboss 6 cant look into my war web-inf lib folder..do u previously encounter the same error ??

    ReplyDelete
  25. I did not encounter anything similar. Anyway, I'm sure it is possible to use session factory without JTA.

    If you're not using JTA, you need to give it the transaction manager yourself.


    How about this? @PersistenceUnit(unitName="custDb") SessionFactory factory;

    ReplyDelete
  26. is there any where that we can just declare the transaction manager in the context.xml ?? how do you deploy ur app to jboss ?? i am using maven-war-plugin..and then excludes the web.xml...

    ReplyDelete
  27. Türkiye'nin en hızlı full oyun indirme sitesine davetlisiniz.
    https://torrentoyunum.com

    ReplyDelete
  28. For latest information you have to visit web and on world wide-web I found this website as best web page for latest updates.Very nice post, I really love this website, keep it up thanks..To getting a visa for Turkey,There is a need to fill a visa on arrival Turkey through the Turkey visa online guide.

    ReplyDelete
  29. takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    takipçi satın al
    instagram takipçi satın al
    instagram takipçi satın al
    takipçi satın al
    takipçi satın al
    instagram takipçi satın al
    instagram takipçi satın al
    instagram takipçi satın al
    instagram takipçi satın al
    takipçi satın al
    instagram takipçi satın al

    ReplyDelete
  30. On your place I would make a video about these transactions and upload it on youtube. From here https://soclikes.com I would get more likes and views for it

    ReplyDelete
  31. Hmm , very informative article! Thanks to author for it! Don't you think it's worth make a video on this article and post it on Twitter? You can also buy twitter followers in order to quickly wind up their number.

    ReplyDelete
  32. You always comes with great stuff for your user. Thanks for being user friendly and Keep posting such great stuff 토토사이트

    ReplyDelete
  33. Great info. Thank you for sharing this fascinating information together. I am so blessed to discover this. Feel free to visit my website; 바카라사이트

    ReplyDelete
  34. I definitely enjoy every little bit of it. This is a great website and good stock.I want to thank you. Apply Online Indian visa, Fill and online Submit Visa Application Form Securely via India visa website.


    ReplyDelete
  35. The website is good and the stock is good. Thanks for all you do. e medical visa India, you can get medical visa India online via Indian evisa portal.

    ReplyDelete
  36. Thanks for the sharing this blog with us. I like it very much. 토토사이트

    ReplyDelete
  37. Hello, I wanted to write a little information related to Visa. Are you interested in traveling to a country? Yes, you can evisa apply online. You can fill out your visa application form online through our Visacent website within 5 to 10 minutes. We offer visas to citizens of over 190 countries. You can read more information about visas through our website.

    ReplyDelete
  38. I desire daily access to this kind of information. I'll read your content in full.
    Abogado De Trafico En Virginia

    ReplyDelete
  39. Excellent informative post. Keep posting more good blogs. Federal Criminal Defense Lawyer Maryland

    ReplyDelete
  40. Obtaining a London visa for Indians is a significant step for Indian travelers eager to explore the iconic city of London and the United Kingdom at large. Whether you're drawn to the historic landmarks like the Tower of London and Buckingham Palace, the world-class museums, or the vibrant culture, understanding the visa requirements and application process is crucial. This guide will provide you with essential information about securing a London visa as an Indian citizen, including visa types, eligibility criteria, application procedures, and necessary documents. By being well-prepared, you can embark on your London adventure with confidence and make the most of your journey.

    ReplyDelete
  41. Hello! I'd like to extend my heartfelt appreciation for the invaluable information shared on your blog. Experience convenience by apply Saudi visa online Save time, complete forms, and check requirements effortlessly via the official website for a stress-free travel experience.

    ReplyDelete
  42. I really enjoyed your post. It's informative and well-written. Looking forward to more from you! For Canadian visa applicants in Riyadh, VFS Canada Riyadh provides efficient and convenient services to facilitate your visa application process. Book your appointment today.

    ReplyDelete
  43. Nestled in the Eastern Himalayas, Sikkim is a stunning Indian state known for its breathtaking landscapes. The Gorgeous State of Sikkim in East Himalayas. With lush forests, dramatic mountains, and vibrant culture, it's a paradise for nature lovers and adventurers. Explore pristine lakes, monasteries, and trekking trails while immersing yourself in the rich local traditions. Sikkim is a hidden gem waiting to be discovered in the heart of the Himalayas.

    ReplyDelete
  44. Your blog is a digital masterpiece. Your passion and creativity shine through in every post.Prevención Violencia Doméstica Acto Nueva Jersey

    ReplyDelete
  45. It's important to reiterate how incredibly fantastic this post is. I'm sincerely thankful and want to express my deep appreciation for the exceptional content you've presented. Airbnb, Tourism Ministry Boost India Travel. Airbnb has joined hands with the Tourism Ministry to enhance India's travel landscape. This collaboration aims to promote unique and authentic travel experiences, supporting local communities and boosting tourism across the country.

    ReplyDelete
  46. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one.
    Abogado de Defensa Criminal Federal de Nueva Jersey

    ReplyDelete
  47. Combining Hibernate, JBoss 6, and Spring 3 allows developers to build scalable, reliable, and maintainable enterprise applications with ease. This stack streamlines the management of JTA transactions, simplifies database operations, and enhances the overall robustness and performance of your applications. Whether you're working on e-commerce platforms, customer relationship management systems, or other mission-critical applications, this technology stack provides the tools you need to build enterprise-grade solutions. virginia beach uncontested divorce forms
    virginia beach uncontested divorce manual

    ReplyDelete
  48. mejores abogados de accidentes de camionesThe post aims to provide a comprehensive tutorial on the integration of JTA transactions with JBoss 6, Spring 3, and Hibernate 3. It introduces the key technologies involved, including JBoss 6, Spring 3, and Hibernate 3, setting the context for the tutorial. The post also clarifies the target audience, stating that it doesn't need a JEE server but can use Tomcat with potential issues. The code assumes code portability, promoting adaptability. The content is concise, focusing on essential information without unnecessary details. The choice of JBoss 6 is contextually relevant, providing context for readers unfamiliar with the server landscape. The phrase "with minor tweaks" encourages readers to adapt the code to their specific server environments, fostering engagement. The server dependency acknowledgement acknowledges the reality of varying server configurations. The closing invitation invites readers to follow the tutorial and explore the integration of JTA transactions with these technologies.

    ReplyDelete
  49. Integrating JTA transactions with Hibernate, JBoss 6, and Spring 3 is a crucial aspect of enterprise Java development. This synergy ensures seamless coordination between databases and application servers, providing robust transactional support. The combination of Hibernate's ORM, JBoss 6's application server, and Spring 3's framework enhances reliability and scalability in Java-based projects. This blog is a goldmine of information. Your blog packs a punch in just a few sentences. Your words are like gems. Thank you for sharing this! A quick, delightful read that left me inspired.Mutual Consent Divorce Maryland

    ReplyDelete
  50. The Federal Criminal Defense Lawyer perseveres in the face of difficulty, much like the famous Manhattan skyline. Whether it's a case involving national security, drug-related offenses, or white-collar crime, they are prepared to take on any task thanks to their arsenal of legal expertise and unwavering spirit. The attorney creates a solid defense that stands the test of time by bridging the distance between the accused and their constitutional rights, just like the city's bridges connect its boroughs. New York City Federal Criminal Defense Lawyer

    ReplyDelete
  51. Very excellent and informative content. I was hunting for this type of material and thoroughly loved reading it.Abogados de Criminal en Condado de Prince William VA

    ReplyDelete
  52. Having an able guide can be extremely beneficial when navigating the frequently choppy waters of divorce. Accordance Advisors shows itself as a source of knowledge . Top New York Divorce Lawyers

    ReplyDelete
  53. JTA (Java Transaction API) transactions with Hibernate facilitate managing distributed transactions across multiple resources. Hibernate seamlessly integrates with JTA, allowing for coordinated transaction management in enterprise applications. This integration ensures atomicity, consistency, isolation, and durability (ACID properties) across database and other resource operations within a transactional context. chapter 7 bankruptcy lawyers near me

    ReplyDelete