From 7a4012db51ef73a1c958398ccb1e4c28c878ba59 Mon Sep 17 00:00:00 2001 From: Heng Sin Low Date: Sun, 11 Feb 2007 16:46:43 +0000 Subject: [PATCH] * [ 1656849 ] WAN: every time when a query fails is tried to run locally --- .../org/compiere/util/CPreparedStatement.java | 65 +++++++++++++++---- dbPort/src/org/compiere/util/CStatement.java | 61 +++++++++++++---- dbPort/src/org/compiere/util/DB.java | 36 ++++++---- 3 files changed, 125 insertions(+), 37 deletions(-) diff --git a/dbPort/src/org/compiere/util/CPreparedStatement.java b/dbPort/src/org/compiere/util/CPreparedStatement.java index 2511a75f39..decc2a89ee 100644 --- a/dbPort/src/org/compiere/util/CPreparedStatement.java +++ b/dbPort/src/org/compiere/util/CPreparedStatement.java @@ -139,13 +139,26 @@ public class CPreparedStatement extends CStatement implements PreparedStatement { log.log(Level.SEVERE, "AppsServer error", ex); p_remoteErrors++; + if (ex instanceof SQLException) + throw (SQLException)ex; + else if (ex instanceof RuntimeException) + throw (RuntimeException)ex; + else + throw new RuntimeException(ex); } // Try locally - log.warning("Execute locally"); - p_stmt = local_getPreparedStatement (false, null); // shared connection - p_vo.clearParameters(); // re-use of result set - ResultSet rs = ((PreparedStatement)p_stmt).executeQuery(); - return rs; + if (!CConnection.get().isRMIoverHTTP()) + { + log.warning("Execute locally"); + p_stmt = local_getPreparedStatement (false, null); // shared connection + p_vo.clearParameters(); // re-use of result set + ResultSet rs = ((PreparedStatement)p_stmt).executeQuery(); + return rs; + } + else + { + throw new IllegalStateException("WAN - Application server not available"); + } } // executeQuery /** @@ -193,15 +206,28 @@ public class CPreparedStatement extends CStatement implements PreparedStatement log.log(Level.SEVERE, "AppsServer not found"); } } - catch (RemoteException ex) + catch (Exception ex) { log.log(Level.SEVERE, "AppsServer error", ex); + if (ex instanceof SQLException) + throw (SQLException)ex; + else if (ex instanceof RuntimeException) + throw (RuntimeException)ex; + else + throw new RuntimeException(ex); } // Try locally - log.warning("execute locally"); - p_stmt = local_getPreparedStatement (false, null); // shared connection - p_vo.clearParameters(); // re-use of result set - return ((PreparedStatement)p_stmt).executeUpdate(); + if (!CConnection.get().isRMIoverHTTP()) + { + log.warning("execute locally"); + p_stmt = local_getPreparedStatement (false, null); // shared connection + p_vo.clearParameters(); // re-use of result set + return ((PreparedStatement)p_stmt).executeUpdate(); + } + else + { + throw new IllegalStateException("WAN - Application server not available"); + } } // executeUpdate /** @@ -877,12 +903,23 @@ public class CPreparedStatement extends CStatement implements PreparedStatement { log.log(Level.SEVERE, "AppsServer error", ex); p_remoteErrors++; + if (ex instanceof RuntimeException) + throw (RuntimeException)ex; + else + throw new RuntimeException(ex); } // Try locally - log.warning("Execute locally"); - p_stmt = local_getPreparedStatement (false, null); // shared connection - p_vo.clearParameters(); // re-use of result set - return local_getRowSet(); + if (!CConnection.get().isRMIoverHTTP()) + { + log.warning("Execute locally"); + p_stmt = local_getPreparedStatement (false, null); // shared connection + p_vo.clearParameters(); // re-use of result set + return local_getRowSet(); + } + else + { + throw new IllegalStateException("WAN - Application server not available"); + } } /** diff --git a/dbPort/src/org/compiere/util/CStatement.java b/dbPort/src/org/compiere/util/CStatement.java index 52135348ca..0d6174fc2c 100644 --- a/dbPort/src/org/compiere/util/CStatement.java +++ b/dbPort/src/org/compiere/util/CStatement.java @@ -150,15 +150,28 @@ public class CStatement implements Statement p_remoteErrors++; } } - catch (RemoteException ex) + catch (Exception ex) { log.log(Level.SEVERE, "AppsServer error", ex); p_remoteErrors++; + if (ex instanceof SQLException) + throw (SQLException)ex; + else if (ex instanceof RuntimeException) + throw (RuntimeException)ex; + else + throw new RuntimeException(ex); } // Try locally - log.warning("execute locally"); - p_stmt = local_getStatement (false, null); // shared connection - return p_stmt.executeQuery(p_vo.getSql()); + if (!CConnection.get().isRMIoverHTTP()) + { + log.warning("execute locally"); + p_stmt = local_getStatement (false, null); // shared connection + return p_stmt.executeQuery(p_vo.getSql()); + } + else + { + throw new IllegalStateException("WAN - Application server not available"); + } } // executeQuery @@ -196,15 +209,28 @@ public class CStatement implements Statement p_remoteErrors++; } } - catch (RemoteException ex) + catch (Exception ex) { log.log(Level.SEVERE, "AppsServer error", ex); p_remoteErrors++; + if (ex instanceof SQLException) + throw (SQLException)ex; + else if (ex instanceof RuntimeException) + throw (RuntimeException)ex; + else + throw new RuntimeException(ex); } // Try locally - log.warning("execute locally"); - p_stmt = local_getStatement (false, null); // shared connection - return p_stmt.executeUpdate(p_vo.getSql()); + if (!CConnection.get().isRMIoverHTTP()) + { + log.warning("execute locally"); + p_stmt = local_getStatement (false, null); // shared connection + return p_stmt.executeUpdate(p_vo.getSql()); + } + else + { + throw new IllegalStateException("WAN - Application server not available"); + } } // executeUpdate /** @@ -860,12 +886,23 @@ public class CStatement implements Statement { log.log(Level.SEVERE, "AppsServer error", ex); p_remoteErrors++; + if (ex instanceof RuntimeException) + throw (RuntimeException)ex; + else + throw new RuntimeException(ex); } // Try locally - log.warning("Execute locally"); - p_stmt = local_getStatement(false, null); // shared connection - p_vo.clearParameters(); // re-use of result set - return local_getRowSet(); + if (!CConnection.get().isRMIoverHTTP()) + { + log.warning("Execute locally"); + p_stmt = local_getStatement(false, null); // shared connection + p_vo.clearParameters(); // re-use of result set + return local_getRowSet(); + } + else + { + throw new IllegalStateException("WAN - Application server not available"); + } } /************************************************************************* diff --git a/dbPort/src/org/compiere/util/DB.java b/dbPort/src/org/compiere/util/DB.java index 44fcd43798..698da57763 100644 --- a/dbPort/src/org/compiere/util/DB.java +++ b/dbPort/src/org/compiere/util/DB.java @@ -60,6 +60,8 @@ public final class DB private static Connection s_connectionID = null; /** Logger */ private static CLogger log = CLogger.getCLogger (DB.class); + + private static Object s_ccLock = new Object(); /** SQL Statement Separator "; " */ public static final String SQLSTATEMENT_SEPARATOR = "; "; @@ -228,9 +230,7 @@ public final class DB DB.closeTarget(); // - if (s_cc == null) - s_cc = cc; - synchronized (s_cc) // use as mutex + synchronized(s_ccLock) { s_cc = cc; s_connections = null; @@ -239,6 +239,7 @@ public final class DB } if ( isRemoteObjects() == false) s_cc.setDataSource(); + log.config(s_cc + " - DS=" + s_cc.isDataSource()); // Trace.printStack(); } // setDBTarget @@ -249,8 +250,14 @@ public final class DB */ public static boolean connect() { //wan profile - if (DB.isRemoteObjects()) return true; + if (CConnection.get().isRMIoverHTTP()) + return CConnection.get().isAppsServerOK(true); + //vpn profile + if (isRemoteObjects() && CConnection.get().isAppsServerOK(true)) + return true; + + //direct connection boolean success =false; try { @@ -284,8 +291,14 @@ public final class DB if (s_cc == null) return false; //wan profile - if (DB.isRemoteObjects()) return true; + if (CConnection.get().isRMIoverHTTP()) + return s_cc.isAppsServerOK(createNew); + //vpn + if (isRemoteObjects() && s_cc.isAppsServerOK(createNew)) + return true; + + //direct connection boolean success = false; CLogErrorBuffer eb = CLogErrorBuffer.get(false); if (eb != null && eb.isIssueError()) @@ -322,7 +335,7 @@ public final class DB public static Connection getConnectionRW (boolean createNew) { //wan profile - if (DB.isRemoteObjects()) return null; + if (CConnection.get().isRMIoverHTTP()) return null; // check health of connection try @@ -374,7 +387,7 @@ public final class DB public static Connection getConnectionID () { //wan profile - if (DB.isRemoteObjects()) return null; + if (CConnection.get().isRMIoverHTTP()) return null; if (s_connectionID != null) { @@ -405,11 +418,11 @@ public final class DB public static Connection getConnectionRO () { //wan profile - if (DB.isRemoteObjects()) return null; + if (CConnection.get().isRMIoverHTTP()) return null; try { - synchronized (s_cc) // use as mutex as s_connection is null the first time + synchronized(s_ccLock) { if (s_connections == null) s_connections = createConnections (Connection.TRANSACTION_READ_COMMITTED); // see below @@ -485,8 +498,8 @@ public final class DB */ public static Connection createConnection (boolean autoCommit, int trxLevel) { - //wan and vpn - if (isRemoteObjects()) return null; + //wan profile + if (CConnection.get().isRMIoverHTTP()) return null; Connection conn = s_cc.getConnection (autoCommit, trxLevel); if (CLogMgt.isLevelFinest()) @@ -708,6 +721,7 @@ public final class DB } } s_connections = null; + // RW connection try {