title: JPA: PersistenceException, could not update
body: <p>I'm using Play Framework 1.2.5 with Java but this is more of a JPA question.</p>
  
  <p>In my project I often search users by their email, so I created the following method:</p>
  
  <pre><code>public static User getUserByEmail(String email) {
      User user = User.find("email = ?", email).first();
      return user;
  }
  </code></pre>
  
  <p>and I call it like this from various methods:</p>
  
  <pre><code>User user = User.getUserByEmail("test@email.com");
  </code></pre>
  
  <p>When I'm trying to modify a field of User like this:</p>
  
  <pre><code>User user = User.getUserByEmail("kospol@test.com");
  user.name = "kospol";
  user.save();
  </code></pre>
  
  <p>I often get a the following exception resulting in total freeze:</p>
  
  <pre><code>Execution exception (In /app/controllers/*******.java around line 46)
  PersistenceException occured : update User set activated=?, ... registered=?,      registeredFrom=?, version=? where id=? and version=?
  
  
  play.exceptions.JavaExecutionException: update User set activated=?, ... registered=?,     registeredFrom=?, version=? where id=? and version=?
      at play.mvc.ActionInvoker.invoke(ActionInvoker.java:237)
      at Invocation.HTTP Request(Play!)
  Caused by: javax.persistence.PersistenceException: update User set activated=?, ... registered=?, registeredFrom=?, version=? where id=? and version=?
      at play.db.jpa.JPABase._save(JPABase.java:44)
      at play.db.jpa.GenericModel.save(GenericModel.java:204)
      at controllers.PushService.register(PushService.java:46)
      at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557)
      at play.mvc.ActionInvoker.invoke(ActionInvoker.java:508)
      at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484)
      at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479)
      at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)
      ... 1 more
  Caused by: javax.persistence.PersistenceException:  org.hibernate.exception.GenericJDBCException: could not update: [models.User#3606]
      at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1389)
      at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1317)
      at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1323)
      at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:965)
      at play.db.jpa.JPABase._save(JPABase.java:41)
      ... 8 more
  Caused by: org.hibernate.exception.GenericJDBCException: could not update: [models.User#3606]
      at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
      at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2613)
      at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2495)
      at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2822)
      at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:113)
      at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
      at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
      at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:345)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
      at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:962)
      ... 9 more
  Caused by: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
      at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
      at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
      at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2683)
      at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2144)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2444)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2347)
      at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2595)
      ... 19 more
  </code></pre>
  
  <p>I have added the @Version annotation to the model. It seems that the problem is the <strong>Lock wait timeout exceeded</strong>. How can I avoid this?</p>
  
  <p>I have tried to use the .merge() method with no results and I'm thinking to remove the getUserByEmail method and get the model straight.</p>