From 8a0091735b8dda6c415cb46d324c180e0ad65fe0 Mon Sep 17 00:00:00 2001 From: Heng Sin Low Date: Wed, 10 Jan 2007 09:33:05 +0000 Subject: [PATCH] * Fixed wan connection profile issue reported by CarlosRuiz --- .../org/compiere/util/CPreparedStatement.java | 210 ++++++------------ dbPort/src/org/compiere/util/CStatement.java | 144 ++++++------ dbPort/src/org/compiere/util/DB.java | 17 +- 3 files changed, 135 insertions(+), 236 deletions(-) diff --git a/dbPort/src/org/compiere/util/CPreparedStatement.java b/dbPort/src/org/compiere/util/CPreparedStatement.java index 5ceb0202cc..76a1f50088 100644 --- a/dbPort/src/org/compiere/util/CPreparedStatement.java +++ b/dbPort/src/org/compiere/util/CPreparedStatement.java @@ -54,25 +54,33 @@ public class CPreparedStatement extends CStatement implements PreparedStatement p_vo.setTrxName(trxName); - // Local access + init(); + } // CPreparedStatement + + /** + * Initialise the prepared statement wrapper object + */ + protected void init() + { + //Local access if (!DB.isRemoteObjects()) { try { Connection conn = null; - Trx trx = trxName == null ? null : Trx.get(trxName, true); + Trx trx = p_vo.getTrxName() == null ? null : Trx.get(p_vo.getTrxName(), true); if (trx != null) conn = trx.getConnection(); else { - if (resultSetConcurrency == ResultSet.CONCUR_UPDATABLE) + if (p_vo.getResultSetConcurrency() == ResultSet.CONCUR_UPDATABLE) conn = DB.getConnectionRW (); else conn = DB.getConnectionRO(); } if (conn == null) throw new DBException("No Connection"); - p_stmt = conn.prepareStatement (p_vo.getSql(), resultSetType, resultSetConcurrency); + p_stmt = conn.prepareStatement (p_vo.getSql(), p_vo.getResultSetType(), p_vo.getResultSetConcurrency()); return; } catch (Exception e) @@ -80,8 +88,8 @@ public class CPreparedStatement extends CStatement implements PreparedStatement log.log(Level.SEVERE, p_vo.getSql(), e); } } - } // CPreparedStatement - + } + /** * Remote Constructor * @param vo value object @@ -831,44 +839,65 @@ public class CPreparedStatement extends CStatement implements PreparedStatement /** - * Get Result as RowSet for local system. - * Get explicit connection as connection is closed when closing RowSet - * @return result as RowSet + * Execute Query + * @return ResultSet or RowSet + * @throws SQLException + * @see java.sql.PreparedStatement#executeQuery() */ - public RowSet local_getRowSet() + public RowSet getRowSet() { - log.finest("local"); - /** + if (p_stmt != null) // local + return local_getRowSet(); + // + // Client -> remote sever + log.finest("server => " + p_vo + ", Remote=" + DB.isRemoteObjects()); try { - AdempiereDatabase db = CConnection.get().getDatabase(); - if (db == null) - throw new IllegalStateException("No Database"); - // - PreparedStatement pstmt = local_getPreparedStatement(true, null); // decicated connection - ResultSet rs = pstmt.executeQuery(); - RowSet rowSet = db.getRowSet (rs); - rs.close(); - pstmt.close(); - // - if (rowSet == null) - throw new NullPointerException("No RowSet"); - // return rowSet; + boolean remote = DB.isRemoteObjects() && CConnection.get().isAppsServerOK(false); + if (remote && p_remoteErrors > 1) + remote = CConnection.get().isAppsServerOK(true); + if (remote) + { + Server server = CConnection.get().getServer(); + if (server != null) + { + RowSet rs = server.pstmt_getRowSet (p_vo); + p_vo.clearParameters(); // re-use of result set + if (rs == null) + log.warning("RowSet is null - " + p_vo); + else + p_remoteErrors = 0; + return rs; + } + log.log(Level.SEVERE, "AppsServer not found"); + p_remoteErrors++; + } } catch (Exception ex) { - log.log(Level.SEVERE, p_vo.toString(), ex); - throw new RuntimeException(ex); + log.log(Level.SEVERE, "AppsServer error", ex); + p_remoteErrors++; } - **/ - // dedicated connection - Connection conn = DB.createConnection (false, Connection.TRANSACTION_READ_COMMITTED); - PreparedStatement pstmt = null; + // 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(); + } + + /** + * Get Result as RowSet for local system. + * Note that connection is closed when closing Oracle CachedRowSet! + * @return result as RowSet + */ + protected RowSet local_getRowSet() + { + log.finest("local"); + RowSet rowSet = null; + PreparedStatement pstmt = (PreparedStatement)p_stmt; try { - pstmt = conn.prepareStatement(p_vo.getSql(), - ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); // Set Parameters ArrayList parameters = p_vo.getParameters(); for (int i = 0; i < parameters.size(); i++) @@ -911,8 +940,6 @@ public class CPreparedStatement extends CStatement implements PreparedStatement rs.close(); pstmt.close(); pstmt = null; - conn.close(); - conn = null; } catch (Exception ex) { @@ -925,9 +952,6 @@ public class CPreparedStatement extends CStatement implements PreparedStatement if (pstmt != null) pstmt.close(); pstmt = null; - if (conn != null) - conn.close(); - conn = null; } catch (Exception e) { @@ -936,116 +960,6 @@ public class CPreparedStatement extends CStatement implements PreparedStatement return rowSet; } // local_getRowSet - /************************************************************************* - * Get Result as RowSet for Remote. - * Get shared connection for RMI! - * If RowSet is transfred via RMI, closing the RowSet does not close the connection - * @return result as RowSet - */ - public RowSet remote_getRowSet() - { - log.finest("remote"); - /** - try - { - AdempiereDatabase db = CConnection.get().getDatabase(); - if (db == null) - { - log.log(Level.SEVERE, "No Database"); - throw new NullPointerException("No Database"); - } - // - PreparedStatement pstmt = local_getPreparedStatement(false, null); // shared connection - ResultSet rs = pstmt.executeQuery(); - RowSet rowSet = db.getRowSet (rs); - rs.close(); - pstmt.close(); - // - if (rowSet != null) - return rowSet; - else - log.log(Level.SEVERE, "No RowSet"); - throw new NullPointerException("Remote - No RowSet"); - } - catch (Exception ex) - { - log.log(Level.SEVERE, p_vo.toString(), ex); - throw new RuntimeException (ex); - } - // return null; - **/ - // shared connection - Connection conn = local_getConnection (p_vo.getTrxName()); - PreparedStatement pstmt = null; - RowSet rowSet = null; - try - { - pstmt = conn.prepareStatement(p_vo.getSql(), - ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); - // Set Parameters - ArrayList parameters = p_vo.getParameters(); - for (int i = 0; i < parameters.size(); i++) - { - Object o = parameters.get(i); - if (o == null) - throw new IllegalArgumentException ("Null Parameter #" + i); - else if (o instanceof NullParameter) - { - int type = ((NullParameter)o).getType(); - pstmt.setNull(i+1, type); - log.finest("#" + (i+1) + " - Null"); - } - else if (o instanceof Integer) - { - pstmt.setInt(i+1, ((Integer)o).intValue()); - log.finest("#" + (i+1) + " - int=" + o); - } - else if (o instanceof String) - { - pstmt.setString(i+1, (String)o); - log.finest("#" + (i+1) + " - String=" + o); - } - else if (o instanceof Timestamp) - { - pstmt.setTimestamp(i+1, (Timestamp)o); - log.finest("#" + (i+1) + " - Timestamp=" + o); - } - else if (o instanceof BigDecimal) - { - pstmt.setBigDecimal(i+1, (BigDecimal)o); - log.finest("#" + (i+1) + " - BigDecimal=" + o); - } - else - throw new java.lang.UnsupportedOperationException ("Unknown Parameter Class=" + o.getClass()); - } - // - // - ResultSet rs = pstmt.executeQuery(); - rowSet = CCachedRowSet.getRowSet(rs); - pstmt.close(); - pstmt = null; - rs.close(); - rs = null; - } - catch (Exception ex) - { - log.log(Level.SEVERE, p_vo.toString(), ex); - throw new RuntimeException (ex); - } - // Close Cursor - try - { - if (pstmt != null) - pstmt.close(); - pstmt = null; - } - catch (Exception e) - { - log.log(Level.SEVERE, "close pstmt", e); - } - return rowSet; - } // remote_getRowSet - /************************************************************************* * Execute Update. * @return row count diff --git a/dbPort/src/org/compiere/util/CStatement.java b/dbPort/src/org/compiere/util/CStatement.java index 6e12928df8..52135348ca 100644 --- a/dbPort/src/org/compiere/util/CStatement.java +++ b/dbPort/src/org/compiere/util/CStatement.java @@ -21,7 +21,9 @@ import java.rmi.*; import java.sql.*; import java.util.*; import java.util.logging.*; + import javax.sql.*; + import org.compiere.db.*; import org.compiere.interfaces.*; @@ -44,26 +46,35 @@ public class CStatement implements Statement String trxName) { p_vo = new CStatementVO (resultSetType, resultSetConcurrency); + p_vo.setTrxName(trxName); - // Local access + init(); + } // CPreparedStatement + + /** + * Initialise the statement wrapper object + */ + protected void init() + { + //Local access if (!DB.isRemoteObjects()) { try { Connection conn = null; - Trx trx = trxName == null ? null : Trx.get(trxName, true); + Trx trx = p_vo.getTrxName() == null ? null : Trx.get(p_vo.getTrxName(), true); if (trx != null) conn = trx.getConnection(); else { - if (resultSetConcurrency == ResultSet.CONCUR_UPDATABLE) + if (p_vo.getResultSetConcurrency() == ResultSet.CONCUR_UPDATABLE) conn = DB.getConnectionRW (); else conn = DB.getConnectionRO(); } if (conn == null) throw new DBException("No Connection"); - p_stmt = conn.createStatement(resultSetType, resultSetConcurrency); + p_stmt = conn.createStatement(p_vo.getResultSetType(), p_vo.getResultSetConcurrency()); return; } catch (SQLException e) @@ -71,8 +82,8 @@ public class CStatement implements Statement log.log(Level.SEVERE, "CStatement", e); } } - } // CPreparedStatement - + } + /** * Minimum Constructor for sub classes */ @@ -88,6 +99,7 @@ public class CStatement implements Statement public CStatement (CStatementVO vo) { p_vo = vo; + init(); } // CPreparedStatement @@ -809,90 +821,66 @@ public class CStatement implements Statement return conn; } // local_getConnection - /************************************************************************* - * Get Result as RowSet for Remote. - * Get shared connection for RMI! - * If RowSet is transfred via RMI, closing the RowSet does not close the connection - * @return result as RowSet + /** + * Execute Query + * @return ResultSet or RowSet + * @throws SQLException + * @see java.sql.PreparedStatement#executeQuery() */ - public RowSet remote_getRowSet() + public RowSet getRowSet() { - log.finest("remote"); - /** + if (p_stmt != null) // local + return local_getRowSet(); + // + // Client -> remote sever + log.finest("server => " + p_vo + ", Remote=" + DB.isRemoteObjects()); try { - AdempiereDatabase db = CConnection.get().getDatabase(); - if (db == null) + boolean remote = DB.isRemoteObjects() && CConnection.get().isAppsServerOK(false); + if (remote && p_remoteErrors > 1) + remote = CConnection.get().isAppsServerOK(true); + if (remote) { - log.log(Level.SEVERE, "No Database"); - throw new NullPointerException("Remote - No Database"); + Server server = CConnection.get().getServer(); + if (server != null) + { + RowSet rs = server.stmt_getRowSet (p_vo); + p_vo.clearParameters(); // re-use of result set + if (rs == null) + log.warning("RowSet is null - " + p_vo); + else + p_remoteErrors = 0; + return rs; + } + log.log(Level.SEVERE, "AppsServer not found"); + p_remoteErrors++; } - // - Statement stmt = local_getStatement (false, null); // shared connection - ResultSet rs = stmt.executeQuery(p_vo.getSql()); - RowSet rowSet = db.getRowSet (rs); - rs.close(); - stmt.close(); - // - if (rowSet != null) - return rowSet; - else - log.log(Level.SEVERE, "No RowSet"); - throw new NullPointerException("Remore - No RowSet"); } catch (Exception ex) { - log.log(Level.SEVERE, p_vo.toString(), ex); - throw new RuntimeException (ex); + log.log(Level.SEVERE, "AppsServer error", ex); + p_remoteErrors++; } - // return null; - **/ - // Shared Connection - Connection conn = local_getConnection (p_vo.getTrxName()); - PreparedStatement pstmt = null; + // 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(); + } + + /************************************************************************* + * Get Result as RowSet for Remote. + * Note that close the oracle OracleCachedRowSet also close connection! + * @return result as RowSet + */ + protected RowSet local_getRowSet() + { + log.finest("remote"); + Statement pstmt = p_stmt; RowSet rowSet = null; try { - pstmt = conn.prepareStatement(p_vo.getSql(), - ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); - // Set Parameters - ArrayList parameters = p_vo.getParameters(); - for (int i = 0; i < parameters.size(); i++) - { - Object o = parameters.get(i); - if (o == null) - throw new IllegalArgumentException ("Null Parameter #" + i); - else if (o instanceof NullParameter) - { - int type = ((NullParameter)o).getType(); - pstmt.setNull(i+1, type); - log.finest("#" + (i+1) + " - Null"); - } - else if (o instanceof Integer) - { - pstmt.setInt(i+1, ((Integer)o).intValue()); - log.finest("#" + (i+1) + " - int=" + o); - } - else if (o instanceof String) - { - pstmt.setString(i+1, (String)o); - log.finest("#" + (i+1) + " - String=" + o); - } - else if (o instanceof Timestamp) - { - pstmt.setTimestamp(i+1, (Timestamp)o); - log.finest("#" + (i+1) + " - Timestamp=" + o); - } - else if (o instanceof BigDecimal) - { - pstmt.setBigDecimal(i+1, (BigDecimal)o); - log.finest("#" + (i+1) + " - BigDecimal=" + o); - } - else - throw new java.lang.UnsupportedOperationException ("Unknown Parameter Class=" + o.getClass()); - } - // - ResultSet rs = pstmt.executeQuery(); + ResultSet rs = pstmt.executeQuery(p_vo.getSql()); rowSet = CCachedRowSet.getRowSet(rs); rs.close(); pstmt.close(); @@ -915,7 +903,7 @@ public class CStatement implements Statement log.log(Level.SEVERE, "close pstmt", e); } return rowSet; - } // remote_getRowSet + } // local_getRowSet public boolean isPoolable() throws SQLException{ return false;} diff --git a/dbPort/src/org/compiere/util/DB.java b/dbPort/src/org/compiere/util/DB.java index 5fd1eca1c8..1e4e51dd18 100644 --- a/dbPort/src/org/compiere/util/DB.java +++ b/dbPort/src/org/compiere/util/DB.java @@ -233,7 +233,8 @@ public final class DB s_connectionRW = null; s_connectionID = null; } - s_cc.setDataSource(); + if ( isRemoteObjects() == false) + s_cc.setDataSource(); log.config(s_cc + " - DS=" + s_cc.isDataSource()); // Trace.printStack(); } // setDBTarget @@ -477,6 +478,9 @@ public final class DB */ public static Connection createConnection (boolean autoCommit, int trxLevel) { + //wan and vpn + if (isRemoteObjects()) return null; + Connection conn = s_cc.getConnection (autoCommit, trxLevel); if (CLogMgt.isLevelFinest()) { @@ -1138,7 +1142,7 @@ public final class DB * @param local local RowSet (own connection) * @return row set or null */ - public static RowSet getRowSet (String sql, boolean local) + public static RowSet getRowSet (String sql) { RowSet retValue = null; // Bugfix Gunther Hoppe, 02.09.2005 add vpj-cd e-evolution @@ -1148,14 +1152,7 @@ public final class DB CStatementVO info = new CStatementVO (RowSet.TYPE_SCROLL_INSENSITIVE, RowSet.CONCUR_READ_ONLY, DB.getDatabase().convertStatement(sql)); // End add vpj-cd e-evolution CPreparedStatement stmt = new CPreparedStatement(info); - if (local) - { - retValue = stmt.local_getRowSet(); - } - else - { - retValue = stmt.remote_getRowSet(); - } + retValue = stmt.getRowSet(); return retValue; } // getRowSet