-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem with rollback
PostPosted: Mon Jan 25, 2010 4:04 pm 
Newbie

Joined: Tue Jun 16, 2009 4:11 pm
Posts: 8
Hi, i'm trying to save two objects using the save method on the same session, the first object is violating a constraint on database, after it raises an error i rollback the transaction and try to save the second object, when i call the save method for the second object, i'm getting the same error as i was before, its like the rollback didn't work and the commit is trying to save the first object.

ps: I can't call the clear method on the session, cause it would detach some objects from the session

Any ideas?
Thanks.

Code:
      Session session = PersistenceUtil.getSession("LOCAL");
      Query createQuery = session.createQuery("from Language where id = -1");
      
      List<Language> list = createQuery.list();
      Language language = list.get(0);
      
      Transaction beginTransaction = null;
      try {
         beginTransaction = session.beginTransaction();
         //heres the constraint it violates
         language.setCode("99");
         session.save(language);         
         session.getTransaction().commit();
      } catch (Exception e) {
         System.out.println("FIRST ERROR! OK!");
         session.getTransaction().rollback();
         //session.clear();
      }
      
      session.cancelQuery();
        createQuery = session.createQuery("from Language where id = -2");
      
      list = createQuery.list();
      language = list.get(0);
      
      try {
         session.beginTransaction();
         language.setName(language.getName() + "_A");
         session.save(language);
         //i'm getting the same error here again
         session.getTransaction().commit();
      } catch (Exception e) {
         e.printStackTrace();
         session.getTransaction().rollback();
      }


It outputs:
Quote:
org.hibernate.exception.ConstraintViolationException: could not update: [br.com.dyad.infrastructure.entity.Language#-1]


Top
 Profile  
 
 Post subject: Re: Problem with rollback
PostPosted: Tue Jan 26, 2010 3:23 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
If there is an error in a transaction you can no longer use the session. It must be rolled back, closed and discarded. See http://docs.jboss.org/hibernate/stable/ ... exceptions

If you need the two objects to be saved independently of each other you'll need two transactions and two sessions.


Top
 Profile  
 
 Post subject: Re: Problem with rollback
PostPosted: Tue Jan 26, 2010 8:15 am 
Newbie

Joined: Tue Jun 16, 2009 4:11 pm
Posts: 8
nordborg wrote:
If there is an error in a transaction you can no longer use the session. It must be rolled back, closed and discarded. See http://docs.jboss.org/hibernate/stable/ ... exceptions

If you need the two objects to be saved independently of each other you'll need two transactions and two sessions.


Thanks for the reply nordborg, is there any way to associate a object loaded with a session thats already closed to another session?


Top
 Profile  
 
 Post subject: Re: Problem with rollback
PostPosted: Tue Jan 26, 2010 9:09 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Yes, there are multiple ways. session.lock() (with LockMode.NONE), session.merge(), session.update(). Which one to use depends on the circumstances. For example, if the object has been updated or not. I guess the example code you posted is not very representative, since I don't see any object that was loaded before the rollback being used again in the second transaction.

By the way, in the example code there is no need to call session.save(language). Hibernate is able to automatically detect changes and update the database when needed.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.