-->
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.  [ 2 posts ] 
Author Message
 Post subject: Transaction session.save ,session.rollback and session.flush
PostPosted: Mon Jul 26, 2004 6:47 pm 
Beginner
Beginner

Joined: Wed Sep 24, 2003 8:27 am
Posts: 36
Team,

I have some code like this

Code:
method{
      do{t = session.transaction;
          try{
                t.begintransaction
                ----code----
                 VO p = new VO();
                 session.save(p);       // db insert not yet called         
                  ---code---                       
                 t.comit;
              }catch{
                         t.rollback;
                       }
           }
}//method end


ID is generated by a custom class that i have built which basically reads a database table for id and updates the same at the same time.

Now the above does not work as the t.rollback does not rollback the saved object. Moreover the next call to flush still inerts the Object as the tx.rollback is just a sql level rollback and not object level.

To correct that i did this and it worked beutifully, meaning that the object was not inserted when session.flush was called next time at some other place in the code.

Code:
method{
      do{t = session.transaction;
          try{
                t.begintransaction
                ----code----
                 VO p = new VO();
                 session.save(p);       // db insert not yet called
                 session.flush();        // db insert forced.         
                  ---code---                       
                 t.comit;
              }catch{
                         t.rollback;
                       }
           }
}//method end


The above code did the real rollback as needed.

However the next time it comes to this method the same id is got from the db as it is available and the error says that "id already asssociated with session". So i did this.

Code:
method{
      do{t = session.transaction;
          try{
                t.begintransaction
                ----code----
                 VO p = new VO();
                 session.save(p);       // db insert not yet called
                 session.flush();        // db insert forced.       
                 session.evict(p)       // forced remove from session 
                  ---code---                       
                 t.comit;
              }catch{
                         t.rollback;
                       }
           }
}//method end


Still i get the same error "id already asssociated with session".

Any clues..

Suchak Jani


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 27, 2004 11:34 pm 
Senior
Senior

Joined: Tue Nov 25, 2003 9:35 am
Posts: 194
Location: San Francisco
The Hibernate Session is a unit of work. After you have committed or rolled back, you should close the session and get a new one for the next unit of work.


Sherman


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