-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Simple update is failing ....
PostPosted: Tue May 10, 2005 3:53 pm 
Regular
Regular

Joined: Fri Mar 04, 2005 1:33 pm
Posts: 65
Location: U.K.
Hi All

I am using Hibernate 2.1. I get HibernateException with no details and hence I am stuck:

Session session = HibernateUtil.getSession();
HibernateUtil.beginTransaction();

// Save Active users
Iterator iterOne = activeUsers.iterator();
while (iterOne.hasNext()){
String userId = (String) iterOne.next();
User user = (User) findByPK(User.class, new Long(userId));
user.setRowStatusId(RowStatusCodes.ACTIVE);
session.saveOrUpdate(user);
}

HibernateUtil.commitTransaction();
}
catch (HibernateException he) {
String message = "Update of the User object failed.";
logger.info(message);
HibernateUtil.rollbackTransaction();
throw new DAOException(message, he);
}
finally {
HibernateUtil.closeSession();
}
--------------------
INFO: CORE3282: stdout: 2491211 [Thread-7] DEBUG ...util.HibernateUtil - Commiting transaction.
INFO: CORE3282: stdout: 2491211 [Thread-7] DEBUG ....util.HibernateUtil - Closing session.
INFO: CORE3282: stdout: 2491211 [Thread-7] DEBUG ...util.HibernateUtil - CommitTransaction called but no transaction was in progress.


Can someonelet me know why am I getting exception ?

Thanks
Ron


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 10, 2005 4:00 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
What is the HibernateException that you are getting?

Could you post the stack trace, or do a Exception.getCause().

Just in case: HibernateUtil.beginTransaction creates a transaction from the same session that you are using in your test, or is it possible that the beginTransaction and the commitTransaction are made on another instant of Session?

Please post the code of your hibernateUtil as well.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject: Simple Update is failing ...
PostPosted: Tue May 10, 2005 4:17 pm 
Regular
Regular

Joined: Fri Mar 04, 2005 1:33 pm
Posts: 65
Location: U.K.
The cause is empty ...

Here is the code from HibernateUtil:

public static Session getSession() throws DAOException {
Session s = (Session) threadSession.get();
// Open a new Session, if this thread has none yet
try {
if (s == null) {
s = sessionFactory.openSession();
s.setFlushMode( FlushMode.COMMIT );
threadSession.set(s);
log.debug( "Fetched session." );
} else {
log.debug( "Fetched pre-existing session." );
}
} catch (HibernateException ex) {
log.error("getSession", ex );
ex.printStackTrace();
throw new DAOException(ex);
}
return s;
}


public static void beginTransaction() throws DAOException {
Transaction tx = (Transaction) threadTransaction.get();

try {
if (tx == null) {
tx = getSession().beginTransaction();
threadTransaction.set(tx);
log.debug( "Starting new transaction." );
} else {
log.debug( "Joining transaction in progress." );
}
} catch (HibernateException ex) {
log.error( "beginTransaction", ex );
ex.printStackTrace();
throw new DAOException(ex);
}
}

public static void commitTransaction() throws DAOException {
Transaction tx = (Transaction) threadTransaction.get();
try {
if ( tx != null && !tx.wasCommitted()
&& !tx.wasRolledBack() ){
tx.commit();
log.debug( "Commiting transaction." );
} else {
log.debug( "CommitTransaction called but no transaction was in progress." );
}
threadTransaction.set(null);
} catch (HibernateException ex) {
log.error( "commitTransaction", ex );
ex.printStackTrace();
rollbackTransaction();
throw new DAOException(ex);
}
}


-- Please let me know if you find anything wrong.

Thanks
Ron


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 10, 2005 4:37 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
I am not seeing any error being thrown. Your message log only indicates that you are calling commitTransaction one too many time.

Are you executing this code from a JUnit test?
if yes, maybe your tearDown method is calling commitTransaction().


Your only problem comes from the fact that you see the "CommitTransaction called but no transaction was in progress." or is there something else you have not posted?

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 9:54 am 
Regular
Regular

Joined: Fri Mar 04, 2005 1:33 pm
Posts: 65
Location: U.K.
The database table rows are not updated .... to the value I am setting.

I think this is some bug in Hibernate 2.1. I am getting a session with flush mode as commit, fetching an object Users by its primary key, setting one column to a specific value, calling session.saveOrUpdate() in a while loop. Once loop is over, I commitTransaction, close session.

Thanks
Ron


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 10:29 am 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Just to make sure that your code is actually doing what you think it is doing:

Could you put the following information on your log

The SQL of the actions executed
The hashcode of the session on which you start the transaction
The hashcode of the transaction
The hashcode of the session from which you read
The hashcode of the session to which you write
The hashcode of the transaction that you commit

I have had a problem like that before. I was not writing to the session on which I was having a transaction.

Regards,

Vincent.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 11:25 am 
Regular
Regular

Joined: Fri Mar 04, 2005 1:33 pm
Posts: 65
Location: U.K.
You are correct. I was not fecthing and updating in the same session. Although I wonder why would it matter ? if all the sessions are referring to the same first level Session cache. Apparently they are not ....


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 11:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Think about the phrase "all the sessions are referring to the same first level Session cache"...


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 11:53 am 
Regular
Regular

Joined: Fri Mar 04, 2005 1:33 pm
Posts: 65
Location: U.K.
After referring to page 180 of 'Hibernate In Action', I guess each session has first level cache but that does not mean all the sessions are referring to same first level cache.

If I fetch in sessionOne and write to sessionTwo, will
sessionTwo.saveOrUpdateCopy() work ?

Thanks
Ron


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 12:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
No I mean simply the semantic inherent in your statement, itself.

You clearly recognize it is a "session cache", then somehow think different sessions would share this; wouldn't that then make it something other than a session cache?

Quote:
will sessionTwo.saveOrUpdateCopy() work ?

Sure, because updating from the session perspective is the act of re-associating a detached entity with a session.

You may not even need the copy portion. Depending on what you do with sessionTwo, you may only need sessionTwo.saveOrUpdate().


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 12:52 pm 
Regular
Regular

Joined: Fri Mar 04, 2005 1:33 pm
Posts: 65
Location: U.K.
Yesterday I spent all day when I was fetching in sessionOne and writing to sessionTwo. It did not work. I tried both saveOrUpdate() as well as saveOrUpdateCopy().

The result was it was not saving the objects in the database except one error message in the log:

Session session = HibernateUtil.getSession();
HibernateUtil.beginTransaction();

// Save Active users
Iterator iterOne = activeUsers.iterator();
while (iterOne.hasNext()){
String userId = (String) iterOne.next();
// findByPK fetches in sessionOne
User user = (User) findByPK(User.class, new Long(userId));
user.setRowStatusId(RowStatusCodes.ACTIVE);
// following session is sessionTwo
session.saveOrUpdate(user);
// also tries session.saveOrUpdateCopy(user);
}

HibernateUtil.commitTransaction();
}
catch (HibernateException he) {
String message = "Update of the User object failed.";
logger.info(message);
HibernateUtil.rollbackTransaction();
throw new DAOException(message, he);
}
finally {
HibernateUtil.closeSession();
}
--------------------
INFO: CORE3282: stdout: 2491211 [Thread-7] DEBUG ...util.HibernateUtil - Commiting transaction.
INFO: CORE3282: stdout: 2491211 [Thread-7] DEBUG ....util.HibernateUtil - Closing session.
INFO: CORE3282: stdout: 2491211 [Thread-7] DEBUG ...util.HibernateUtil - CommitTransaction called but no transaction was in progress.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 1:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
except one error message in the log

The error might be helpful ;)

Is findByPK() also using this HibernateUtil class? If so, I need to see it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 1:25 pm 
Regular
Regular

Joined: Fri Mar 04, 2005 1:33 pm
Posts: 65
Location: U.K.
The error message was one liner:
INFO: CORE3282: stdout: 2491211 [Thread-7] DEBUG ...util.HibernateUtil - CommitTransaction called but no transaction was in progress.

One another user replied this message is coming from JUnit test's tearDown method().

The findByPK() code is as follows:
---
public Object findByPK( Class clazz, Serializable key ) throws DAOException, InvalidDataException {
Object result = null;

if ( null == key ) {
throw new InvalidDataException( "Attempted a find operation with a null key");
}

try {
Session session = HibernateUtil.getSession();
HibernateUtil.beginTransaction();

result = session.get( clazz, key );

HibernateUtil.commitTransaction();

} catch ( HibernateException he ) {
String message = "Failed to find [" + clazz.getName() + "] by primary key";
log.error( message, he );
HibernateUtil.rollbackTransaction();
throw new DAOException( message , he );
} finally {
HibernateUtil.closeSession();
}

return result;
}
---
The HubernateUtil code can be found in previous posts of this chain.

Thanks for your help,
Ron


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 1:34 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
I don;t mind helping, but if I need to look through reams of user code I sure would like to be able to look at it without everything left-justified and non-indented (hint: use the code tags).


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 11, 2005 1:52 pm 
Regular
Regular

Joined: Fri Mar 04, 2005 1:33 pm
Posts: 65
Location: U.K.
Now I am using code tags .. hopefully it will keep the indentation ....
Code:
    public Object findByPK( Class clazz, Serializable key ) throws DAOException, InvalidDataException {
   Object result = null;

   if ( null == key ) {
       throw new InvalidDataException( "Attempted a find operation with a null key");
   }

   try {
       Session session = HibernateUtil.getSession();
       HibernateUtil.beginTransaction();

       result = session.get( clazz, key );

       HibernateUtil.commitTransaction();

   } catch ( HibernateException he ) {
       String message = "Failed to find [" + clazz.getName() + "] by primary key";
       log.error( message, he  );
       HibernateUtil.rollbackTransaction();
       throw new DAOException( message , he );
   } finally {
       HibernateUtil.closeSession();
   }

   return result;
    }

------------------------------------
HibernateUtil code is as follows:
------------------------------------
Code:
    public static Session getSession() throws DAOException {
   Session s = (Session) threadSession.get();
   // Open a new Session, if this thread has none yet
   try {
       if (s == null) {
      s = sessionFactory.openSession();
      s.setFlushMode( FlushMode.COMMIT );
      threadSession.set(s);
      log.debug( "Fetched session." );
       } else {
      log.debug( "Fetched pre-existing session." );
       }
   } catch (HibernateException ex) {
       log.error("getSession", ex );
       ex.printStackTrace();
       throw new DAOException(ex);
   }
   return s;
    }


    public static void closeSession() throws DAOException {
   try {
       Session s = (Session) threadSession.get();
       threadSession.set(null);
       if (s != null && s.isOpen()) {
      log.debug( "Closing session." );
      s.flush();
      s.close();
       }
   } catch (HibernateException ex) {
       log.error( "closeSession", ex );
       ex.printStackTrace();
       throw new DAOException(ex);
   }
    }


    public static void beginTransaction() throws DAOException {
   Transaction tx = (Transaction) threadTransaction.get();
   
   try {
       if (tx == null) {
      tx = getSession().beginTransaction();
      threadTransaction.set(tx);
      log.debug( "Starting new transaction." );      
       } else {
      log.debug( "Joining transaction in progress." );      
       }
   } catch (HibernateException ex) {
       log.error( "beginTransaction", ex );
       ex.printStackTrace();
       throw new DAOException(ex);
   }
    }


    public static void commitTransaction() throws DAOException {
   Transaction tx = (Transaction) threadTransaction.get();
   try {
       if ( tx != null && !tx.wasCommitted()
       && !tx.wasRolledBack() ){
      tx.commit();
      log.debug( "Commiting transaction." );      
       } else {
      log.debug( "CommitTransaction called but no transaction was in progress." );
       }
       threadTransaction.set(null);
   } catch (HibernateException ex) {
       log.error( "commitTransaction", ex );
       ex.printStackTrace();
       rollbackTransaction();
       throw new DAOException(ex);
   }
    }




Thanks
Ron


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.