-->
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.  [ 6 posts ] 
Author Message
 Post subject: Why get dirty data
PostPosted: Fri Dec 15, 2006 5:33 pm 
Newbie

Joined: Thu Nov 23, 2006 5:00 pm
Posts: 19
I have following code:

Code:
try
{
   
//   session.beginTransaction();
         
   CallableStatement st = session.connection().prepareCall("{call storedProcedure(?,?,?,?,?,?,?,?)}");  //insert one row into table
          ....//set the parameters         
            st.close();
            st = null;
            System.out.println("go to commit");
           session.getTransaction().commit();
           System.out.println("go to after commit");
}
catch (Exception e)
{
         System.out.println("Exception happens:"+e);
}
      
        session.flush();

        results = session.createCriteria(GroupAccount.class).add(Restrictions.and(Restrictions.eq("accountNo",accountNo), Restrictions.eq("groupID",new Integer(groupID)))).list();

        for (ListIterator iter = results.listIterator(); iter.hasNext(); ){
    GroupAccount aTrack = (GroupAccount)iter.next();
    System.out.println("groupID = "+aTrack.getGroupID()+", naObjID = "+aTrack.getNaObjID()+", accountNo = " +aTrack.getAccountNo());
      }
     session.close();


When I run, I got the following results:
[java] go to commit
[java] Exception happens:org.hibernate.TransactionException: Transaction not successfully started

However, the new row still be printed out. I checked the database, this row is not there.

What happened? what whould I do the make the consistence between the cache and database?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 6:09 pm 
Newbie

Joined: Thu Nov 23, 2006 5:00 pm
Posts: 19
I changed the code to following:
Code:
try
{
   
   session.beginTransaction();
         
   CallableStatement st = session.connection().prepareCall("{call storedProcedure(?,?,?,?,?,?,?,?)}");  //insert one row into table
          ....//set the parameters         
            st.close();
            st = null;
            if (0 == 0)
                throw new Exception();
             session.getTransaction().commit();
   
}
catch (Exception e)
{
         System.out.println("Exception happens:"+e);
         if (session.getTransaction() != null)
              session.getTransaction().rollback();
}
       
        session.flush();

        results = session.createCriteria(GroupAccount.class).add(Restrictions.and(Restrictions.eq("accountNo",accountNo), Restrictions.eq("groupID",new Integer(groupID)))).list();

        for (ListIterator iter = results.listIterator(); iter.hasNext(); ){
    GroupAccount aTrack = (GroupAccount)iter.next();
    System.out.println("groupID = "+aTrack.getGroupID()+", naObjID = "+aTrack.getNaObjID()+", accountNo = " +aTrack.getAccountNo());
      }
     session.close();


This time, even if I did not use session.flush(), the new row does not be inserted.
However, if I commented the rollback(), the new row can still be printout. In the database, there is no such row.


Top
 Profile  
 
 Post subject: Re: Why get dirty data
PostPosted: Fri Dec 15, 2006 6:31 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Well, if you comment the line that begins the transaction, it's quite understandable that the message is "transaction not successfully started"
zhb402 wrote:
Code:
try
{   
//   session.beginTransaction();
...


_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 6:58 pm 
Newbie

Joined: Thu Nov 23, 2006 5:00 pm
Posts: 19
Yes.
I just want to know why the dirty data are read.
I tested, if I close the sessionFactory, and open again, the data is correct (no dirty data).
I think this means the temporary data have been writen to database and when the sessionFactory is closed, the all unsuccessfully committed transactions are rollback automatically. Is this right?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 8:11 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
You mean the result of this printout ?
Quote:
Code:
System.out.println("groupID = "+aTrack.getGroupID()+", naObjID = "+aTrack.getNaObjID()+", accountNo = " +aTrack.getAccountNo());


If you have received an exception, trash your session and don't try and read anything from it.
Quote:
How do I handle exceptions?

Clearly, whenever an exception happens, the database transaction has to be rolled back. Another rule in Hibernate is that the current Session has to be closed and discarded immediately, it can't be re-used.

http://www.hibernate.org/43.html#A7

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 17, 2006 3:05 am 
Newbie

Joined: Thu Nov 23, 2006 5:00 pm
Posts: 19
Yes, I mean that printout, since that is gotten from database query and it is dirty.

I just want to do a test. I know there is an exception and the transaction is failed. However, I want to know whether the data is write temperarily into the database? and even the session is closed, the rollback still does not execute and it will do until sessionfactory is close.


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