How to start Transaction in JTA EntityManager

别来无恙 提交于 2019-12-25 12:03:14

问题


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</class>
    <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


回答1:


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.




回答2:


Finally I could handle this by changing the line:

<persistence-unit name="HMC" transaction-type="RESOURCE_LOCAL">


来源:https://stackoverflow.com/questions/5049763/how-to-start-transaction-in-jta-entitymanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!