-->
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: Retry Update() on session using transactions
PostPosted: Tue Dec 27, 2005 1:31 pm 
Newbie

Joined: Sun Sep 25, 2005 9:11 am
Posts: 13
Location: Bergen, Norway
I'm trying to re-update an object after a transaction fails, but when I try to call Update() the second time, the new data is not used in the query.

Is this a bug, or is its just not possible to this?

Code:
using (ISession session = OpenSession())
            {
                using (ITransaction tx = session.BeginTransaction())
                {
                    Customer customer = (Customer)session.Load(typeof(Customer), 16, LockMode.UpgradeNoWait);
                    customer.Company = "verylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylongverylong";

                    try
                    {
                        session.Update(customer);
                        tx.Commit(); // throws exception, because Company is varchar(255)
                    }
                    catch
                    {
                        tx.Rollback();

                        customer.Company = "test";
                        using (ITransaction tx2 = session.BeginTransaction())
                        {
                            session.Update(customer);
                            tx2.Commit(); // The same query as before is executed, with the long value of "Company". Ofcourse, this throws the same exception as above.
                        }
                    }
                }
            }


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 27, 2005 2:02 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You can not re-use a Session if an exception has been thrown. What you are doing is wrong, don't throw invalid data onto your database to see what sticks. Validate in the application first.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 27, 2005 2:52 pm 
Newbie

Joined: Sun Sep 25, 2005 9:11 am
Posts: 13
Location: Bergen, Norway
I know its wrong to push invalid data to the database, but I just did it in the example to illustrate a case that would cause an exception. :)

But, there is several things that could cause an transaction to fail. So basically, I should use a new session if the transaction fails?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 27, 2005 4:06 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
That's what the Hibernate documentation says. It should also be in the NHibernate documentation.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 27, 2005 4:28 pm 
Newbie

Joined: Sun Sep 25, 2005 9:11 am
Posts: 13
Location: Bergen, Norway
Sorry, I should have read the documentation better. :)

Anyway, thanks!


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.