-->
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.  [ 6 posts ] 
Author Message
 Post subject: Suport of nested JDBC transactions in hibernate3.2 is absent
PostPosted: Fri Oct 20, 2006 6:40 am 
Newbie

Joined: Fri Oct 20, 2006 6:09 am
Posts: 3
Hello people
I have a problem with the nested trnasactions with the 3.0.2.cr5:

Code:
Session session = factory.openSession();
Transaction tx = null;
try {
    tx = session.beginTransaction();
    // Do some work
    session.load(...);
    session.persist(...);

    Transaction nested = null;
    try {
          nested = session.beginTransaction();
          // Do some work
          session.load(...);
          session.persist(...);

          nested.commit();
    }catch(RuntimeException e){
          nested.rollback();
    }

    if (nested.wasCommited()){
         // Do some work
         session.load(...);
         session.persist(...);
    }else{
         // Do some other work
         session.load(...);
         session.persist(...);
    }

    tx.commit();  // place which causes an exception now
}
catch (RuntimeException e) {
    tx.rollback();
    throw e;
}
finally {
    session.close();
}


in result I found and exception:
Code:
Exception in thread "main" org.hibernate.TransactionException: Transaction not successfully started
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:100)
....



It was working in the 3.0.5 but now it's broken see code of hibernate code
Code:
SessionImpl.java frgment :

   public Transaction beginTransaction() throws HibernateException {
      errorIfClosed();
      if ( rootSession != null ) {
         // todo : should seriously consider not allowing a txn to begin from a child session
         //      can always route the request to the root session...
         log.warn( "Transaction started on non-root session" );
      }
      Transaction result = getTransaction();
      result.begin();
      return result;
   }

   public Transaction getTransaction() throws HibernateException {
      errorIfClosed();
      return jdbcContext.getTransaction();
   }

JDBCContext.java frgment :
       public Transaction getTransaction() throws HibernateException {
      if (hibernateTransaction==null) {
         hibernateTransaction = owner.getFactory().getSettings()
               .getTransactionFactory()
               .createTransaction( this, owner );
      }
      return hibernateTransaction;
   }




Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 20, 2006 7:18 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
There is no such thing as a nested JDBC transaction.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 16, 2006 8:26 am 
Newbie

Joined: Fri Oct 20, 2006 6:09 am
Posts: 3
Christian, Possibly, I was using wrong terminology. However see how it possible to organize nested transactions with the JDBC.

Code:
Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost/example", "postgres", "******");
connection.setAutoCommit(false);
//start outer transaction
connection.createStatement().execute("start transaction");
try{
     .....
     // some work
     ...
     // start inner transaction
     connection.createStatement().execute("start transaction");
     try{
         ....
         // some work
         ....
         // commit inner transaction
         connection.commit();
     }catch(Throwable e){
         // rollback inner transaction
         conection.rollback();
         ...
         // process exception
     }
     // comimt outer transaction
     connection.commit();
}catch(Throwable e){
        // rollback outer transaction
        conection.rollback();
         ...
         // process exception
}

Is anyone could say something helpful about the initial problem?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 16, 2006 8:31 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
This code is completely broken, it should't even run. Repeat: There are no nested transactions in JDBC. Your DBMS is just not throwing exception when you call "start transaction" after the first "start transaction".

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 16, 2006 11:02 am 
Newbie

Joined: Fri Oct 20, 2006 6:09 am
Posts: 3
Christian actually the last code is realy wrong. Thank you! And the problem is in the selected Database. I found that Postgress doesn't support nessted transactions well (they are suggesting savepoints instead).
But the question is not in JDBC. JDBC just passes SQL to the database.

Try such example sample with the MySQL (InnoDB).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 16, 2006 6:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The closest you can get to supporting nested Transactions is to use Transaction Suspension - see CMT standard. True nested transactions are not supported.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.