IDEMPIERE-5376 CConnection and DB clean up (#1432)

This commit is contained in:
hengsin 2022-08-10 20:52:53 +08:00 committed by GitHub
parent d8f48d1f17
commit 3a2f0ff64d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 77 deletions

View File

@ -1116,7 +1116,6 @@ public class CConnection implements Serializable, Cloneable
* @param transactionIsolation Connection transaction level * @param transactionIsolation Connection transaction level
* @return Connection * @return Connection
*/ */
@SuppressWarnings("unused")
public Connection getConnection (boolean autoCommit, int transactionIsolation) public Connection getConnection (boolean autoCommit, int transactionIsolation)
{ {
Connection conn = null; Connection conn = null;
@ -1133,18 +1132,7 @@ public class CConnection implements Serializable, Cloneable
try try
{ {
// if (!Ini.isClient() // Server conn = m_db.getCachedConnection(this, autoCommit, transactionIsolation);
// && trxLevel != Connection.TRANSACTION_READ_COMMITTED) // PO_LOB.save()
// {
//Exception ee = null;
try
{
conn = m_db.getCachedConnection(this, autoCommit, transactionIsolation);
}
catch (Exception e)
{
//ee = e;
}
// Verify Connection // Verify Connection
if (conn != null) if (conn != null)
{ {
@ -1160,19 +1148,13 @@ public class CConnection implements Serializable, Cloneable
String msg = ule.getLocalizedMessage() String msg = ule.getLocalizedMessage()
+ " -> Did you set the LD_LIBRARY_PATH ? - " + getConnectionURL(); + " -> Did you set the LD_LIBRARY_PATH ? - " + getConnectionURL();
m_dbException = new Exception(msg); m_dbException = new Exception(msg);
log.severe(msg); System.err.println(msg);
} }
catch (SQLException ex) catch (SQLException ex)
{ {
m_dbException = ex; m_dbException = ex;
if (conn == null) if (conn == null)
{ {
//log might cause infinite loop since it will try to acquire database connection again
/*
log.log(Level.SEVERE, getConnectionURL ()
+ ", (1) AutoCommit=" + autoCommit + ",TrxIso=" + getTransactionIsolationInfo(transactionIsolation)
+ " - " + ex.getMessage());
*/
System.err.println(getConnectionURL () System.err.println(getConnectionURL ()
+ ", (1) AutoCommit=" + autoCommit + ",TrxIso=" + getTransactionIsolationInfo(transactionIsolation) + ", (1) AutoCommit=" + autoCommit + ",TrxIso=" + getTransactionIsolationInfo(transactionIsolation)
+ " - " + ex.getMessage()); + " - " + ex.getMessage());
@ -1181,29 +1163,24 @@ public class CConnection implements Serializable, Cloneable
{ {
try try
{ {
log.severe(getConnectionURL () System.err.println(getConnectionURL ()
+ ", (2) AutoCommit=" + conn.getAutoCommit() + "->" + autoCommit + ", (2) AutoCommit=" + conn.getAutoCommit() + "->" + autoCommit
+ ", TrxIso=" + getTransactionIsolationInfo(conn.getTransactionIsolation()) + "->" + getTransactionIsolationInfo(transactionIsolation) + ", TrxIso=" + getTransactionIsolationInfo(conn.getTransactionIsolation()) + "->" + getTransactionIsolationInfo(transactionIsolation)
// + " (" + getDbUid() + "/" + getDbPwd() + ")"
+ " - " + ex.getMessage()); + " - " + ex.getMessage());
} }
catch (Exception ee) catch (Exception ee)
{ {
log.severe(getConnectionURL () System.err.println(getConnectionURL ()
+ ", (3) AutoCommit=" + autoCommit + ", TrxIso=" + getTransactionIsolationInfo(transactionIsolation) + ", (1) AutoCommit=" + autoCommit + ",TrxIso=" + getTransactionIsolationInfo(transactionIsolation)
// + " (" + getDbUid() + "/" + getDbPwd() + ")" + " - " + ex.getMessage());
+ " - " + ex.getMessage());
} }
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
m_dbException = ex; m_dbException = ex;
//log might cause infinite loop since it will try to acquire database connection again
//log.log(Level.SEVERE, getConnectionURL(), ex);
System.err.println(getConnectionURL() + " - " + ex.getLocalizedMessage()); System.err.println(getConnectionURL() + " - " + ex.getLocalizedMessage());
} }
// System.err.println ("CConnection.getConnection - " + conn);
return conn; return conn;
} // getConnection } // getConnection
@ -1214,7 +1191,7 @@ public class CConnection implements Serializable, Cloneable
public Exception getDatabaseException () public Exception getDatabaseException ()
{ {
return m_dbException; return m_dbException;
} // getConnectionException }
/*************************************************************************/ /*************************************************************************/

View File

@ -419,22 +419,12 @@ public final class DB
public static Connection createConnection (boolean autoCommit, int trxLevel) public static Connection createConnection (boolean autoCommit, int trxLevel)
{ {
Connection conn = s_cc.getConnection (autoCommit, trxLevel); Connection conn = s_cc.getConnection (autoCommit, trxLevel);
if (CLogMgt.isLevelFinest())
{
/**
try
{
log.finest(s_cc.getConnectionURL()
+ ", UserID=" + s_cc.getDbUid()
+ ", AutoCommit=" + conn.getAutoCommit() + " (" + autoCommit + ")"
+ ", TrxIso=" + conn.getTransactionIsolation() + "( " + trxLevel + ")");
}
catch (Exception e)
{
}
**/
}
if (conn == null)
{
throw new IllegalStateException("DB.createConnection - @NoDBConnection@");
}
//hengsin: failed to set autocommit can lead to severe lock up of the system //hengsin: failed to set autocommit can lead to severe lock up of the system
try { try {
if (conn != null && conn.getAutoCommit() != autoCommit) if (conn != null && conn.getAutoCommit() != autoCommit)
@ -447,46 +437,19 @@ public final class DB
} // createConnection } // createConnection
/** /**
* @Deprecated (since="10", forRemoval=true)
* Create new Connection. * Create new Connection.
* The connection must be closed explicitly by the application * The connection must be closed explicitly by the application
* *
* @param autoCommit auto commit * @param autoCommit auto commit
* @param readOnly ignore
* @param trxLevel - Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_REPEATABLE_READ, or Connection.TRANSACTION_READ_COMMITTED. * @param trxLevel - Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_REPEATABLE_READ, or Connection.TRANSACTION_READ_COMMITTED.
* @return Connection connection * @return Connection connection
* @deprecated
*/ */
public static Connection createConnection (boolean autoCommit, boolean readOnly, int trxLevel) public static Connection createConnection (boolean autoCommit, boolean readOnly, int trxLevel)
{ {
Connection conn = s_cc.getConnection (autoCommit, trxLevel); return createConnection(autoCommit, trxLevel);
//hengsin: this could be problematic as it can be reuse for readwrite activites after return to pool
/*
if (conn != null)
{
try
{
conn.setReadOnly(readOnly);
}
catch (SQLException ex)
{
conn = null;
log.log(Level.SEVERE, ex.getMessage(), ex);
}
}*/
if (conn == null)
{
throw new IllegalStateException("DB.getConnectionRO - @NoDBConnection@");
}
//hengsin: failed to set autocommit can lead to severe lock up of the system
try {
if (conn.getAutoCommit() != autoCommit)
{
throw new IllegalStateException("Failed to set the requested auto commit mode on connection. [autocommit=" + autoCommit +"]");
}
} catch (SQLException e) {}
return conn;
} // createConnection } // createConnection
/** /**

View File

@ -45,7 +45,7 @@ public class AutoCommitConnectionBroker {
} }
} catch (SQLException e) {} } catch (SQLException e) {}
Connection connection = DB.createConnection(true, false, Connection.TRANSACTION_READ_COMMITTED); Connection connection = DB.createConnection(true, Connection.TRANSACTION_READ_COMMITTED);
connReference = new ConnectionReference(connection); connReference = new ConnectionReference(connection);
threadLocalConnection.set(connReference); threadLocalConnection.set(connReference);
return connection; return connection;