users@glassfish.java.net

Hibernate strange behavior

From: <glassfish_at_javadesktop.org>
Date: Tue, 31 Mar 2009 01:24:42 PDT

Hello, I'm using hibernate 3 with Glassfish 9.1_02

I'm having some issues with transactions, according to Hibernate documentation, the use of org.hibernate.transaction.JTATransactionFactory lets Hibernate to check weather a Container Managed Transaction is running and if so relying on that, otherwise creates a new transaction using a new transaction (looking up for java:comp/UserTransaction).
my code is something like:

Transaction tx = null;
try {
   
    tx = factory.getCurrentSession().beginTransaction();

    // Do some work
    factory.getCurrentSession().load(...);
    factory.getCurrentSession().persist(...);

    tx.commit();
}
catch (RuntimeException e) {
    tx.rollback();
    throw e;
}

This is inside an EJB 3 method. If I try to run this code, I got the following exception:

 javax.naming.NameNotFoundException:
Lookup of java:comp/UserTransaction not allowed for Container managed Transaction beans at com.sun.ejb.containers.BaseContainer.checkUserTransactionLookup(BaseContainer.java:691)
        at com.sun.enterprise.naming.java.javaURLContext.lookup(javaURLContext.java:190)
        at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:396)
        at javax.naming.InitialContext.lookup(InitialContext.java:392)
        at org.hibernate.transaction.JTATransaction.<init>(JTATransaction.java:60)

So it looks like bean is under Container Manager Transaction. Why Hibernate doesn't detect this and creates a new transaction?

Following the hibernate documentation, I tried to switch to org.hibernate.transaction.CMTTransactionFactory
which should rely on existing CMT. I changed my code to something like:

@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void doSomeWork() {
    // Do some work
    factory.getCurrentSession().load(...);
    factory.getCurrentSession().persist(...);
}

So I let the container to manager the transaction, but if I try to save some simple object via hibernate, nothing is written to the DB, thus I guess there's no transaction.commit() executed.

The only way to let it work is to use
org.hibernate.transaction.CMTTransactionFactory and switch back to explicit transaction handling (first code sample) but this is not in accordance with documentation.

Any help would be really appreciated.

Stefano Emiliozzi
[Message sent by forum member 'thepomy' (thepomy)]

http://forums.java.net/jive/thread.jspa?messageID=339782