Community   SpringSource   Projects    Downloads    Documentation    Forums    Training   Exchange   Blogs

Go Back   Spring Community Forums > Core Spring Projects > Data Access

Reply
 
Thread Tools Display Modes
  #1  
Old Dec 12th, 2007, 07:39 AM
jimpo jimpo is offline
Member
 
Join Date: Aug 2007
Posts: 57
Default JPA + JMS + JTA transactions

I have not found clear answers for this, maybe because JPA & JMS are too short words to search for in this forum

Say I have a method which does some updates to db using JPA and sends some messages using JMS. I want the method to execute transactionally. For this I need to use JTA, Spring's own transaction management handling is not enough (correct?).

How should I configure the different transactionManagers etc. to achieve this?

I am using Spring 2.0.x (although upgrading to 2.5 might be an option in case it is critical).
Reply With Quote
  #2  
Old Dec 12th, 2007, 07:45 AM
Marten Deinum Marten Deinum is offline
Senior Member
 
Join Date: Jun 2006
Location: The Netherlands
Posts: 9,296
Default

Quote:
Spring's own transaction management handling is not enough (correct?).
Spring doesn't really do transaction management it delegates to underlying transaction managers. There is also one for jta JTATransactionManager. You need however a container which supports that or configure a standalone JTA implementation like JOTM.
__________________
Marten Deinum
  • Senior Java Consultant
  • SpringSource Certified Trainer
Conspect ICT diensten
Blog
LinkedIn
Use the [ code ] tags, young padawan
Reply With Quote
  #3  
Old Dec 12th, 2007, 07:53 AM
jimpo jimpo is offline
Member
 
Join Date: Aug 2007
Posts: 57
Default

Quote:
Originally Posted by mdeinum View Post
Spring doesn't really do transaction management it delegates to underlying transaction managers. There is also one for jta JTATransactionManager. You need however a container which supports that or configure a standalone JTA implementation like JOTM.
Yep, that's what I fgured. What I am unsure of is how to configure the JPA, JTA and JMS related settings so that all participate in the JTA transaction nicely.
Reply With Quote
  #4  
Old Dec 12th, 2007, 08:01 AM
Marten Deinum Marten Deinum is offline
Senior Member
 
Join Date: Jun 2006
Location: The Netherlands
Posts: 9,296
Default

Configure them as you normally would. The only difference that instead of a JPATransactionManager you configure a JtaTransactionManager thats it... No more no less...
__________________
Marten Deinum
  • Senior Java Consultant
  • SpringSource Certified Trainer
Conspect ICT diensten
Blog
LinkedIn
Use the [ code ] tags, young padawan
Reply With Quote
  #5  
Old Dec 12th, 2007, 08:23 AM
jimpo jimpo is offline
Member
 
Join Date: Aug 2007
Posts: 57
Default

I still don't quite get it. How do I configure my JPA entityManager (persistenceManager, or some other bean) to be aware of the JTA transactionManager?

When I use plain JPA (without JTA) my JPA transactionManager and JPA entity manager are connected by the transactionManager config:

Code:
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>
If I use jtaTransactionManager instead, this approach is of course no longer possible. How do I create the link between jtaTransactionManager and JPA entityManagerFactory (or some other JPA bean) so that JPA becomes aware of the JTA transactions?
Reply With Quote
  #6  
Old Dec 12th, 2007, 08:54 AM
Marten Deinum Marten Deinum is offline
Senior Member
 
Join Date: Jun 2006
Location: The Netherlands
Posts: 9,296
Default

You don't have to. You only have to make sure that all of your DataSources are XA capable.
__________________
Marten Deinum
  • Senior Java Consultant
  • SpringSource Certified Trainer
Conspect ICT diensten
Blog
LinkedIn
Use the [ code ] tags, young padawan
Reply With Quote
  #7  
Old Dec 12th, 2007, 09:09 AM
jimpo jimpo is offline
Member
 
Join Date: Aug 2007
Posts: 57
Default

Quote:
Originally Posted by mdeinum View Post
You don't have to. You only have to make sure that all of your DataSources are XA capable.
So, some kind of magic happens behind the scenes? In which class: JtaTransactionManager, LocalContainerEntityManagerFactoryBean, ...?

Is this documented somewhere, in Spring HTML documentation, or in Javadocs? Or do I need to look at the source code for this kind of information?

I have not found much documentation on this, but I found one example on http://erich.soomsam.net/2007/04/. I don't fully understand all the code on that page (which is partly the reason I created this thread).

Here, there seems to be lots of configuration for connecting JPA and JTA, such as
Code:
    <!-- configure Hibernate to participate in JTA transactions using the JOTM transaction manager and
         specify further Hibernate specific configuration properties -->
    <property name="jpaPropertyMap">
      <map>
        <entry key="hibernate.transaction.manager_lookup_class"
               value="org.hibernate.transaction.JOTMTransactionManagerLookup"/>
        <entry key="hibernate.transaction.flush_before_completion"
               value="true"/>
        <entry key="hibernate.transaction.auto_close_session"
               value="true"/>
        <entry key="hibernate.current_session_context_class"
               value="jta"/>
        <entry key="hibernate.connection.release_mode"
               value="auto"/>                            
      </map>
    </property>
Isn't this necessary, then?

Appreciate all the comments, thanks.
Reply With Quote
  #8  
Old Dec 12th, 2007, 09:47 AM
dejanp dejanp is offline
Senior Member
 
Join Date: Sep 2004
Posts: 1,086
Default

It could work with Hibernate, but if you want to do it "by the book" you would have to define the jtadatasource in persistence.xml.
Reply With Quote
  #9  
Old Dec 14th, 2007, 02:14 AM
jimpo jimpo is offline
Member
 
Join Date: Aug 2007
Posts: 57
Default

Quote:
Originally Posted by dejanp View Post
It could work with Hibernate, but if you want to do it "by the book" you would have to define the jtadatasource in persistence.xml.
Trying to gather up all the pieces... So, for my JPA persistence to work correctly with JTA transaction manager, I need to

1. use transaction-type="JTA" in persistence.xml
2. define a JTA transaction manager
3. use XA capable dataSources

That's it? I don't configure the link between JPA (entityManagers etc) and JTA in any way? Isn't there any documentation on this?

What's with all the extra configuration in http://erich.soomsam.net/2007/04/ ? Why doesn't he just do the 3 points from the list above?
Reply With Quote
  #10  
Old Dec 14th, 2007, 02:19 AM
Marten Deinum Marten Deinum is offline
Senior Member
 
Join Date: Jun 2006
Location: The Netherlands
Posts: 9,296
Default

The link here is the datasource, you have to make sure that you use the same datasource. For JTA is doesn´t matter what type of transactional resource it is, as long as it it XA capable, which is what you want how else would one integrate JMS and JDBC and ... if you need to make all the specific implementations aware of the JTATransaction.....

Why he does the extra configuration, I have no idea, he could wire them up.

I registered a JIRA issue and placed a request for documentation, this issue seems to arise more and more on the forums. Would be nice if we had some reference to point to.
__________________
Marten Deinum
  • Senior Java Consultant
  • SpringSource Certified Trainer
Conspect ICT diensten
Blog
LinkedIn
Use the [ code ] tags, young padawan

Last edited by Marten Deinum; Dec 14th, 2007 at 02:24 AM.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:08 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.