How to start Transaction in JTA EntityManager
I have JPA mapping to HSQLDB and persistence.xml reads as below :
<persistence-unit name="HMC">
<jta-data-source>java:hmc</jta-data-source>
<class>org.hmc.jpa.models.BloodGroup</class>
<class>org.hmc.jpa.models.ContactInfo</cla开发者_如何转开发ss>
<properties>
<property name=hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
</properties>
</persistence-unit>
and get EntityManager as :
entManagerFactory = Persistence.createEntityManagerFactory("HMC");
I also have datasource defined in my JBoss5.1 for hsqldb. If I begin transaction, it throws IllegalStateException : A JTA EntityManager cannot use getTransaction()
Can anybody let me know how to start and commit the transactions under these circumstances.
Regards,
Satya
this is what the javadocs for getTransaction says...
EntityTransaction getTransaction()
Return the resource-level EntityTransaction object. The EntityTransaction instance may be used serially to begin and commit multiple transactions.
Returns:
EntityTransaction instance
Throws:
IllegalStateException - if invoked on a JTA entity manager
So basically it means that if the transaction-type attribute is JTA with a jdbc XA datasource, then you'd get an IllegalStateException.
Suggested fix: Try to set the transaction-type to resource-local with a localTx jdbc datasource or else it will lead to an IllegalStateException.
Finally I could handle this by changing the line:
<persistence-unit name="HMC" transaction-type="RESOURCE_LOCAL">
精彩评论