-->
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: Problems with rollback!!!!
PostPosted: Thu Mar 15, 2012 1:53 pm 
Newbie

Joined: Thu Feb 17, 2011 5:08 pm
Posts: 6
Hi guys!
I have some problems here!!

My application is multiuser and all of them share the same hibernate session (for memory reasons)
I process some objects in batch process (inserts and updates, some of them with merge) in the same transaction.
It's very strange but....

The problem is when, for example, i try to insert a new row in any table (really master/detail table and update another row in other table) and it throws a constraint exception. I catch this exception, I rollback current transaction and show the message. I can't clear the session because the users share the same session!!!!!

Then... i try to execute a simple update but the application try to execute all again!!! not only the update sentences!!! Of course it throws the previous constraint exception again!!!

I don't want this happen again. I want that it executes only te update sentenses ignoring previous inserts and updates...

I thought this would be done automatically WITH ROLLBACK, but no, it does not.

here my method

Code:
public boolean saveOrUpdateAndDelete(List<TransactionalOperation> list) {
        boolean result = false;
        org.hibernate.Transaction tx = null;

        try {
            tx = session.beginTransaction();
            System.out.println("Se abrio la transaccion");
            int i;
            for (i = 0; i < list.size(); i++) {
                //  System.out.println("Es " + list.get(i).getClass());
                if (list.get(i).getEntity() != null) {
                    if (!list.get(i).executeOperation()) {

// executeOperation execute update, insert or detele
                        System.err.println("aca lanzo el error");
                        throw new HibernateException(errorMsg);
                    }
                }
            }
            tx.commit();
            System.out.println("se cerro la transaccion");
            result = true;
        } catch (ConstraintViolationException e) {
            tx.rollback();
            System.out.println("Error de Constraint " + e.getSQLException().getNextException().getMessage());
            errorMsg = "ViolaciĆ³n de integridad de datos en " + this.getConstraintTable(e.getSQLException().getNextException().getMessage());
            System.out.println("rollback");

        } catch (HibernateException ex) {
            if (ex.getCause() != null && ex.getCause() instanceof ConstraintViolationException) {
                errorMsg = "ViolaciĆ³n de integridad de datos.";
            } else {
                errorMsg = ex.getMessage();
            }
            tx.rollback();
            System.out.println("rollback global");
        }
        return result;
    }


Thanks

Nicolas


Top
 Profile  
 
 Post subject: Re: Problems with rollback!!!!
PostPosted: Thu Mar 15, 2012 5:07 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
My application is multiuser and all of them share the same hibernate session (for memory reasons)


You can't do that. A session is not thread-safe and can't be used by multiple users at the same time. A session is designed to be short-lived and you will not save any memory by trying to keep on to a session for too long. It is more likely that it will use more memory than multiple short-lived sessions. If you worry about performance you should look into lazy loading, second-level cache, etc.

Quote:
...and it throws a constraint exception...


From the Hibernate documentation (at http://docs.jboss.org/hibernate/core/3. ... exceptions):

Quote:
If the Session throws an exception, including any SQLException, immediately rollback the database transaction, call Session.close() and discard the Session instance. Certain methods of Session will not leave the session in a consistent state. No exception thrown by Hibernate can be treated as recoverable.


In other words, I think you need to redesign your application. I would recommend buying a book about Hibernate (for example, the one linked on top of this forum).


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.