You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Angelo Chen <an...@yahoo.com.hk> on 2008/03/07 09:08:46 UTC
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("a101@mycom.com"); // email already used by other user
}
void onActionFromUpdate2() {
Usr usr = (Usr) session.get(Usr.class, new Long(2));
usr.setEmail("a0001@mycom.com"); // 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 'a101@mycom.com' 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 'a101@mycom.com'
for key 2
...
[WARN] PerthreadManager Error invoking listener
org.apache.tapestry.internal.hibernate.HibernateSessionManagerImpl@a73411:
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
org.apache.tapestry.internal.hibernate.HibernateSessionManagerImpl@5321a8:
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: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: T5: proper handling of hibernate exception?
Posted by Angelo Chen <an...@yahoo.com.hk>.
Hi Ted,
Thanks, I added flush then catch the exception, problem gone.
Ted Steen wrote:
>
> The problem is that the exceptions does not occur at the line
> usr.setEmail("a101@mycom.com"); // 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.
>
>
>
--
View this message in context: http://www.nabble.com/T5%3A-proper-handling-of-hibernate-exception--tp15891081p15896736.html
Sent from the Tapestry - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Re: T5: proper handling of hibernate exception?
Posted by Ted Steen <te...@gmail.com>.
The problem is that the exceptions does not occur at the line
usr.setEmail("a101@mycom.com"); // 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 <an...@siemens.com>:
> 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("a101@mycom.com"); // 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:angelochen960@yahoo.com.hk]
> 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("a101@mycom.com"); // email already used by other user
> }
>
> void onActionFromUpdate2() {
> Usr usr = (Usr) session.get(Usr.class, new Long(2));
> usr.setEmail("a0001@mycom.com"); // 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 'a101@mycom.com' 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 'a101@mycom.com'
> for key 2
> ...
> [WARN] PerthreadManager Error invoking listener
> org.apache.tapestry.internal.hibernate.HibernateSessionManagerImpl@a73411:
> 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
> org.apache.tapestry.internal.hibernate.HibernateSessionManagerImpl@5321a8:
> 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: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
--
/ted
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
AW: T5: proper handling of hibernate exception?
Posted by "Stroeber, Andreas" <an...@siemens.com>.
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("a101@mycom.com"); // 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:angelochen960@yahoo.com.hk]
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("a101@mycom.com"); // email already used by other user
}
void onActionFromUpdate2() {
Usr usr = (Usr) session.get(Usr.class, new Long(2));
usr.setEmail("a0001@mycom.com"); // 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 'a101@mycom.com' 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 'a101@mycom.com'
for key 2
...
[WARN] PerthreadManager Error invoking listener
org.apache.tapestry.internal.hibernate.HibernateSessionManagerImpl@a73411:
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
org.apache.tapestry.internal.hibernate.HibernateSessionManagerImpl@5321a8:
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: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org