-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Rollback doesn't work with JTA transaction
PostPosted: Tue May 09, 2006 3:07 am 
Beginner
Beginner

Joined: Tue May 09, 2006 2:30 am
Posts: 22
Hi !

I have a problem with transactions that does not rollback as expected. I'm using Hibernate with JTA inside a Jonas container.

I have several transactions done in the same hibernate session. If a transaction rollback and the next one commit, both are commited !

In the following example reproducing the problem, I use JTA transaction demarcation (but the problem also exists with CMT transaction demarcation) :

Code:
  /**
    * @ejb.interface-method
    *  view-type = "local"
    * @ejb.transaction
    *  type = "RequiresNew"
    */
  public void makeTransaction(boolean rollBack, String name, String lname, SiteH site) throws Exception
  {
    Session session = HibernateUtil.currentSession().getSession();
    Transaction hibernateTransaction = session.beginTransaction();

    PersonalDetailH test = new PersonalDetailH(name, lname, Boolean.FALSE, site);
    session.save(test);

    if (rollBack)
    {
      hibernateTransaction.rollback();
      throw new Exception("exception");
    }
    else
      hibernateTransaction.commit();
  }

  /**
    * @ejb.interface-method
    *  view-type = "remote"
    * @ejb.transaction
    *  type = "Never"
  */
  public void testTransactionsRollBack() throws Exception
  {
    try
    {
      SessionMoneyTestLocal thisSession = (SessionMoneyTestLocal)sessionContext.getEJBLocalObject();
      SiteH site = SiteH.getEx(new Integer(3));
      try {
        thisSession.makeTransaction(false, "NO ROLL BACK", "1", site);
      } catch (Exception e) {}
      try {
        thisSession.makeTransaction(true, "ROLL BACK !!", "2", site);
      } catch (Exception e) {}
      try {
        thisSession.makeTransaction(true, "ROLL BACK !!", "3", site);
      } catch (Exception e) {}
      try {
        thisSession.makeTransaction(false, "NO ROLL BACK", "4", site);
      } catch (Exception e) {}
      try {
        thisSession.makeTransaction(true, "ROLL BACK !!", "5", site);
      } catch (Exception e) {}
      try {
        thisSession.makeTransaction(false, "NO ROLL BACK", "6", site);
      } catch (Exception e) {}
      try {
        thisSession.makeTransaction(true, "ROLL BACK !!", "7", site);
      } catch (Exception e) {}
    }
    finally
    {
      HibernateUtil.closeSession();
    }
  }


database state:
Before calling testTransactionsRollBack :
Code:
saga_celine_money=# select first_name, last_name from personal_detail where site_id=3 order by id;
  first_name | last_name
------------+-----------
(0 rows)


After calling testTransactionsRollBack :
Code:
saga_celine_money=# select first_name, last_name from personal_detail where site_id=3 order by id;
  first_name  | last_name
--------------+-----------
NO ROLL BACK | 1
ROLL BACK !! | 2
ROLL BACK !! | 3
NO ROLL BACK | 4
ROLL BACK !! | 5
NO ROLL BACK | 6
(6 rows)


Expected database state after call :
Code:
saga_celine_money=# select first_name, last_name from personal_detail where site_id=3 order by id;
  first_name  | last_name
--------------+-----------
NO ROLL BACK | 1
NO ROLL BACK | 4
NO ROLL BACK | 6
(3 rows)


Hibernate version:
3.1.3
EJB container
JONAS 4.7.3
database:
PostgreSQL, version: 8.1.3

Can anyone else confirm this ?

Regards.

_________________
--
Celine


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 10:59 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Are you properly configuring the TransactionFactory that Hibernate should use in each case (hibernate.transaction.factory_class)?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 11:56 am 
Beginner
Beginner

Joined: Tue May 09, 2006 2:30 am
Posts: 22
Yes, I did set this property to 'org.hibernate.transaction.JTATransactionFactory'.

_________________
--
Celine


Top
 Profile  
 
 Post subject: Don't use both CMT and Hibernate Transaction
PostPosted: Tue May 30, 2006 8:48 am 
Newbie

Joined: Mon Feb 27, 2006 2:58 am
Posts: 10
Hi

You should avoid using both CMT and hibernate transaction together.Because CMT lies above the session, but hibernate transaction is governed by the session.I mean, you can open any hibernate transactions within a session.Similarly you can open/getcurrentsession any sessions within a CMT.Actually both are different transactions.If you use both ,hibernate won't open a new transaction, since already a CMT exists so simply it will delegate to the existing CMT.but problem will araise when you commit, actually when you commit hibernate transaction you are commiting CMT, which is the container job.

Problem: In your case, both are commited by CMT, though you rollback hibernate transaction
Solution: Either use CMT or hibernate transaction . Also use sessionfactory.getCurrentSession() which will give you the same session across the transaction


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 30, 2006 9:14 am 
Beginner
Beginner

Joined: Tue May 09, 2006 2:30 am
Posts: 22
Hi

Thanks for the time you spend in responding me be I can't see why are both transactions commited by CMT : after setting the Hibernate transaction to rollback, I throw an exception, that should make the CMT Transaction to rollback to !

In fact I tried the same code using CMT transaction only (no hibernate transaction demarcation) and it ended with the same result....

I really don't understand this behavior !

_________________
--
Celine


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.