File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes How to use JTA with JPA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to use JTA with JPA" Watch "How to use JTA with JPA" New topic
Author

How to use JTA with JPA

hildich kilal
Ranch Hand

Joined: Aug 25, 2008
Posts: 44
Hello ,
We have a project which is using JPA (over hibernate). We are using Weblogic 11g application server.
Now till now we were using non -jta transaction throughout our application. All jdbc action was being done in DAO classes , using persistencemanager.

There is a requirement to use JTA at some places where we need to execute JDBC, JMS and some cache updates in one global transactions. For this we need to use JTA now.

So few questions I have are
1) can we use JTA and Non - Jta transactions using same persistenceUnit?
2) Do I need to define a JTA data source in JTA transaction?
3) To use JTA transaction in this case is following good enough
- Define hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_lookup_class=org.hibernate.transaction.WeblogicTransactionManagerLookup
- Use JNDI to get UserTransactions and begin and commit it.
- create XA resources for hibernate and JMS between these.
How will Hibernate or JMS code will know that we need to associate the transaction with a global user transaction in this case?
Code I have is as follows

My persistence.xml looks like following


my jpa.properties looks like following


where MyDS is a non JTA data source defined in my weblogic server.

Also we have own classes to create PersistenceManager by parsing the persistence.xml and using persistenceUnitInfo classes.

Any pointers regarding this will be really helpful.

Thanks
Hildich K
debraj mallick
Ranch Hand

Joined: Mar 08, 2011
Posts: 188

hi hildich kilal,

did you got the any solution?
hildich kilal
Ranch Hand

Joined: Aug 25, 2008
Posts: 44
debraj mallick wrote:hi hildich kilal,

did you got the any solution?


Hello Debraj,
Following are my findings after some research and readings

So few questions I have are
1) can we use JTA and Non - Jta transactions using same persistenceUnit?

No . We can not use JTA and Non JTA transactions in same PersistenceUnit. We need to create separate Persistence Unit. in J2SE application you can simply use to create entity manager factory but not in a J2EE. In an EE container you need to inject EntityManager using Container's injection mechanisms.
2) Do I need to define a JTA data source in JTA transaction?

Yes you need to use JTA data source in JTA transaction. It helps to avoid unnecessary connection pooling handling. In fact it is strongly advisable to use Data sources (or connection pooling apps like c3p0) in non jta applications also. I did use c3p0 / Data source from Weblogic successfully to resolve connection out of limit issue in my non JTA application recently.


Hope it helps
Thanks
Hk
debraj mallick
Ranch Hand

Joined: Mar 08, 2011
Posts: 188

hi hildich kilal,

above information really helped me a lot,
in your persistance, you have used transaction-type="RESOURCE_LOCAL", how to use JTA here?
hildich kilal
Ranch Hand

Joined: Aug 25, 2008
Posts: 44
Debraj,
FOllowing is the way you can use JTA in persistence unit



Make sure that you use a JTA data source with this persistence unit. You can define data source in a separate properties file or define it in persistence.xml iteself. Look for persistence.xml documentation to see how to use data source with hibernate.

Thanks
debraj mallick
Ranch Hand

Joined: Mar 08, 2011
Posts: 188

Thanks hildich kilal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use JTA with JPA