-->
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: other problem??
PostPosted: Fri Sep 30, 2005 2:03 am 
I will session.saveorupdate(entity);

because date is wrong, so rollback(), but entity.id is have a new id.

again session.saveorupdate(entity) throw exception "row is delete or update......."

example:
Code:
    //add new data
     Entity  entity = new Entity();
     entity.name = "test";

    try
    {
          // data wrong,throw exception, this is right.
           session.saveorupdate(entity);
     }
     catch
     {
           //this data is right. should is save to dataBase, but throw exception
           entity.name = "newtest";
           session.saveorupdate(entity);
      }


Top
  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 3:41 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Do not use a session after there was an exception. Rollback the transaction and close it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 4:06 am 
NO,NO,NO..

actually is Entity.id(OID) by assign, but after rollback(),entity can't clear.


Top
  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 12:27 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Here is how sessions should be used:
Code:
ISession sess = factory.OpenSession();
ITransaction tx;
try {
   tx = sess.BeginTransaction();
   //do some work
   ...
   tx.Commit();
} catch (Exception e) {
   if (tx != null) tx.Rollback();
   throw;
} finally {
   sess.Close();
   sess = null; // This is to stress that you shouldn't use it after
}


The transaction is optionnal.

In your case:
Code:
    //add new data
     Entity  entity = new Entity();
     entity.name = "test";

    bool Failed = false;
    try
    {
        // data wrong,throw exception, this is right.
        session.saveorupdate(entity);
    }
    catch
    {
        Failed = true;
    }
    finally {
   session.Close();
    }

    if(Failed)
        try
        {
            session = sessionFactory.OpenSession();
            // this data is right. should get saved in the dataBase
            entity.name = "newtest";
            session.saveorupdate(entity);
        }
        catch
        {
            Console("Wow, It fails again!"); // handle exception...
        }
        finally {
       session.Close();
        }

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 12:32 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Here is how sessions should be used:
Code:
ISession sess = factory.OpenSession();
ITransaction tx;
try {
   tx = sess.BeginTransaction();
   //do some work
   ...
   tx.Commit();
} catch (Exception e) {
   if (tx != null) tx.Rollback();
   throw;
} finally {
   sess.Close();
   sess = null; // This is to stress that you shouldn't use it after
}


The transaction is optionnal.

In your case:
Code:
    //add new data
     Entity  entity = new Entity();
     entity.name = "test";

    bool Failed = false;
    try
    {
        // data wrong,throw exception, this is right.
        session.saveorupdate(entity);
    }
    catch
    {
        Failed = true;
    }
    finally {
   session.Close();
    }

    if(Failed)
        try
        {
            session = sessionFactory.OpenSession();
            // this data is right. should get saved in the dataBase
            entity.name = "newtest";
            session.saveorupdate(entity);
        }
        catch
        {
            Console("Wow, It fails again!"); // handle exception...
        }
        finally {
       session.Close();
        }

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


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.