• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use JTA with JPA

 
hildich kilal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi hildich kilal,

did you got the any solution?
 
hildich kilal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks hildich kilal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic