Hello,
In my dao layer, I have two methods which load and update records in a user table. The user table is mapped one to one, and lazy loading is set to false. In these methods, I am using Spring's HibernateTemplate.
public User loadUserByUsername( String username )
{
List results = getHibernateTemplate().find("from User user where user.username= ?", username);
return (User) DataAccessUtils.uniqueResult( results );
}
public void updateUser( User user)
{
getHibernateTemplate().update(user);
}
I am calling both these methods from a service layer to retrieve a user by username, modify it, then update the record. Basically, something like this:
User user = getUserDao().loadUserByUsername(username);
user.setEmail("test@test.com");
getUserDao().updateUser(user);
First I did not execute these methods inside a transaction, and I saw the following stack trace:
11 Mar 2008 12:42:29,171 [http-8050-Processor25, ] ERROR org.hibernate.event.def.AbstractFlushingEventListe ner - Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
...
Caused by: java.sql.BatchUpdateException: Lock wait timeout exceeded; try restarting transaction
at com.mysql.jdbc.ServerPreparedStatement.executeBatc h(ServerPreparedStatement.java:647)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch( BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(Ab stractBatcher.java:195)
Once I put these calls inside a transaction, it works fine. What I would like to know is why I need the transaction for this update to work? I am using mysql 5
Thanks.
|