The problem is that the exceptions does not occur at the line
        usr.setEmail("[EMAIL PROTECTED]"); // email already used by other user
the exception occurs when the session is flushed (at the end of the request)
so my suggestion would be to make an UsrService with a updateUsr(Usr
usr) method that is properly wrapped in a transaction. Then you have
better control when things happen and also, you can catch an exception
coming from the updateUsr(...) service method.

Personally I would also add a checkIfEmailExists(String email) and use
that in my updateUsr(Usr usr) and throw EmailAlreadyExistsException
when neccecary.


2008/3/7, Stroeber, Andreas <[EMAIL PROTECTED]>:
> I am wondering about this question here at a tapestry mailing list?!
>
>  But how about the following hints:
>
>  1. Catch your exceptions!
>
>  void onActionFromUpdate1() {
>    try {
>
>       Usr usr = (Usr) session.get(Usr.class, new Long(2));
>         usr.setEmail("[EMAIL PROTECTED]"); // email already used by other user
>
>    } catch (Exception ex) {
>       setMyPageErrorMessage("Opps. I did it again.");
>  }
>
>
>  2. Make sure to close your hibernate-connections properly (although 
> hibernate does this for you in some modes)
>
>         try {   .... }
>         catch { .... }
>         finally { session.close(); statement.close(); connection.close(); }
>
>
>  Grz
>  Andi
>
>
>  -----Ursprüngliche Nachricht-----
>  Von: Angelo Chen [mailto:[EMAIL PROTECTED]
>  Gesendet: Freitag, 7. März 2008 09:09
>  An: users@tapestry.apache.org
>  Betreff: T5: proper handling of hibernate exception?
>
>
>
>  I have a class where its email field has unique constraint, a page like this:
>
>   @Inject
>   private Session session;
>
>   void onActionFromUpdate1() {
>       Usr usr = (Usr) session.get(Usr.class, new Long(2));
>       usr.setEmail("[EMAIL PROTECTED]");   // email already used by other user
>   }
>
>   void onActionFromUpdate2() {
>       Usr usr = (Usr) session.get(Usr.class, new Long(2));
>       usr.setEmail("[EMAIL PROTECTED]");  // email available
>   }
>
>
>  if I click update2, which change the email address to a non existing one, 
> everything works.
>
>  if I do following:
>
>  click update 1, that will change email address to one already exists, 
> naturally I got this:
>
>  [WARN] JDBCExceptionReporter SQL Error: 1062, SQLState: 23000 [ERROR] 
> JDBCExceptionReporter Duplicate entry '[EMAIL PROTECTED]' for key 2 [ERROR] 
> AbstractFlushingEventListener Could not synchronize database state with 
> session
>  org.hibernate.exception.ConstraintViolationException: Could not execute JDBC 
> batch update ...
>  Caused by: java.sql.BatchUpdateException: Duplicate entry '[EMAIL PROTECTED]'
>  for key 2
>  ...
>  [WARN] PerthreadManager Error invoking listener
>  [EMAIL PROTECTED]:
>  Could not execute JDBC batch update
>  org.hibernate.exception.ConstraintViolationException: Could not execute JDBC 
> batch update
>
>  Click update 2, it will take a long time before the page got refreshed, 
> error as follow:
>
>  WARN] JDBCExceptionReporter SQL Error: 1205, SQLState: 41000 [ERROR] 
> JDBCExceptionReporter Lock wait timeout exceeded; try restarting transaction 
> [ERROR] AbstractFlushingEventListener 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.PreparedStatement.executeBatchSerially(PreparedStatement.java:1257)
>
>  [WARN] PerthreadManager Error invoking listener
>  [EMAIL PROTECTED]:
>  Could not execute JDBC batch update
>         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.PreparedStatement.executeBatchSerially(PreparedStatement.java:1257)
>
>  Any way to fix this
>
>
>  --
>  View this message in context: 
> http://www.nabble.com/T5%3A-proper-handling-of-hibernate-exception--tp15891081p15891081.html
>  Sent from the Tapestry - User mailing list archive at Nabble.com.
>
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: [EMAIL PROTECTED]
>  For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: [EMAIL PROTECTED]
>  For additional commands, e-mail: [EMAIL PROTECTED]
>
>


-- 
/ted

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to