* Fixed wan connection profile issue reported by CarlosRuiz

This commit is contained in:
Heng Sin Low 2007-01-10 09:33:05 +00:00
parent 93aab65e1c
commit 8a0091735b
3 changed files with 135 additions and 236 deletions

View File

@ -54,25 +54,33 @@ public class CPreparedStatement extends CStatement implements PreparedStatement
p_vo.setTrxName(trxName); p_vo.setTrxName(trxName);
init();
} // CPreparedStatement
/**
* Initialise the prepared statement wrapper object
*/
protected void init()
{
//Local access //Local access
if (!DB.isRemoteObjects()) if (!DB.isRemoteObjects())
{ {
try try
{ {
Connection conn = null; 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) if (trx != null)
conn = trx.getConnection(); conn = trx.getConnection();
else else
{ {
if (resultSetConcurrency == ResultSet.CONCUR_UPDATABLE) if (p_vo.getResultSetConcurrency() == ResultSet.CONCUR_UPDATABLE)
conn = DB.getConnectionRW (); conn = DB.getConnectionRW ();
else else
conn = DB.getConnectionRO(); conn = DB.getConnectionRO();
} }
if (conn == null) if (conn == null)
throw new DBException("No Connection"); 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; return;
} }
catch (Exception e) catch (Exception e)
@ -80,7 +88,7 @@ public class CPreparedStatement extends CStatement implements PreparedStatement
log.log(Level.SEVERE, p_vo.getSql(), e); log.log(Level.SEVERE, p_vo.getSql(), e);
} }
} }
} // CPreparedStatement }
/** /**
* Remote Constructor * Remote Constructor
@ -831,44 +839,65 @@ public class CPreparedStatement extends CStatement implements PreparedStatement
/** /**
* Get Result as RowSet for local system. * Execute Query
* Get explicit connection as connection is closed when closing RowSet * @return ResultSet or RowSet
* @return result as 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 try
{ {
AdempiereDatabase db = CConnection.get().getDatabase(); boolean remote = DB.isRemoteObjects() && CConnection.get().isAppsServerOK(false);
if (db == null) if (remote && p_remoteErrors > 1)
throw new IllegalStateException("No Database"); remote = CConnection.get().isAppsServerOK(true);
// if (remote)
PreparedStatement pstmt = local_getPreparedStatement(true, null); // decicated connection {
ResultSet rs = pstmt.executeQuery(); Server server = CConnection.get().getServer();
RowSet rowSet = db.getRowSet (rs); if (server != null)
rs.close(); {
pstmt.close(); RowSet rs = server.pstmt_getRowSet (p_vo);
// p_vo.clearParameters(); // re-use of result set
if (rowSet == null) if (rs == null)
throw new NullPointerException("No RowSet"); log.warning("RowSet is null - " + p_vo);
// return rowSet; else
p_remoteErrors = 0;
return rs;
}
log.log(Level.SEVERE, "AppsServer not found");
p_remoteErrors++;
}
} }
catch (Exception ex) catch (Exception ex)
{ {
log.log(Level.SEVERE, p_vo.toString(), ex); log.log(Level.SEVERE, "AppsServer error", ex);
throw new RuntimeException(ex); p_remoteErrors++;
} }
**/ // Try locally
// dedicated connection log.warning("Execute locally");
Connection conn = DB.createConnection (false, Connection.TRANSACTION_READ_COMMITTED); p_stmt = local_getPreparedStatement (false, null); // shared connection
PreparedStatement pstmt = null; 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; RowSet rowSet = null;
PreparedStatement pstmt = (PreparedStatement)p_stmt;
try try
{ {
pstmt = conn.prepareStatement(p_vo.getSql(),
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
// Set Parameters // Set Parameters
ArrayList parameters = p_vo.getParameters(); ArrayList parameters = p_vo.getParameters();
for (int i = 0; i < parameters.size(); i++) for (int i = 0; i < parameters.size(); i++)
@ -911,8 +940,6 @@ public class CPreparedStatement extends CStatement implements PreparedStatement
rs.close(); rs.close();
pstmt.close(); pstmt.close();
pstmt = null; pstmt = null;
conn.close();
conn = null;
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -925,9 +952,6 @@ public class CPreparedStatement extends CStatement implements PreparedStatement
if (pstmt != null) if (pstmt != null)
pstmt.close(); pstmt.close();
pstmt = null; pstmt = null;
if (conn != null)
conn.close();
conn = null;
} }
catch (Exception e) catch (Exception e)
{ {
@ -936,116 +960,6 @@ public class CPreparedStatement extends CStatement implements PreparedStatement
return rowSet; return rowSet;
} // local_getRowSet } // 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. * Execute Update.
* @return row count * @return row count

View File

@ -21,7 +21,9 @@ import java.rmi.*;
import java.sql.*; import java.sql.*;
import java.util.*; import java.util.*;
import java.util.logging.*; import java.util.logging.*;
import javax.sql.*; import javax.sql.*;
import org.compiere.db.*; import org.compiere.db.*;
import org.compiere.interfaces.*; import org.compiere.interfaces.*;
@ -44,26 +46,35 @@ public class CStatement implements Statement
String trxName) String trxName)
{ {
p_vo = new CStatementVO (resultSetType, resultSetConcurrency); p_vo = new CStatementVO (resultSetType, resultSetConcurrency);
p_vo.setTrxName(trxName);
init();
} // CPreparedStatement
/**
* Initialise the statement wrapper object
*/
protected void init()
{
//Local access //Local access
if (!DB.isRemoteObjects()) if (!DB.isRemoteObjects())
{ {
try try
{ {
Connection conn = null; 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) if (trx != null)
conn = trx.getConnection(); conn = trx.getConnection();
else else
{ {
if (resultSetConcurrency == ResultSet.CONCUR_UPDATABLE) if (p_vo.getResultSetConcurrency() == ResultSet.CONCUR_UPDATABLE)
conn = DB.getConnectionRW (); conn = DB.getConnectionRW ();
else else
conn = DB.getConnectionRO(); conn = DB.getConnectionRO();
} }
if (conn == null) if (conn == null)
throw new DBException("No Connection"); throw new DBException("No Connection");
p_stmt = conn.createStatement(resultSetType, resultSetConcurrency); p_stmt = conn.createStatement(p_vo.getResultSetType(), p_vo.getResultSetConcurrency());
return; return;
} }
catch (SQLException e) catch (SQLException e)
@ -71,7 +82,7 @@ public class CStatement implements Statement
log.log(Level.SEVERE, "CStatement", e); log.log(Level.SEVERE, "CStatement", e);
} }
} }
} // CPreparedStatement }
/** /**
* Minimum Constructor for sub classes * Minimum Constructor for sub classes
@ -88,6 +99,7 @@ public class CStatement implements Statement
public CStatement (CStatementVO vo) public CStatement (CStatementVO vo)
{ {
p_vo = vo; p_vo = vo;
init();
} // CPreparedStatement } // CPreparedStatement
@ -809,90 +821,66 @@ public class CStatement implements Statement
return conn; return conn;
} // local_getConnection } // 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
*/
public RowSet remote_getRowSet()
{
log.finest("remote");
/** /**
* Execute Query
* @return ResultSet or RowSet
* @throws SQLException
* @see java.sql.PreparedStatement#executeQuery()
*/
public RowSet getRowSet()
{
if (p_stmt != null) // local
return local_getRowSet();
//
// Client -> remote sever
log.finest("server => " + p_vo + ", Remote=" + DB.isRemoteObjects());
try try
{ {
AdempiereDatabase db = CConnection.get().getDatabase(); boolean remote = DB.isRemoteObjects() && CConnection.get().isAppsServerOK(false);
if (db == null) if (remote && p_remoteErrors > 1)
remote = CConnection.get().isAppsServerOK(true);
if (remote)
{ {
log.log(Level.SEVERE, "No Database"); Server server = CConnection.get().getServer();
throw new NullPointerException("Remote - No Database"); if (server != null)
} {
// RowSet rs = server.stmt_getRowSet (p_vo);
Statement stmt = local_getStatement (false, null); // shared connection p_vo.clearParameters(); // re-use of result set
ResultSet rs = stmt.executeQuery(p_vo.getSql()); if (rs == null)
RowSet rowSet = db.getRowSet (rs); log.warning("RowSet is null - " + p_vo);
rs.close();
stmt.close();
//
if (rowSet != null)
return rowSet;
else else
log.log(Level.SEVERE, "No RowSet"); p_remoteErrors = 0;
throw new NullPointerException("Remore - No RowSet"); return rs;
}
log.log(Level.SEVERE, "AppsServer not found");
p_remoteErrors++;
}
} }
catch (Exception ex) catch (Exception ex)
{ {
log.log(Level.SEVERE, p_vo.toString(), ex); log.log(Level.SEVERE, "AppsServer error", ex);
throw new RuntimeException (ex); p_remoteErrors++;
} }
// return null; // Try locally
**/ log.warning("Execute locally");
// Shared Connection p_stmt = local_getStatement(false, null); // shared connection
Connection conn = local_getConnection (p_vo.getTrxName()); p_vo.clearParameters(); // re-use of result set
PreparedStatement pstmt = null; 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; RowSet rowSet = null;
try try
{ {
pstmt = conn.prepareStatement(p_vo.getSql(), ResultSet rs = pstmt.executeQuery(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); rowSet = CCachedRowSet.getRowSet(rs);
rs.close(); rs.close();
pstmt.close(); pstmt.close();
@ -915,7 +903,7 @@ public class CStatement implements Statement
log.log(Level.SEVERE, "close pstmt", e); log.log(Level.SEVERE, "close pstmt", e);
} }
return rowSet; return rowSet;
} // remote_getRowSet } // local_getRowSet
public boolean isPoolable() throws SQLException{ return false;} public boolean isPoolable() throws SQLException{ return false;}

View File

@ -233,6 +233,7 @@ public final class DB
s_connectionRW = null; s_connectionRW = null;
s_connectionID = null; s_connectionID = null;
} }
if ( isRemoteObjects() == false)
s_cc.setDataSource(); s_cc.setDataSource();
log.config(s_cc + " - DS=" + s_cc.isDataSource()); log.config(s_cc + " - DS=" + s_cc.isDataSource());
// Trace.printStack(); // Trace.printStack();
@ -477,6 +478,9 @@ public final class DB
*/ */
public static Connection createConnection (boolean autoCommit, int trxLevel) public static Connection createConnection (boolean autoCommit, int trxLevel)
{ {
//wan and vpn
if (isRemoteObjects()) return null;
Connection conn = s_cc.getConnection (autoCommit, trxLevel); Connection conn = s_cc.getConnection (autoCommit, trxLevel);
if (CLogMgt.isLevelFinest()) if (CLogMgt.isLevelFinest())
{ {
@ -1138,7 +1142,7 @@ public final class DB
* @param local local RowSet (own connection) * @param local local RowSet (own connection)
* @return row set or null * @return row set or null
*/ */
public static RowSet getRowSet (String sql, boolean local) public static RowSet getRowSet (String sql)
{ {
RowSet retValue = null; RowSet retValue = null;
// Bugfix Gunther Hoppe, 02.09.2005 add vpj-cd e-evolution // 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)); CStatementVO info = new CStatementVO (RowSet.TYPE_SCROLL_INSENSITIVE, RowSet.CONCUR_READ_ONLY, DB.getDatabase().convertStatement(sql));
// End add vpj-cd e-evolution // End add vpj-cd e-evolution
CPreparedStatement stmt = new CPreparedStatement(info); CPreparedStatement stmt = new CPreparedStatement(info);
if (local) retValue = stmt.getRowSet();
{
retValue = stmt.local_getRowSet();
}
else
{
retValue = stmt.remote_getRowSet();
}
return retValue; return retValue;
} // getRowSet } // getRowSet