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

* IDEMPIERE-5376 CConnection and DB clean up

* IDEMPIERE-5376 CConnection and DB clean up

- More clean up
This commit is contained in:
hengsin 2022-08-13 18:05:52 +08:00 committed by GitHub
parent 165229d2db
commit 288e96fe8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 186 additions and 407 deletions

View File

@ -149,7 +149,7 @@ public class ApplyMigrationScripts extends SvrProcess {
if (statementReady) {
if (sqlBuf.length() == 0)
continue;
Connection conn = DB.getConnectionRW();
Connection conn = DB.getConnection();
conn.setAutoCommit(false);
Statement stmt = null;
try {

View File

@ -113,7 +113,7 @@ public class ColumnSync extends SvrProcess
Connection conn = null;
ResultSet rs = null;
try {
conn = DB.getConnectionRO();
conn = DB.getConnection();
DatabaseMetaData md = conn.getMetaData();
String catalog = DB.getDatabase().getCatalog();
String schema = DB.getDatabase().getSchema();

View File

@ -94,7 +94,7 @@ public class TableCreateColumns extends SvrProcess
Connection conn = null;
try {
conn = DB.getConnectionRO();
conn = DB.getConnection();
AdempiereDatabase db = DB.getDatabase();
DatabaseMetaData md = conn.getMetaData();
String catalog = db.getCatalog();

View File

@ -256,7 +256,7 @@ public class UUIDGenerator extends SvrProcess {
// Find Column in Database
Connection conn = null;
try {
conn = DB.getConnectionRO();
conn = DB.getConnection();
DatabaseMetaData md = conn.getMetaData();
String catalog = DB.getDatabase().getCatalog();
String schema = DB.getDatabase().getSchema();

View File

@ -55,7 +55,7 @@ public class MIssue extends X_AD_Issue
{
if (s_log.isLoggable(Level.CONFIG))
s_log.config(record.getMessage());
if (!DB.isConnected(false))
if (!DB.isConnected())
return null;
MSystem system = MSystem.get(Env.getCtx());
if (system == null || !system.isAutoErrorReport())

View File

@ -184,7 +184,7 @@ public class MSequence extends X_AD_Sequence
{
try
{
conn = DB.getConnectionID();
conn = DB.getConnection(false);
// Error
if (conn == null)
return -1;
@ -388,7 +388,7 @@ public class MSequence extends X_AD_Sequence
if (trx != null)
conn = trx.getConnection();
else
conn = DB.getConnectionID();
conn = DB.getConnection(false);
// Error
if (conn == null)
return null;

View File

@ -216,13 +216,13 @@ public class CLogErrorBuffer extends Handler
String methodName = record.getSourceMethodName(); //
if (methodName == null)
methodName = "";
if (DB.isConnected(false)
&& methodName != null
if (methodName != null
&& !methodName.equals("saveError")
&& !methodName.equals("get_Value")
&& !methodName.equals("dataSave")
&& loggerName.indexOf("Issue") == -1
&& loggerName.indexOf("CConnection") == -1
&& DB.isConnected()
)
{
// create issue on a separate thread in order to eventually

View File

@ -38,8 +38,6 @@ import java.util.Properties;
import java.util.logging.Level;
import javax.sql.RowSet;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import org.adempiere.exceptions.DBException;
import org.adempiere.util.ProcessUtil;
@ -224,8 +222,6 @@ public final class DB
mailPassword = Ini.getVar("ADEMPIERE_MAIL_PASSWORD");
else
mailPassword = env.getProperty("ADEMPIERE_MAIL_PASSWORD");
// if (mailPassword == null || mailPassword.length() == 0)
// return;
//
StringBuilder sql = new StringBuilder("UPDATE AD_Client SET")
.append(" SMTPHost=").append(DB.TO_STRING(server))
@ -278,7 +274,6 @@ public final class DB
s_cc.setDataSource();
if (log.isLoggable(Level.CONFIG)) log.config(s_cc + " - DS=" + s_cc.isDataSource());
// Trace.printStack();
} // setDBTarget
/**
@ -290,25 +285,14 @@ public final class DB
boolean success =false;
try
{
Connection connRW = getConnectionRW();
if (connRW != null)
Connection conn = getConnection();
if (conn != null)
{
s_cc.readInfo(connRW);
connRW.close();
s_cc.readInfo(conn);
conn.close();
}
Connection connRO = getConnectionRO();
if (connRO != null)
{
connRO.close();
}
Connection connID = getConnectionID();
if (connID != null)
{
connID.close();
}
success = ((connRW != null) && (connRO != null) && (connID != null));
success = (conn != null);
}
catch (Exception e)
{
@ -325,16 +309,6 @@ public final class DB
* @return true, if connected to database
*/
public static boolean isConnected()
{
return isConnected(true);
}
/**
* Is there a connection to the database ?
* @param createNew If true, try to connect it not already connected
* @return true, if connected to database
*/
public static boolean isConnected(boolean createNew)
{
//bug [1637432]
if (s_cc == null) return false;
@ -343,7 +317,7 @@ public final class DB
boolean success = false;
try
{
Connection conn = getConnectionRW(createNew); // try to get a connection
Connection conn = getConnection(); // try to get a connection
if (conn != null)
{
conn.close();
@ -356,55 +330,107 @@ public final class DB
success = false;
}
return success;
} // isConnected
/**
* @return Connection (r/w)
*/
public static Connection getConnectionRW()
{
return getConnectionRW(true);
}
/**
* @Deprecated (since="10", forRemoval=true)
* Replace by {@link #isConnected()}
*
* Is there a connection to the database ?
* @param createNew ignore
* @return true, if connected to database
* @deprecated
*/
public static boolean isConnected(boolean createNew)
{
return isConnected();
} // isConnected
/**
* Get auto commit connection from connection pool.
* @return {@link Connection}
*/
public static Connection getConnection()
{
return getConnection(true);
}
/**
* Get auto or not auto commit connection from connection pool.
* Usually you should use @{@link #getConnection()} instead to get auto commit connection
* and use {@link Trx} to works with not autoCommit connection.
* @param autoCommit
* @return {@link Connection}
*/
public static Connection getConnection(boolean autoCommit)
{
return createConnection(autoCommit, Connection.TRANSACTION_READ_COMMITTED);
}
/**
* @Deprecated (since="10", forRemoval=true)
* Replace by @{@link #getConnection()}
*
* @return Connection (r/w)
* @deprecated
*/
public static Connection getConnectionRW()
{
return getConnection();
}
/**
* @Deprecated (since="10", forRemoval=true)
* Replace by @{@link #getConnection()}
*
* Return (pooled) r/w AutoCommit, Serializable connection.
* For Transaction control use Trx.getConnection()
* @param createNew If true, try to create new connection if no existing connection
* @param createNew ignore
* @return Connection (r/w)
* @deprecated
*/
public static Connection getConnectionRW (boolean createNew)
{
return createConnection(true, false, Connection.TRANSACTION_READ_COMMITTED);
return getConnection();
} // getConnectionRW
/**
* @Deprecated (since="10", forRemoval=true)
* Replace by @{@link #getConnection(boolean)}.
* Note that this is intended for internal use only from the beginning.
*
* Return everytime a new r/w no AutoCommit, Serializable connection.
* To be used to ID
* @return Connection (r/w)
* @deprecated
*/
public static Connection getConnectionID ()
{
return createConnection(false, false, Connection.TRANSACTION_READ_COMMITTED);
return getConnection(false);
} // getConnectionID
/**
* @Deprecated (since="10", forRemoval=true)
* Replace by @{@link #getConnection()}. Use {@link Trx} instead for readonly transaction.
*
* Return read committed, read/only from pool.
* @return Connection (r/o)
* @deprecated
*/
public static Connection getConnectionRO ()
{
return createConnection(true, true, Connection.TRANSACTION_READ_COMMITTED); // see below
return getConnection();
} // getConnectionRO
/**
* Return a replica connection if possible, otherwise read committed, read/only from pool.
* Return a replica connection if possible, otherwise from pool.
* @return Connection (r/o)
*/
public static Connection getReportingConnectionRO ()
{
Connection conn = DBReadReplica.getConnectionRO();
if (conn == null)
conn = getConnectionRO();
conn = getConnection();
return conn;
} // getReportingConnectionRO
@ -438,8 +464,11 @@ public final class DB
/**
* @Deprecated (since="10", forRemoval=true)
* Replace by {@link #createConnection(boolean, int)}.
* Use {@link Trx} instead for readonly transaction.
*
* 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 readOnly ignore
@ -515,13 +544,16 @@ public final class DB
/**************************************************************************
* @Deprecated (since="10", forRemoval=true)
*
* Check database Version with Code version
* @param ctx context
* @return true if Database version (date) is the same
* @deprecated
*/
public static boolean isDatabaseOK (Properties ctx)
{
// Check Version
// Check Version
String version = "?";
String sql = "SELECT Version FROM AD_System";
PreparedStatement pstmt = null;
@ -551,21 +583,10 @@ public final class DB
return true;
String AD_Message = "DatabaseVersionError";
String title = org.compiere.Adempiere.getName() + " " + Msg.getMsg(ctx, AD_Message, true);
// Code assumes Database version {0}, but Database has Version {1}.
String msg = Msg.getMsg(ctx, AD_Message); // complete message
msg = MessageFormat.format(msg, new Object[] {Adempiere.DB_VERSION, version});
Object[] options = { UIManager.get("OptionPane.noButtonText"), "Migrate" };
int no = JOptionPane.showOptionDialog (null, msg,
title, JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
UIManager.getIcon("OptionPane.errorIcon"), options, options[0]);
if (no == 1)
{
JOptionPane.showMessageDialog (null,
"Start RUN_Migrate (in utils)\nSee: http://www.adempiere.com/maintain",
title, JOptionPane.INFORMATION_MESSAGE);
Env.exitEnv(1);
}
System.err.println(msg);
return false;
} // isDatabaseOK
@ -577,7 +598,7 @@ public final class DB
*/
public static boolean isBuildOK (Properties ctx)
{
// Check Build
// Check Build
String buildClient = Adempiere.getVersion();
String buildDatabase = "";
boolean failOnBuild = false;
@ -614,7 +635,6 @@ public final class DB
return true;
String AD_Message = "BuildVersionError";
String title = org.compiere.Adempiere.getName() + " " + Msg.getMsg(ctx, AD_Message, true);
// The program assumes build version {0}, but database has build Version {1}.
String msg = Msg.getMsg(ctx, AD_Message); // complete message
msg = MessageFormat.format(msg, new Object[] {buildClient, buildDatabase});
@ -623,18 +643,8 @@ public final class DB
return true;
}
if (Ini.isClient())
{
JOptionPane.showMessageDialog (null,
msg,
title, JOptionPane.ERROR_MESSAGE);
Env.exitEnv(1);
}
else
{
log.log(Level.SEVERE, msg);
}
return false;
log.log(Level.SEVERE, msg);
return false;
} // isDatabaseOK
@ -659,7 +669,7 @@ public final class DB
/**************************************************************************
* Prepare Forward Read Only Call
* @param sql SQL
* @param sql
* @return Callable Statement
*/
public static CallableStatement prepareCall(String sql)
@ -669,7 +679,7 @@ public final class DB
/**************************************************************************
* Prepare Call
* @param SQL sql
* @param SQL
* @param resultSetConcurrency
* @param trxName
* @return Callable Statement
@ -707,10 +717,10 @@ public final class DB
/**
* Prepare Statement.
* @param sql sql statement
* @param sql
* @param resultSetType - ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.TYPE_SCROLL_SENSITIVE
* @param resultSetConcurrency - ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
* @return Prepared Statement r/o or r/w depending on concur
* @return Prepared Statement
* @deprecated
*/
public static CPreparedStatement prepareStatement (String sql,
@ -721,11 +731,11 @@ public final class DB
/**
* Prepare Statement.
* @param sql sql statement
* @param sql
* @param resultSetType - ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.TYPE_SCROLL_SENSITIVE
* @param resultSetConcurrency - ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
* @param trxName transaction name
* @return Prepared Statement r/o or r/w depending on concur
* @return Prepared Statement
*/
public static CPreparedStatement prepareStatement(String sql,
int resultSetType, int resultSetConcurrency, String trxName)
@ -750,7 +760,7 @@ public final class DB
* @param resultSetType - ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.TYPE_SCROLL_SENSITIVE
* @param resultSetConcurrency - ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
* @param trxName transaction name
* @return Statement - either r/w ir r/o depending on concur
* @return Statement
*/
public static Statement createStatement(int resultSetType, int resultSetConcurrency, String trxName)
{
@ -826,7 +836,7 @@ public final class DB
/**
* Execute Update.
* saves "DBExecuteError" in Log
* @param sql sql
* @param sql
* @return number of rows updated or -1 if error
* @deprecated
*/
@ -838,7 +848,7 @@ public final class DB
/**
* Execute Update.
* saves "DBExecuteError" in Log
* @param sql sql
* @param sql
* @param trxName optional transaction name
* @return number of rows updated or -1 if error
*/
@ -850,7 +860,7 @@ public final class DB
/**
* Execute Update.
* saves "DBExecuteError" in Log
* @param sql sql
* @param sql
* @param trxName optional transaction name
* @param timeOut optional timeout parameter
* @return number of rows updated or -1 if error
@ -863,7 +873,7 @@ public final class DB
/**
* Execute Update.
* saves "DBExecuteError" in Log
* @param sql sql
* @param sql
* @param ignoreError if true, no execution error is reported
* @return number of rows updated or -1 if error
* @deprecated
@ -876,7 +886,7 @@ public final class DB
/**
* Execute Update.
* saves "DBExecuteError" in Log
* @param sql sql
* @param sql
* @param ignoreError if true, no execution error is reported
* @param trxName transaction
* @return number of rows updated or -1 if error
@ -889,7 +899,7 @@ public final class DB
/**
* Execute Update.
* saves "DBExecuteError" in Log
* @param sql sql
* @param sql
* @param ignoreError if true, no execution error is reported
* @param trxName transaction
* @param timeOut optional timeOut parameter
@ -903,7 +913,7 @@ public final class DB
/**
* Execute Update.
* saves "DBExecuteError" in Log
* @param sql sql
* @param sql
* @param param int param
* @param trxName transaction
* @return number of rows updated or -1 if error
@ -916,7 +926,7 @@ public final class DB
/**
* Execute Update.
* saves "DBExecuteError" in Log
* @param sql sql
* @param sql
* @param param int param
* @param trxName transaction
* @param timeOut optional timeOut parameter
@ -930,7 +940,7 @@ public final class DB
/**
* Execute Update.
* saves "DBExecuteError" in Log
* @param sql sql
* @param sql
* @param param int parameter
* @param ignoreError if true, no execution error is reported
* @param trxName transaction
@ -944,7 +954,7 @@ public final class DB
/**
* Execute Update.
* saves "DBExecuteError" in Log
* @param sql sql
* @param sql
* @param param int parameter
* @param ignoreError if true, no execution error is reported
* @param trxName transaction
@ -959,7 +969,7 @@ public final class DB
/**
* Execute Update.
* saves "DBExecuteError" in Log
* @param sql sql
* @param sql
* @param params array of parameters
* @param ignoreError if true, no execution error is reported
* @param trxName optional transaction name
@ -973,7 +983,7 @@ public final class DB
/**
* Execute Update.
* saves "DBExecuteError" in Log
* @param sql sql
* @param sql
* @param params array of parameters
* @param ignoreError if true, no execution error is reported
* @param trxName optional transaction name
@ -984,7 +994,7 @@ public final class DB
{
if (sql == null || sql.length() == 0)
throw new IllegalArgumentException("Required parameter missing - " + sql);
verifyTrx(trxName, sql);
verifyTrx(trxName);
//
int no = -1;
CPreparedStatement cs = ProxyFactory.newCPreparedStatement(ResultSet.TYPE_FORWARD_ONLY,
@ -999,11 +1009,6 @@ public final class DB
cs.setQueryTimeout(timeOut);
}
no = cs.executeUpdate();
// No Transaction - Commit
if (trxName == null)
{
cs.commit(); // Local commit
}
}
catch (Exception e)
{
@ -1016,7 +1021,6 @@ public final class DB
String msg = DBException.getDefaultDBExceptionMessage(e);
log.saveError (msg != null ? msg : "DBExecuteError", e);
}
// throw new DBException(e);
}
finally
{
@ -1054,7 +1058,7 @@ public final class DB
if (sql == null || sql.length() == 0)
throw new IllegalArgumentException("Required parameter missing - " + sql);
//
verifyTrx(trxName, sql);
verifyTrx(trxName);
int no = -1;
CPreparedStatement cs = ProxyFactory.newCPreparedStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE, sql, trxName); // converted in call
@ -1069,11 +1073,6 @@ public final class DB
}
}
no = cs.executeUpdate();
// No Transaction - Commit
if (trxName == null)
{
cs.commit(); // Local commit
}
}
catch (Exception e)
{
@ -1182,16 +1181,26 @@ public final class DB
*/
public static boolean rollback (boolean throwException, String trxName) throws SQLException
{
// Not on transaction scope, Connection are thus auto commit/rollback
if (trxName == null)
{
return true;
}
try
{
Connection conn = null;
Trx trx = trxName == null ? null : Trx.get(trxName, true);
Trx trx = Trx.get(trxName, false);
if (trx != null)
return trx.rollback(true);
else
conn = DB.getConnectionRW ();
if (conn != null && !conn.getAutoCommit())
conn.rollback();
if (throwException)
{
throw new IllegalStateException("Could not load transation with identifier: " + trxName);
}
else
{
return false;
}
}
catch (SQLException e)
{
@ -1200,14 +1209,13 @@ public final class DB
throw e;
return false;
}
return true;
} // commit
/**
* Get Row Set.
* When a Rowset is closed, it also closes the underlying connection.
* If the created RowSet is transfered by RMI, closing it makes no difference
* @param sql sql
* @param sql
* @return row set or null
*/
public static RowSet getRowSet (String sql)
@ -1222,8 +1230,8 @@ public final class DB
/**
* Get int Value from sql
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params array of parameters
* @return first value or -1 if not found
* @throws DBException if there is any SQLException
@ -1275,8 +1283,8 @@ public final class DB
/**
* Get String Value from sql
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params collection of parameters
* @return first value or -1
* @throws DBException if there is any SQLException
@ -1288,8 +1296,8 @@ public final class DB
/**
* Get int Value from sql
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params array of parameters
* @return first value or -1 if not found or error
*/
@ -1309,8 +1317,8 @@ public final class DB
/**
* Get int Value from sql
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params collection of parameters
* @return first value or null
*/
@ -1321,8 +1329,8 @@ public final class DB
/**
* Get String Value from sql
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params array of parameters
* @return first value or null
* @throws DBException if there is any SQLException
@ -1374,8 +1382,8 @@ public final class DB
/**
* Get String Value from sql
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params collection of parameters
* @return first value or null
* @throws DBException if there is any SQLException
@ -1387,8 +1395,8 @@ public final class DB
/**
* Get String Value from sql
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params array of parameters
* @return first value or null
*/
@ -1408,8 +1416,8 @@ public final class DB
/**
* Get String Value from sql
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params collection of parameters
* @return first value or null
*/
@ -1420,8 +1428,8 @@ public final class DB
/**
* Get BigDecimal Value from sql
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params array of parameters
* @return first value or null if not found
* @throws DBException if there is any SQLException
@ -1473,8 +1481,8 @@ public final class DB
/**
* Get BigDecimal Value from sql
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params collection of parameters
* @return first value or null if not found
* @throws DBException if there is any SQLException
@ -1487,8 +1495,8 @@ public final class DB
/**
* Get BigDecimal Value from sql
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params array of parameters
* @return first value or null
*/
@ -1508,8 +1516,8 @@ public final class DB
/**
* Get BigDecimal Value from sql
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params collection of parameters
* @return first value or null
*/
@ -1520,8 +1528,8 @@ public final class DB
/**
* Get Timestamp Value from sql
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params array of parameters
* @return first value or null
* @throws DBException if there is any SQLException
@ -1573,8 +1581,8 @@ public final class DB
/**
* Get BigDecimal Value from sql
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params collection of parameters
* @return first value or null if not found
* @throws DBException if there is any SQLException
@ -1586,8 +1594,8 @@ public final class DB
/**
* Get Timestamp Value from sql
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params array of parameters
* @return first value or null
*/
@ -1606,8 +1614,8 @@ public final class DB
/**
* Get Timestamp Value from sql
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params collection of parameters
* @return first value or null
*/
@ -1679,7 +1687,6 @@ public final class DB
}
KeyNamePair[] retValue = new KeyNamePair[list.size()];
list.toArray(retValue);
// s_log.fine("getKeyNamePairs #" + retValue.length);
return retValue;
} // getKeyNamePairs
@ -1822,10 +1829,10 @@ public final class DB
/**************************************************************************
* Get next number for Key column = 0 is Error.
* * @param ctx client
@param TableName table name
* @param trxName optionl transaction name
* @return next no
* @param ctx client
* @param TableName table name
* @param trxName optional transaction name
* @return next id no
*/
public static int getNextID (Properties ctx, String TableName, String trxName)
{
@ -1841,11 +1848,11 @@ public final class DB
* @param AD_Client_ID client
* @param TableName table name
* @param trxName optional Transaction Name
* @return next no
* @return next id no
*/
public static int getNextID (int AD_Client_ID, String TableName, String trxName)
{
return MSequence.getNextID (AD_Client_ID, TableName, trxName); // it is ok to call deprecated method here
return MSequence.getNextID (AD_Client_ID, TableName, trxName);
} // getNextID
/**
@ -1950,6 +1957,7 @@ public final class DB
} // getDocumentNo
/**
* @Deprecated (forRemoval=true)
* Is this a remote client connection.
*
* Deprecated, always return false.
@ -1962,6 +1970,7 @@ public final class DB
} // isRemoteObjects
/**
* @Deprecated (forRemoval=true)
* Is this a remote client connection
*
* Deprecated, always return false.
@ -2057,7 +2066,7 @@ public final class DB
/**
* Package Strings for SQL command in quotes
* @param txt String with text
* @return escaped string for insert statement (NULL if null)
* @return escaped string for sql statement (NULL if null)
*/
public static String TO_STRING (String txt)
{
@ -2072,7 +2081,7 @@ public final class DB
* </pre>
* @param txt String with text
* @param maxLength Maximum Length of content or 0 to ignore
* @return escaped string for insert statement (NULL if null)
* @return escaped string for sql statement (NULL if null)
*/
public static String TO_STRING (String txt, int maxLength)
{
@ -2166,19 +2175,6 @@ public final class DB
/** Quote */
private static final char QUOTE = '\'';
/**
* Run Post Migration manually
* @param args ignored
*/
public static void main (String[] args)
{
Adempiere.startup(true);
MSystem system = MSystem.get(Env.getCtx());
system.setIsJustMigrated(true);
afterMigration(Env.getCtx());
} // main
// Following methods are kept for BeanShell compatibility.
// See BF [ 2030233 ] Remove duplicate code from DB class
// TODO: remove this when BeanShell will support varargs methods
@ -2210,6 +2206,7 @@ public final class DB
{
return getSQLValueBD(trxName, sql, new Object[]{int_param1});
}
//End BeanShell compatibility.
/**
* Get Array of ValueNamePair items.
@ -2404,7 +2401,7 @@ public final class DB
return m_isUUIDSupported;
}
private static void verifyTrx(String trxName, String sql) {
private static void verifyTrx(String trxName) {
if (trxName != null && Trx.get(trxName, false) == null) {
// Using a trx that was previously closed or never opened
// probably timed out - throw Exception (IDEMPIERE-644)
@ -2419,7 +2416,7 @@ public final class DB
* @return true if table or view with name=tableName exists in db
*/
public static boolean isTableOrViewExists(String tableName) {
Connection conn = getConnectionRO();
Connection conn = getConnection();
ResultSet rs = null;
try {
DatabaseMetaData metadata = conn.getMetaData();
@ -2447,8 +2444,8 @@ public final class DB
/**
* Get an array of objects from sql (one per each column on the select clause), column indexing starts with 0
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params array of parameters
* @return null if not found
* @throws DBException if there is any SQLException
@ -2508,8 +2505,8 @@ public final class DB
/**
* Get an array of arrays of objects from sql (one per each row, and one per each column on the select clause), column indexing starts with 0
* WARNING: This method must be used just for queries returning few records, using it for many records implies heavy memory consumption
* @param trxName trx
* @param sql sql
* @param trxName optional transaction name
* @param sql
* @param params array of parameters
* @return null if not found
* @throws DBException if there is any SQLException
@ -2570,7 +2567,7 @@ public final class DB
/**
* Prepare Read Replica Statement
* @param sql sql statement
* @param sql
* @param trxName transaction
* @return Prepared Statement (from replica if possible, otherwise normal statement)
*/
@ -2580,7 +2577,7 @@ public final class DB
/**
* Prepare Read Replica Statement
* @param sql sql statement
* @param sql
* @param resultSetType - ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.TYPE_SCROLL_SENSITIVE
* @param resultSetConcurrency - ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
* @param trxName transaction name

View File

@ -181,7 +181,7 @@ public final class Env
public static void exitEnv (int status)
{
//hengsin, avoid unncessary query of session when exit without log in
if (DB.isConnected(false)) {
if (DB.isConnected()) {
// End Session
MSession session = MSession.get(Env.getCtx()); // finish
if (session != null) {

View File

@ -214,7 +214,7 @@ public class MoveClient extends SvrProcess {
try {
try {
if (p_IsCopyClient) {
externalConn = DB.getConnectionRO();
externalConn = DB.getConnection();
} else {
externalConn = DB.getDatabase(p_JDBC_URL).getDriverConnection(p_JDBC_URL, p_UserName, p_Password);
}

View File

@ -27,7 +27,6 @@ import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
@ -42,7 +41,6 @@ import javax.sql.DataSource;
import org.adempiere.db.oracle.OracleBundleActivator;
import org.adempiere.exceptions.DBException;
import org.compiere.Adempiere;
import org.compiere.dbPort.Convert;
import org.compiere.dbPort.Convert_Oracle;
import org.compiere.model.MColumn;
@ -950,222 +948,6 @@ public class DB_Oracle implements AdempiereDatabase
return null; //do not do re-execution of alternative SQL
}
/**************************************************************************
* Testing
* @param args ignored
*/
public static void main (String[] args)
{
Adempiere.startupEnvironment(true);
CConnection cc = CConnection.get();
DB_Oracle db = (DB_Oracle)cc.getDatabase();
db.cleanup();
try
{
Connection conn = null;
// System.out.println("Driver=" + db.getDriverConnection(cc));
DataSource ds = db.getDataSource(cc);
System.out.println("DS=" + ds.getConnection());
conn = db.getCachedConnection(cc, true, Connection.TRANSACTION_READ_COMMITTED);
System.out.println("Cached=" + conn);
System.out.println(db);
//////////////////////////
System.out.println("JAVA classpath: [\n" +
System.getProperty("java.class.path") + "\n]");
DatabaseMetaData dmd = conn.getMetaData();
System.out.println("DriverVersion: ["+
dmd.getDriverVersion()+"]");
System.out.println("DriverMajorVersion: ["+
dmd.getDriverMajorVersion()+"]");
System.out.println("DriverMinorVersion: ["+
dmd.getDriverMinorVersion()+"]");
System.out.println("DriverName: ["+
dmd.getDriverName()+"]");
System.out.println("ProductName: ["+
dmd.getDatabaseProductName() +"]");
System.out.println("ProductVersion: [\n"+
dmd.getDatabaseProductVersion()+"\n]");
//////////////////////////
}
catch (Exception e1)
{
e1.printStackTrace();
}
db.cleanup();
System.out.println("--------------------------------------------------");
/**
DROP TABLE X_Test;
CREATE TABLE X_Test
(
Text1 NVARCHAR2(2000) NULL,
Text2 VARCHAR2(2000) NULL
);
try
{
String myString1 = "123456789 12345678";
StringBuilder myString = new StringBuilder();
for (int i = 0; i < 99; i++)
myString.append(myString1).append((char)('a'+i)).append("\n");
System.out.println(myString.length());
System.out.println(Util.size(myString.toString()));
//
myString = new StringBuilder().append(Util.trimSize(myString.toString(), 2000));
System.out.println(myString.length());
System.out.println(Util.size(myString.toString()));
//
Connection conn2 = db.getCachedConnection(cc, true, Connection.TRANSACTION_READ_COMMITTED);
//
PreparedStatement pstmt = conn2.prepareStatement
("INSERT INTO X_Test(Text1, Text2) values(?,?)");
pstmt.setString(1, myString.toString()); // NVARCHAR2 column
pstmt.setString(2, myString.toString()); // VARCHAR2 column
System.out.println(pstmt.executeUpdate());
//
Statement stmt = conn2.createStatement();
System.out.println(stmt.executeUpdate
("INSERT INTO X_Test(Text1, Text2) values('" + myString + "','" + myString + "')"));
}
catch (Exception e)
{
e.printStackTrace();
}
db.cleanup();
System.out.println("--------------------------------------------------");
**/
System.exit(0);
System.out.println("--------------------------------------------------");
try
{
Connection conn1 = db.getCachedConnection(cc, false, Connection.TRANSACTION_READ_COMMITTED);
Connection conn2 = db.getCachedConnection(cc, true, Connection.TRANSACTION_READ_COMMITTED);
Connection conn3 = db.getCachedConnection(cc, false, Connection.TRANSACTION_READ_COMMITTED);
System.out.println("3 -> " + db);
conn1.close();
conn2.close();
conn1 = db.getCachedConnection(cc, true, Connection.TRANSACTION_READ_COMMITTED);
conn2 = db.getCachedConnection(cc, true, Connection.TRANSACTION_READ_COMMITTED);
System.out.println("3 -> " + db);
conn1.close();
conn2.close();
conn3.close();
System.out.println("0 -> " + db);
}
catch (Exception e1)
{
e1.printStackTrace();
}
db.cleanup();
// System.exit(0);
System.out.println("--------------------------------------------------");
System.out.println(DB.getConnectionRO());
System.out.println(DB.getConnectionRW());
System.out.println(DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED));
System.out.println(DB.getConnectionRO());
System.out.println(DB.getConnectionRW());
System.out.println(DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED));
System.out.println(DB.getConnectionRO());
System.out.println(DB.getConnectionRW());
System.out.println(DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED));
System.out.println(DB.getConnectionRO());
System.out.println(DB.getConnectionRW());
System.out.println(DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED));
System.out.println(DB.getConnectionRO());
System.out.println(DB.getConnectionRW());
System.out.println(DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED));
System.out.println(DB.getConnectionRO());
System.out.println(DB.getConnectionRW());
System.out.println(DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED));
System.out.println(DB.getConnectionRO());
System.out.println(DB.getConnectionRW());
System.out.println(DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED));
System.out.println(DB.getConnectionRO());
System.out.println(DB.getConnectionRW());
System.out.println(DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED));
System.out.println(DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED));
System.out.println(DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED));
System.out.println(DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED));
System.out.println(DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED));
System.out.println(DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED));
System.out.println(db);
try
{
System.out.println("-- Sleeping --");
Thread.sleep(60000);
System.out.println(db);
db.close();
db.cleanup();
System.out.println(db);
}
catch (InterruptedException e)
{
}
/**
// Connection option 1
try
{
System.setProperty("oracle.jdbc.Trace", "true");
DriverManager.registerDriver(new OracleDriver());
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//dev:1521/dev", "adempiere", "adempiere");
System.out.println("Catalog=" + con.getCatalog());
DatabaseMetaData md = con.getMetaData();
System.out.println("URL=" + md.getURL());
System.out.println("User=" + md.getUserName());
//
System.out.println("Catalog");
ResultSet rs = md.getCatalogs();
while (rs.next())
System.out.println("- " + rs.getString(1));
//
System.out.println("Table");
rs = md.getTables(null, "ADEMPIERE", null, new String[] {"TABLE"});
while (rs.next())
System.out.println("- User=" + rs.getString(2) + " | Table=" + rs.getString(3)
+ " | Type=" + rs.getString(4) + " | " + rs.getString(5));
//
System.out.println("Column");
rs = md.getColumns(null, "ADEMPIERE", "C_ORDER", null);
while (rs.next())
System.out.println("- Tab=" + rs.getString(3) + " | Col=" + rs.getString(4)
+ " | Type=" + rs.getString(5) + ", " + rs.getString(6)
+ " | Size=" + rs.getString(7) + " | " + rs.getString(8)
+ " | Digits=" + rs.getString(9) + " | Radix=" + rs.getString(10)
+ " | Null=" + rs.getString(11) + " | Rem=" + rs.getString(12)
+ " | Def=" + rs.getString(13) + " | " + rs.getString(14)
+ " | " + rs.getString(15) + " | " + rs.getString(16)
+ " | Ord=" + rs.getString(17) + " | Null=" + rs.getString(18)
);
con.close();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
**/
} // main
public Convert getConvert() {
return m_convert;
}

View File

@ -68,7 +68,7 @@ public class DBDataSource extends compiereDataSource
jr.getQuery(),
parametersMap,
params,
DB.getConnectionRO()
DB.getConnection()
);
if(m_pstmt != null)
m_resultSet = m_pstmt.executeQuery();

View File

@ -103,7 +103,7 @@ public class ReportProcessor
Language currLang = Env.getLanguage(Env.getCtx());
m_Param.put("CURRENT_LANG", currLang.getAD_Language());
java.sql.Connection conn = DB.getConnectionRO();
java.sql.Connection conn = DB.getConnection();
m_Param.put("REPORT_CONNECTION", conn ); //DB_CONN
m_Param.put("DB_CONN", conn );