Satya
Satya

Reputation: 2124

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

Upvotes: 0

Views: 6920

Answers (2)

Satya
Satya

Reputation: 2124

Finally I could handle this by changing the line:

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

Upvotes: 1

Anuj Kaushal
Anuj Kaushal

Reputation: 61

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.

Upvotes: 2

Related Questions