From ab2af4a2066e104247954e1fce8ee341391d27b9 Mon Sep 17 00:00:00 2001 From: teo_sarca Date: Thu, 5 Jun 2008 18:07:07 +0000 Subject: [PATCH] FR [ 1984794 ] DBException improvements --- base/src/org/compiere/db/DB_Oracle.java | 4 +- base/src/org/compiere/model/GridTable.java | 15 ++-- base/src/org/compiere/util/DBException.java | 94 ++++++++++++++++++++- 3 files changed, 100 insertions(+), 13 deletions(-) diff --git a/base/src/org/compiere/db/DB_Oracle.java b/base/src/org/compiere/db/DB_Oracle.java index 3481f2f390..ad45d2ebab 100644 --- a/base/src/org/compiere/db/DB_Oracle.java +++ b/base/src/org/compiere/db/DB_Oracle.java @@ -36,6 +36,7 @@ import org.compiere.dbPort.Convert; import org.compiere.dbPort.Convert_Oracle; import org.compiere.util.CLogger; import org.compiere.util.DB; +import org.compiere.util.DBException; import org.compiere.util.DisplayType; import org.compiere.util.Ini; import org.compiere.util.Language; @@ -636,8 +637,7 @@ public class DB_Oracle implements AdempiereDatabase { exception = e; conn = null; - if (e instanceof SQLException - && ((SQLException)e).getErrorCode() == 1017) // invalid username/password + if (DBException.isInvalidUserPassError(e)) { //log might cause infinite loop since it will try to acquire database connection again /* diff --git a/base/src/org/compiere/model/GridTable.java b/base/src/org/compiere/model/GridTable.java index 7b992c119d..9c896baf31 100644 --- a/base/src/org/compiere/model/GridTable.java +++ b/base/src/org/compiere/model/GridTable.java @@ -1096,7 +1096,7 @@ public class GridTable extends AbstractTableModel m_rowChanged = m_newRow; else { - fireDataStatusEEvent("SaveErrorNoChange", "", false); + fireDataStatusEEvent("SaveErrorNoChange", "", true); return SAVE_ERROR; } } @@ -1671,11 +1671,11 @@ public class GridTable extends AbstractTableModel log.log(Level.SEVERE, "Inserted row not found"); // } - catch (SQLException e) + catch (Exception e) { String msg = "SaveError"; - if (e.getErrorCode() == 1) // Unique Constraint + if (DBException.isUniqueContraintError(e)) // Unique Constraint { log.log(Level.SEVERE, "Key Not Unique", e); msg = "SaveErrorNotUnique"; @@ -1802,10 +1802,7 @@ public class GridTable extends AbstractTableModel msg = ppE.getValue(); info = ppE.getName(); // Unique Constraint - Exception ex = CLogger.retrieveException(); - if (ex != null - && ex instanceof SQLException - && ((SQLException)ex).getErrorCode() == 1) + if (DBException.isUniqueContraintError(CLogger.retrieveException())) msg = "SaveErrorNotUnique"; } fireDataStatusEEvent(msg, info, true); @@ -2281,7 +2278,7 @@ public class GridTable extends AbstractTableModel { log.log(Level.SEVERE, sql.toString(), e); String msg = "DeleteError"; - if (e.getErrorCode() == 2292) // Child Record Found + if (DBException.isChildRecordFoundError(e)) msg = "DeleteErrorDependent"; fireDataStatusEEvent(msg, e.getLocalizedMessage(), true); return false; @@ -2924,7 +2921,7 @@ public class GridTable extends AbstractTableModel catch (SQLException e0) { // Zoom Query may have invalid where clause - if (e0.getErrorCode() == 904) // ORA-00904: "C_x_ID": invalid identifier + if (DBException.isInvalidIdentifierError(e0)) log.warning("Count - " + e0.getLocalizedMessage() + "\nSQL=" + m_SQL_Count); else log.log(Level.SEVERE, "Count SQL=" + m_SQL_Count, e0); diff --git a/base/src/org/compiere/util/DBException.java b/base/src/org/compiere/util/DBException.java index 8abe24ab53..c50c7a84b0 100644 --- a/base/src/org/compiere/util/DBException.java +++ b/base/src/org/compiere/util/DBException.java @@ -16,15 +16,22 @@ *****************************************************************************/ package org.compiere.util; +import java.sql.SQLException; + +import org.adempiere.exceptions.AdempiereException; + /** * This RuntimeException is used to pass SQLException up the chain of calling * methods to determine what to do where needed. * * @author Vincent Harcq * @version $Id: DBException.java,v 1.2 2006/07/30 00:54:35 jjanke Exp $ + * + * @author Teo Sarca, SC ARHIPAC SERVICE SRL */ -public class DBException extends RuntimeException { - +public class DBException extends AdempiereException { + private static final long serialVersionUID = 1L; + /** * Create a new DBException based on a SQLException * @param e Specicy the Exception cause @@ -42,5 +49,88 @@ public class DBException extends RuntimeException { { super(msg); } // DBException + + /** + * @return Wrapped SQLException or null + */ + public SQLException getSQLException() { + Throwable cause = getCause(); + if (cause instanceof SQLException) + return (SQLException)cause; + return null; + } + /** + * @see java.sql.SQLException#getErrorCode() + */ + public int getErrorCode() { + SQLException e = getSQLException(); + return (e != null ? e.getErrorCode() : -1); + } + + /** + * @see java.sql.SQLException#getNextException() + */ + public SQLException getNextException() { + SQLException e = getSQLException(); + return (e != null ? e.getNextException() : null); + } + + /** + * @see java.sql.SQLException#getSQLState() + */ + public String getSQLState() { + SQLException e = getSQLException(); + return (e != null ? e.getSQLState() : null); + } + + + private static final boolean isErrorCode(Exception e, int errorCode) { + if (e == null) { + return false; + } + else if (e instanceof SQLException) { + return ((SQLException)e).getErrorCode() == errorCode; + } + else if (e instanceof DBException) { + SQLException sqlEx = ((DBException)e).getSQLException(); + if (sqlEx != null) + return sqlEx.getErrorCode() == errorCode; + else + return false; + } + return false; + } + + /** + * Check if Unique Constraint Exception (aka ORA-00001) + * @param e exception + */ + public static boolean isUniqueContraintError(Exception e) { + return isErrorCode(e, 1); + } + + /** + * Check if "child record found" exception (aka ORA-02292) + * @param e exception + */ + public static boolean isChildRecordFoundError(Exception e) { + return isErrorCode(e, 2292); + } + + /** + * Check if "invalid identifier" exception (aka ORA-00904) + * @param e exception + */ + public static boolean isInvalidIdentifierError(Exception e) { + return isErrorCode(e, 904); + } + + /** + * Check if "invalid username/password" exception (aka ORA-01017) + * @param e exception + */ + public static boolean isInvalidUserPassError(Exception e) { + return isErrorCode(e, 1017); + } } // DBException