2 Replies Latest reply on Nov 25, 2005 9:04 AM by tads

    Hibernate + JBoss + Transaction/Session scope

    Johann DUPUIS Newbie

      It seems that several hibernate sessions are in one way sharing the same Transaction.

      Let me explain my problem:

      I've 2 different EJBs, the first one (i will call it 'EJBA') is stateful, and call
      the second one (i will call it 'EJBB') is stateless.

      Here is what I'm doing:

      Scenario 1:
      1. EJBA is instanciated
      2. An hibernate session (A) is associated to EJBA:
      InitialContext ic = new InitialContext();
      SessionFactory sessionFactory = (SessionFactory) PortableRemoteObject.narrow(ic.lookup(
      "java:/HibernateFactory"), SessionFactory.class);
      hibernateSession = sessionFactory.openSession();
      3. trA= hibernateSession.beginTransaction() is done (on session A)
      4. Two queries are done against the session (A) in EJBA, i will call them 'QueryA1' and 'QueryA2'
      5. hibernateSession (session A) .isDirty()? returns true
      ==> OK, i agree
      6. A function of EJBB is called by EJBA, inside this function, the EJBB get a session (B) to do some queries:
      InitialContext ic = new InitialContext();
      SessionFactory sessionFactory = ( SessionFactory ) PortableRemoteObject.narrow( ic.lookup( "java:/HibernateFactory" ), SessionFactory.class );
      Session session= sessionFactory.openSession();
      7. session (session B) .isDirty? returns false
      ==> OK, i agree
      1st conclusion: session A and session B are two different sessions (it is was I'm expecting)
      8. trB= session.beginTransaction() is done (on session B)
      9. A query is done against the session (B) in EJBB, i will call it 'QueryB1'
      10. trB.commit() is called (I mean i want to commit the QueryB1 made by EJBB inside session B.
      11. session B is closed
      12. (back to EJBA)
      trA.rollback() is issued by EJBA (to rollback QueryA1 and QueryA2.
      13. Have a look at the result in database:
      Everything is rollbacked! (my step 10 commit is ignored!).


      Scenario 2:
      Replacing the step 12 by a trA.commit(), then in step 13 everything is commited.

      Scenario 3:
      Same as scenario 1, but I continue after the trA.rollback().
      Since I've rollbacked the 'QueryA1' and 'QueryA2', I want to commit a 'QueryA3'.
      14. trA= hibernateSession.beginTransaction() is done (on session A) [again]
      I get the exception:
      16:42:23,611 ERROR [JTATransaction] Could not register Synchronization
      javax.transaction.RollbackException: Already marked for rollback
      at org.jboss.tm.TransactionImpl.registerSynchronization(TransactionImpl.java:689)
      at net.sf.hibernate.transaction.JTATransaction.begin(JTATransaction.java:169)
      at net.sf.hibernate.transaction.JTATransactionFactory.beginTransaction(JTATransactionFactory.java:58)
      at net.sf.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:2195)
      at com.eft.cbk.kernel.tar.business.TARTasks.createCaseInCBK(TARTasks.java:251)

      15. A function of EJBB is called by EJBA, inside this function, the EJBB get a session (B) to do some queries:
      InitialContext ic = new InitialContext();
      SessionFactory sessionFactory = ( SessionFactory ) PortableRemoteObject.narrow( ic.lookup( "java:/HibernateFactory" ), SessionFactory.class );
      Session session= sessionFactory.openSession();
      16. session (session B) .isDirty? returns false
      ==> OK, i agree
      1st conclusion: session A and session B are two different sessions (it is was I'm expecting)
      17. trB= session.beginTransaction() is done (on session B)
      I get the exception:
      16:42:23,440 ERROR [JTATransaction] Could not register Synchronization
      javax.transaction.RollbackException: Already marked for rollback
      at org.jboss.tm.TransactionImpl.registerSynchronization(TransactionImpl.java:689)
      at net.sf.hibernate.transaction.JTATransaction.begin(JTATransaction.java:169)
      at net.sf.hibernate.transaction.JTATransactionFactory.beginTransaction(JTATransactionFactory.java:58)
      at net.sf.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:2195)
      at com.eft.cbk.v2x.database.api.entity_seq.Entity_seqTable.getEntitySeq(Entity_seqTable.java:74)



      What is wrong?
      I have two different beans, with each a different hibernate session that should work with two different transactions.
      It seems the sessions differs, but there is a "link" between the both transactions.

      Help would be greatly appreciated.

      Environment:
      JBoss: 3.2.3
      Hibernate: 2.1.2
      Sun JDK: j2sdk1.4.2_03
      OS: Windows 2000
      Database: Thin Driver Oracle 8.1.7