Search code examples
hibernateservletstransactionsejbjta

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


Solution

  • 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