Cosmin Cosmin
Cosmin Cosmin

Reputation: 1556

Why doesn't Hibernate writes changes when using UserTransaction in Servlet?

With Hibernate:

@PersistenceUnit(unitName = "oracle")
private EntityManagerFactory emf;
@Resource
private UserTransaction u;

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         EntityManager em = emf.createEntityManager();
try {
         u.begin();
         em.persist(some entity);
         u.commit();
     } catch (Exception e) {
         e.printStackTrace();
     }

This doesn't write anything to the database, if I switch to EclipseLink it works ok.
If I use

EntityTransaction et = em.getTransaction();

instead of UserTransaction hibernates writes to the DB. (So somehow hibernate doesn't see the JTA, like EclipseLink does).



What's wrong with Hibernate ? (4.0.0-Final) Thanks

edit:

I added the last 2 properties: (i'm using glassfish)

<properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
    <property name="hibernate.show_sql" value="true"/>
    <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory" />
    <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup" />
</properties>

but still doesn't work

Upvotes: 2

Views: 3261

Answers (3)

Vedran
Vedran

Reputation: 11029

For me it started working correctly only after I removed the following line:

<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory" />

Upvotes: 0

Cosmin Cosmin
Cosmin Cosmin

Reputation: 1556

I removed the last two properties, as they aren't necessary.
My problem was that I was creating the EM outside the u.begin(), and I should create it inside the transaction or call em.joinTransaction().

Thanks:

I got my answer from here: Hibernate JPA with JTA and Glassfish Application Server doesn't seem to commit

Upvotes: 2

Matej
Matej

Reputation: 6209

Make sure that Hibernate will be aware of the JTA transaction manager to enlist itself as a transaction resource.

Configure the *hibernate.transaction.factory_class* property to org.hibernate.transaction.JTATransactionFactory and the *hibernate.transaction.manager_lookup_class* property to the transaction manager lookup class for your server, e.g. org.hibernate.transaction.JBossTransactionManagerLookup, or implement your own by inheriting from org.hibernate.transaction.JNDITransactionManagerLookup.

Sample hibernate.properties:

hibernate.transaction.factory_class = org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_lookup_class = org.hibernate.transaction.JBossTransactionManagerLookup

See the Hibernate Reference Documentation, Section 3.3. JDBC connections for more information.

Upvotes: 1

Related Questions