details: https://code.openbravo.com/erp/devel/pi/rev/cdba0d010a46 changeset: 27915:cdba0d010a46 user: Carlos Aristu <carlos.aristu <at> openbravo.com> date: Wed Nov 18 12:57:36 2015 +0100 summary: fixes issue 30813: Show batch exception cause in SessionHandler commitAndClose
A new method, getCausingException(Throwable t), has been created in OBException to recover the underlying exception of a BatchUpdateException. This method is used in the commitAndClose() method of SessionHandler class to solve this issue. Together with this, this new method is now used when instantating OBException with this constructor: OBException(Throwable cause) in order to retrieve the underlying exception information in this case also. diffstat: src/org/openbravo/base/exception/OBException.java | 29 ++++++++++++++++++++++- src/org/openbravo/dal/core/SessionHandler.java | 3 +- 2 files changed, 30 insertions(+), 2 deletions(-) diffs (71 lines): diff -r 24120cc68db5 -r cdba0d010a46 src/org/openbravo/base/exception/OBException.java --- a/src/org/openbravo/base/exception/OBException.java Wed Nov 18 10:40:21 2015 +0100 +++ b/src/org/openbravo/base/exception/OBException.java Wed Nov 18 12:57:36 2015 +0100 @@ -19,6 +19,8 @@ package org.openbravo.base.exception; +import java.sql.BatchUpdateException; + import org.apache.log4j.Logger; /** @@ -64,7 +66,13 @@ public OBException(Throwable cause) { super(cause); - getLogger().error(cause.getMessage(), cause); + Throwable foundCause = getCausingException(cause); + if (foundCause != cause) { + // passing foundCause ensures that the underlying stack trace is printed + getLogger().error(cause.getMessage() + " - " + foundCause.getMessage(), foundCause); + } else { + getLogger().error(cause.getMessage(), cause); + } } /** @@ -85,4 +93,23 @@ public boolean isLogExceptionNeeded() { return logExceptionNeeded; } + + /** + * Hibernate and JDBC will wrap the exception thrown by triggers/constraints in another exception + * (the java.sql.BatchUpdateException) and this exception is sometimes wrapped again. Also the + * java.sql.BatchUpdateException stores the underlying exception in the nextException and not in + * the cause property. This method retrieves the original cause of the exception in this type of + * cases. + * + * @return a Throwable object with the causing exception + */ + public static Throwable getCausingException(Throwable t) { + if (t instanceof BatchUpdateException) { + return ((BatchUpdateException) t).getNextException(); + } else if (t.getCause() instanceof BatchUpdateException + && ((BatchUpdateException) t.getCause()).getNextException() != null) { + return ((BatchUpdateException) t.getCause()).getNextException(); + } + return t; + } } diff -r 24120cc68db5 -r cdba0d010a46 src/org/openbravo/dal/core/SessionHandler.java --- a/src/org/openbravo/dal/core/SessionHandler.java Wed Nov 18 10:40:21 2015 +0100 +++ b/src/org/openbravo/dal/core/SessionHandler.java Wed Nov 18 12:57:36 2015 +0100 @@ -29,6 +29,7 @@ import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; +import org.openbravo.base.exception.OBException; import org.openbravo.base.model.Entity; import org.openbravo.base.provider.OBNotSingleton; import org.openbravo.base.provider.OBProvider; @@ -272,7 +273,7 @@ tx = null; err = false; } catch (SQLException e) { - log.error("Error while closing the connection", e); + log.error("Error while closing the connection", OBException.getCausingException(e)); } finally { if (err) { try { ------------------------------------------------------------------------------ _______________________________________________ Openbravo-commits mailing list Openbravo-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbravo-commits