diff --git a/base/.classpath b/base/.classpath
index 7e7879afa6..5fdce263ed 100644
--- a/base/.classpath
+++ b/base/.classpath
@@ -18,5 +18,6 @@
- * Timing: - * - CM with source_route not in address_list = 28.5 sec - * - CM with source_route in address_list = 58.0 sec - * - direct = 4.3-8 sec (no real difference if on other box) - * - bequeath = 3.4-8 sec - *- * @param connection Connection Descriptor - * @return connection String - */ - public String getConnectionURL (CConnection connection) - { - StringBuffer sb = null; - // Server Connections (bequeath) - if (connection.isBequeath()) - { - sb = new StringBuffer ("jdbc:oracle:oci8:@"); - // bug: does not work if there is more than one db instance - use Net8 - // sb.append(connection.getDbName()); - } - else // thin driver - { - sb = new StringBuffer ("jdbc:oracle:thin:@"); - // direct connection - if (connection.isViaFirewall()) - { - // (description=(address_list= - // ( (source_route=yes) - // (address=(protocol=TCP)(host=cmhost)(port=1630)) - // (address=(protocol=TCP)(host=dev)(port=1521)) - // (connect_data=(service_name=dev1.adempiere.org))) - sb.append("(DESCRIPTION=(ADDRESS_LIST=") - .append("(SOURCE_ROUTE=YES)") - .append("(ADDRESS=(PROTOCOL=TCP)(HOST=").append(connection.getFwHost()) - .append(")(PORT=").append(connection.getFwPort()).append("))") - .append("(ADDRESS=(PROTOCOL=TCP)(HOST=").append(connection.getDbHost()) - .append(")(PORT=").append(connection.getDbPort()).append(")))") - .append("(CONNECT_DATA=(SERVICE_NAME=").append(connection.getDbName()).append(")))"); - } - else - { - // old: jdbc:oracle:thin:@dev2:1521:sid - // new: jdbc:oracle:thin:@//dev2:1521/serviceName - sb.append("//") - .append(connection.getDbHost()) - .append(":").append(connection.getDbPort()) - .append("/").append(connection.getDbName()); - } - } - m_connectionURL = sb.toString(); - // log.config(m_connectionURL); - // - m_userName = connection.getDbUid(); - return m_connectionURL; - } // getConnectionURL - - /** - * Get Connection URL. - * http://download-east.oracle.com/docs/cd/B14117_01/java.101/b10979/urls.htm#BEIDBFDF - * @param dbHost db Host - * @param dbPort db Port - * @param dbName db Name - * @param userName user name - * @return connection - */ - public String getConnectionURL (String dbHost, int dbPort, String dbName, - String userName) - { - m_userName = userName; - m_connectionURL = "jdbc:oracle:thin:@//" - + dbHost + ":" + dbPort + "/" + dbName; - return m_connectionURL; - } // getConnectionURL - - /** - * Get Database Connection String - * @param connectionURL Connection URL - * @param userName user name - * @return connection String - */ - public String getConnectionURL (String connectionURL, String userName) - { - m_userName = userName; - m_connectionURL = connectionURL; - return m_connectionURL; - } // getConnectionURL - - /** - * Get JDBC Catalog - * @return null - not used - */ - public String getCatalog() - { - return null; - } // getCatalog - - /** - * Get JDBC Schema - * @return user name - */ - public String getSchema() - { - if (m_userName != null) - return m_userName.toUpperCase(); - log.severe("User Name not set (yet) - call getConnectionURL first"); - return null; - } // getSchema - - /** - * Supports BLOB - * @return true if BLOB is supported - */ - public boolean supportsBLOB() - { - return true; - } // supportsBLOB - - /** - * String Representation - * @return info - */ - public String toString() - { - StringBuffer sb = new StringBuffer("DB_Oracle["); - sb.append(m_connectionURL); - try - { - if (m_ds != null) - sb.append("-").append(m_ds.getDataSourceName()) - // .append(",ExplCache=").append(m_ds.getExplicitCachingEnabled()) - .append(",ImplCache=").append(m_ds.getImplicitCachingEnabled()) - .append(",MaxStmts=").append(m_ds.getMaxStatements()); - // .append(",Ref=").append(m_ds.getReference()); - if (m_cacheMgr != null && m_cacheMgr.existsCache(CACHE_NAME)) - sb.append(";ConnectionActive=").append(m_cacheMgr.getNumberOfActiveConnections(CACHE_NAME)) - .append(",CacheAvailable=").append(m_cacheMgr.getNumberOfAvailableConnections(CACHE_NAME)); - } - catch (Exception e) - { - sb.append("=").append(e.getLocalizedMessage()); - } - sb.append("]"); - return sb.toString(); - } // toString - - /** - * Get Status - * @return status info - */ - public String getStatus() - { - StringBuffer sb = new StringBuffer(); - try - { - if (m_cacheMgr != null && m_cacheMgr.existsCache(CACHE_NAME)) - sb.append("-Connections=").append(m_cacheMgr.getNumberOfActiveConnections(CACHE_NAME)) - .append(",Cache=").append(m_cacheMgr.getNumberOfAvailableConnections(CACHE_NAME)); - } - catch (Exception e) - {} - return sb.toString(); - } // getStatus - - - /************************************************************************** - * Convert an individual Oracle Style statements to target database statement syntax. - * @param oraStatement oracle statement - * @return converted Statement oracle statement - */ - public String convertStatement (String oraStatement) - { - return oraStatement; - } // convertStatement - - - - /** - * Check if DBMS support the sql statement - * @sql SQL statement - * @return true: yes - */ - public boolean isSupported(String sql) - { - return true; - //jz temp, modify later - } - - - - /** - * Get constraint type associated with the index - * @tableName table name - * @IXName Index name - * @return String[0] = 0: do not know, 1: Primary Key 2: Foreign Key - * String[1] - String[n] = Constraint Name - */ - public String getConstraintType(Connection conn, String tableName, String IXName) - { - if (IXName == null || IXName.length()==0) - return "0"; - if (IXName.endsWith("_KEY")) - return "1"+IXName; - else - return "0"; - //jz temp, modify later from user.constraints - } - - /** - * Get Name of System User - * @return system - */ - public String getSystemUser() - { - return "system"; - } // getSystemUser - - /** - * Get Name of System Database - * @param databaseName database Name - * @return e.g. master or database Name - */ - public String getSystemDatabase(String databaseName) - { - return databaseName; - } // getSystemDatabase + /** Logger */ + private static CLogger log = CLogger.getCLogger (DB_Oracle.class); - /** - * Create SQL TO Date String from Timestamp - * - * @param time Date to be converted - * @param dayOnly true if time set to 00:00:00 - * - * @return TO_DATE('2001-01-30 18:10:20',''YYYY-MM-DD HH24:MI:SS') - * or TO_DATE('2001-01-30',''YYYY-MM-DD') - */ - public String TO_DATE (Timestamp time, boolean dayOnly) - { - if (time == null) - { - if (dayOnly) - return "TRUNC(SysDate)"; - return "SysDate"; - } - - StringBuffer dateString = new StringBuffer("TO_DATE('"); - // YYYY-MM-DD HH24:MI:SS.mmmm JDBC Timestamp format - String myDate = time.toString(); - if (dayOnly) - { - dateString.append(myDate.substring(0,10)); - dateString.append("','YYYY-MM-DD')"); - } - else - { - dateString.append(myDate.substring(0, myDate.indexOf('.'))); // cut off miliseconds - dateString.append("','YYYY-MM-DD HH24:MI:SS')"); - } - return dateString.toString(); - } // TO_DATE - - /** - * Create SQL for formatted Date, Number - * - * @param columnName the column name in the SQL - * @param displayType Display Type - * @param AD_Language 6 character language setting (from Env.LANG_*) - * - * @return TRIM(TO_CHAR(columnName,'999G999G999G990D00','NLS_NUMERIC_CHARACTERS='',.''')) - * or TRIM(TO_CHAR(columnName,'TM9')) depending on DisplayType and Language - * @see org.compiere.util.DisplayType - * @see org.compiere.util.Env - * - * */ - public String TO_CHAR (String columnName, int displayType, String AD_Language) - { - StringBuffer retValue = new StringBuffer("TRIM(TO_CHAR("); - retValue.append(columnName); - - // Numbers - if (DisplayType.isNumeric(displayType)) - { - if (displayType == DisplayType.Amount) - retValue.append(",'999G999G999G990D00'"); - else - retValue.append(",'TM9'"); - // TO_CHAR(GrandTotal,'999G999G999G990D00','NLS_NUMERIC_CHARACTERS='',.''') - if (!Language.isDecimalPoint(AD_Language)) // reversed - retValue.append(",'NLS_NUMERIC_CHARACTERS='',.'''"); - } - else if (DisplayType.isDate(displayType)) - { - retValue.append(",'") - .append(Language.getLanguage(AD_Language).getDBdatePattern()) - .append("'"); - } - retValue.append("))"); - // - return retValue.toString(); - } // TO_CHAR - - /** - * Return number as string for INSERT statements with correct precision - * @param number number - * @param displayType display Type - * @return number as string - */ - public String TO_NUMBER (BigDecimal number, int displayType) - { - if (number == null) - return "NULL"; - BigDecimal result = number; - int scale = DisplayType.getDefaultPrecision(displayType); - if (scale > number.scale()) - { - try - { - result = number.setScale(scale, BigDecimal.ROUND_HALF_UP); - } - catch (Exception e) - { - // log.severe("Number=" + number + ", Scale=" + " - " + e.getMessage()); - } - } - return result.toString(); - } // TO_NUMBER - - - /** - * Get SQL Commands. - * The following variables are resolved: - * @SystemPassword@, @AdempiereUser@, @AdempierePassword@ - * @SystemPassword@, @DatabaseName@, @DatabaseDevice@ - * @param cmdType CMD_* - * @return array of commands to be executed - */ - public String[] getCommands (int cmdType) - { - if (CMD_CREATE_USER == cmdType) - return new String[] - { - - }; - // - if (CMD_CREATE_DATABASE == cmdType) - return new String[] - { - - }; - // - if (CMD_DROP_DATABASE == cmdType) - return new String[] - { - - }; - // - return null; - } // getCommands - - /** - * Create DataSource - * @param connection connection - * @return data dource - */ - public DataSource getDataSource(CConnection connection) - { - if (m_ds != null) - return m_ds; - try - { - m_ds = new OracleDataSource(); - m_ds.setDriverType("thin"); - m_ds.setNetworkProtocol("tcp"); - m_ds.setServerName(connection.getDbHost()); - m_ds.setServiceName(connection.getDbName()); - m_ds.setPortNumber(connection.getDbPort()); - m_ds.setUser(connection.getDbUid()); - m_ds.setPassword(connection.getDbPwd()); - // - m_ds.setDataSourceName("AdempiereDS"); - m_ds.setDescription("Adempiere Oracle Data Source"); - m_ds.setImplicitCachingEnabled(true); - m_ds.setExplicitCachingEnabled(true); - m_ds.setMaxStatements(MAX_STATEMENTS); - // http://download-east.oracle.com/docs/cd/B14117_01/java.101/b10979/oralob.htm#sthref1258 - Properties connProperties = new Properties(); - //http://www.db.cs.ucdavis.edu/teaching/165B-SQ06/Project/Readme.txt BUG-1568923 - //bug [ 1568770 ] Oracle CLOBs are limited in size - connProperties.setProperty("SetBigStringTryClob", "true"); - m_ds.setConnectionProperties(connProperties); - // - Properties cacheProperties = new Properties(); - // cacheProperties.setProperty("InitialLimit", "3"); // at startup - // cacheProperties.setProperty("MaxStatementsLimit", "10"); - cacheProperties.setProperty("ClosestConnectionMatch", "true"); - cacheProperties.setProperty("ValidateConnection", "true"); - if (Ini.isClient()) - { - cacheProperties.setProperty("MinLimit", "0"); - // cacheProperties.setProperty("MaxLimit", "5"); - cacheProperties.setProperty("InactivityTimeout", "300"); // 5 Min - cacheProperties.setProperty("AbandonedConnectionTimeout", "300"); // 5 Min - } - else // Server Settings - { - cacheProperties.setProperty("MinLimit", "3"); - // cacheProperties.setProperty("MaxLimit", "5"); - cacheProperties.setProperty("InactivityTimeout", "600"); // 10 Min - cacheProperties.setProperty("AbandonedConnectionTimeout", "600"); // 10 Min - } - cacheProperties.setProperty("PropertyCheckInterval", "120"); // 2 Min - // - if (USE_CACHE) - { - m_ds.setConnectionCachingEnabled(true); - m_ds.setConnectionCacheName(CACHE_NAME); - m_ds.setFastConnectionFailoverEnabled(true); - } - // - if (m_cacheMgr == null && USE_CACHE) - { - m_cacheMgr = OracleConnectionCacheManager.getConnectionCacheManagerInstance(); - if (!m_cacheMgr.existsCache(CACHE_NAME)) - m_cacheMgr.createCache(CACHE_NAME, m_ds, cacheProperties); - } - // test -// OracleConnection con = m_ds.getConnection(); -// con.close(); - // - log.config(toString()); - // - return m_ds; - } - catch (Exception e) - { - log.log(Level.SEVERE, toString(), e); - } - return null; - } // getDataSource + private static int m_maxbusyconnections = 0; - /** - * Get Cached Connection - * @param connection info - * @param autoCommit true if autocommit connection - * @param transactionIsolation Connection transaction level - * @return connection or null - * @throws Exception - */ - public Connection getCachedConnection (CConnection connection, - boolean autoCommit, int transactionIsolation) - throws Exception - { - OracleConnection conn = null; - Exception exception = null; - try - { - if (USE_CACHE && m_cacheMgr == null) - getDataSource(connection); - if (m_ds == null) - getDataSource(connection); - - // Properties connAttr = new Properties(); - // connAttr.setProperty("TRANSACTION_ISOLATION", CConnection.getTransactionIsolationInfo(transactionIsolation)); - // OracleConnection conn = (OracleConnection)m_ds.getConnection(connAttr); - // - // Try 5 times max - for (int i = 0; i < 5; i++) - { - try - { - conn = (OracleConnection)m_ds.getConnection(); - if (conn != null) - { - if (conn.getTransactionIsolation() != transactionIsolation) - conn.setTransactionIsolation(transactionIsolation); - if (conn.getAutoCommit() != autoCommit) - conn.setAutoCommit(autoCommit); - conn.setDefaultRowPrefetch(20); // 10 default - reduces round trips - } - } - catch (Exception e) - { - exception = e; - conn = null; - if (e instanceof SQLException - && ((SQLException)e).getErrorCode() == 1017) // invalid username/password - { - log.severe("Cannot connect to database: " - + getConnectionURL(connection) - + " - UserID=" + connection.getDbUid()); - break; - } - } - try - { - if (conn != null && conn.isClosed()) - conn = null; - // OK - if (conn != null && !conn.isClosed()) - break; - if (i == 0) - Thread.yield(); // give some time - else - Thread.sleep(100); - } - catch (Exception e) - { - exception = e; - conn = null; - } - } // 5 tries - - if (conn == null && exception != null) - { - log.log(Level.SEVERE, exception.toString()); - log.fine(toString()); - log.finest("Reference=" + m_ds.getReference()); - } - // else - // { - // System.out.println(conn + " " + getStatus()); - // conn.registerConnectionCacheCallback(this, "test", OracleConnection.ALL_CONNECTION_CALLBACKS); - // } - } - catch (Exception e) - { - // System.err.println ("DB_Oracle.getCachedConnection"); - // if (!(e instanceof SQLException)) - // e.printStackTrace(); - exception = e; - } - if (exception != null) - throw exception; - return conn; - } // getCachedConnection + /** + * Get Database Name + * @return database short name + */ + public String getName() + { + return Database.DB_ORACLE; + } // getName - /** - * Get Connection from Driver - * @param connection info - * @return connection or null - * @throws SQLException - */ - public Connection getDriverConnection (CConnection connection) throws SQLException - { - getDriver(); - return DriverManager.getConnection (getConnectionURL (connection), - connection.getDbUid(), connection.getDbPwd()); - } // getDriverConnection + /** + * Get Database Description + * @return database long name and version + */ + public String getDescription() + { + try + { + if (s_driver == null) + getDriver(); + } + catch (Exception e) + { + } + if (s_driver != null) + return s_driver.toString(); + return "No Driver"; + } // getDescription - /** - * Get Driver Connection - * @param dbUrl URL - * @param dbUid user - * @param dbPwd password - * @return connection - * @throws SQLException - */ - public Connection getDriverConnection (String dbUrl, String dbUid, String dbPwd) - throws SQLException - { - getDriver(); - return DriverManager.getConnection (dbUrl, dbUid, dbPwd); - } // getDriverConnection + /** + * Get Standard JDBC Port + * @return standard port + */ + public int getStandardPort() + { + return DEFAULT_PORT; + } // getStandardPort - /** - * Close - */ - public void close() - { - log.config(toString()); - if (m_ds != null) - { - try - { - m_ds.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - if (m_cacheMgr != null) - { - try - { - if (m_cacheMgr.existsCache(CACHE_NAME)) - m_cacheMgr.purgeCache(CACHE_NAME, false); // not active - // m_cache.disableCache(CACHE_NAME); - // m_cache.removeCache(CACHE_NAME, 0); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - m_cacheMgr = null; - m_ds = null; - } // close + /** + * Get and register Database Driver + * @return Driver + * @throws SQLException + */ + public Driver getDriver() throws SQLException + { + if (s_driver == null) + { + // Speed up transfer rate + System.setProperty("oracle.jdbc.TcpNoDelay", "true"); + // Oracle Multi - Language + System.setProperty("oracle.jdbc.defaultNChar", "true"); + // + s_driver = new OracleDriver(); + DriverManager.registerDriver (s_driver); + DriverManager.setLoginTimeout (Database.CONNECTION_TIMEOUT); + } + return s_driver; + } // getDriver - /** - * Clean up - */ - public void cleanup() - { - if (!USE_CACHE) - return; - - log.config(""); - try - { - if (m_cacheMgr == null) - m_cacheMgr = OracleConnectionCacheManager.getConnectionCacheManagerInstance(); - String[] cacheNames = m_cacheMgr.getCacheNameList(); - for (int i = 0; i < cacheNames.length; i++) - { - String name = cacheNames[i]; - System.out.println(" cleanup: " + name); - System.out.println(" Before = Active=" + m_cacheMgr.getNumberOfActiveConnections(name) - + ", Available=" + m_cacheMgr.getNumberOfAvailableConnections(name)); - m_cacheMgr.purgeCache(name, false); - System.out.println(" Cached = Active=" + m_cacheMgr.getNumberOfActiveConnections(name) - + ", Available=" + m_cacheMgr.getNumberOfAvailableConnections(name)); - m_cacheMgr.purgeCache(name, true); - System.out.println(" All = Active=" + m_cacheMgr.getNumberOfActiveConnections(name) - + ", Available=" + m_cacheMgr.getNumberOfAvailableConnections(name)); - } - } - catch (Exception e) - { - e.printStackTrace(); - } - } // cleanup + /** + * Get Database Connection String. + *
+ * Timing: + * - CM with source_route not in address_list = 28.5 sec + * - CM with source_route in address_list = 58.0 sec + * - direct = 4.3-8 sec (no real difference if on other box) + * - bequeath = 3.4-8 sec + *+ * @param connection Connection Descriptor + * @return connection String + */ + public String getConnectionURL (CConnection connection) + { + StringBuffer sb = null; + // Server Connections (bequeath) + if (connection.isBequeath()) + { + sb = new StringBuffer ("jdbc:oracle:oci8:@"); + // bug: does not work if there is more than one db instance - use Net8 + // sb.append(connection.getDbName()); + } + else // thin driver + { + sb = new StringBuffer ("jdbc:oracle:thin:@"); + // direct connection + if (connection.isViaFirewall()) + { + // (description=(address_list= + // ( (source_route=yes) + // (address=(protocol=TCP)(host=cmhost)(port=1630)) + // (address=(protocol=TCP)(host=dev)(port=1521)) + // (connect_data=(service_name=dev1.adempiere.org))) + sb.append("(DESCRIPTION=(ADDRESS_LIST=") + .append("(SOURCE_ROUTE=YES)") + .append("(ADDRESS=(PROTOCOL=TCP)(HOST=").append(connection.getFwHost()) + .append(")(PORT=").append(connection.getFwPort()).append("))") + .append("(ADDRESS=(PROTOCOL=TCP)(HOST=").append(connection.getDbHost()) + .append(")(PORT=").append(connection.getDbPort()).append(")))") + .append("(CONNECT_DATA=(SERVICE_NAME=").append(connection.getDbName()).append(")))"); + } + else + { + // old: jdbc:oracle:thin:@dev2:1521:sid + // new: jdbc:oracle:thin:@//dev2:1521/serviceName + sb.append("//") + .append(connection.getDbHost()) + .append(":").append(connection.getDbPort()) + .append("/").append(connection.getDbName()); + } + } + m_connectionURL = sb.toString(); + // log.config(m_connectionURL); + // + m_userName = connection.getDbUid(); + return m_connectionURL; + } // getConnectionURL - /************************************************************************** - * Handle Abandoned Connection - * @param conn connection - * @param userObject - * @return true if close - false for keeping it - */ - public boolean handleAbandonedConnection (OracleConnection conn, Object userObject) - { - System.out.println("--------------------handleAbandonedConnection " + conn + " - " + userObject); - return true; // reclaim it - } // handleAbandonedConnection + /** + * Get Connection URL. + * http://download-east.oracle.com/docs/cd/B14117_01/java.101/b10979/urls.htm#BEIDBFDF + * @param dbHost db Host + * @param dbPort db Port + * @param dbName db Name + * @param userName user name + * @return connection + */ + public String getConnectionURL (String dbHost, int dbPort, String dbName, + String userName) + { + m_userName = userName; + m_connectionURL = "jdbc:oracle:thin:@//" + + dbHost + ":" + dbPort + "/" + dbName; + return m_connectionURL; + } // getConnectionURL - /** - * Release Connection - * @param conn connection - * @param userObject - */ - public void releaseConnection (OracleConnection conn, Object userObject) - { - System.out.println("----------------------releaseConnection " + conn + " - " + userObject); - } // releaseConnection + /** + * Get Database Connection String + * @param connectionURL Connection URL + * @param userName user name + * @return connection String + */ + public String getConnectionURL (String connectionURL, String userName) + { + m_userName = userName; + m_connectionURL = connectionURL; + return m_connectionURL; + } // getConnectionURL - - /** - * Get Data Type - * @param columnName - * @param displayType display type - * @param precision precision - * @param defaultValue if true adds default value - * @return data type - */ - public String getDataType (String columnName, int displayType, int precision, - boolean defaultValue) - { - String retValue = null; - //handle special case, bug [ 1618423 ] - if (columnName != null) - { - if (displayType == DisplayType.Button - && columnName.endsWith("_ID")) - { - retValue = "NUMBER(10)"; - } - } - if (retValue == null) - { - switch (displayType) - { - // IDs - case DisplayType.Account: - case DisplayType.Assignment: - case DisplayType.Color: - case DisplayType.ID: - case DisplayType.Location: - case DisplayType.Locator: - case DisplayType.PAttribute: - case DisplayType.Search: - case DisplayType.Table: - case DisplayType.TableDir: - case DisplayType.Image: - retValue = "NUMBER(10)"; - break; - - // Dynamic Precision - case DisplayType.Amount: - retValue = "NUMBER"; - if (defaultValue) - retValue += " DEFAULT 0"; - break; - - case DisplayType.Binary: - retValue = "BLOB"; - break; - - case DisplayType.Button: - retValue = "CHAR(1)"; - break; - - // Number Dynamic Precision - case DisplayType.CostPrice: - retValue = "NUMBER"; - if (defaultValue) - retValue += " DEFAULT 0"; - break; - - // Date - case DisplayType.Date: - case DisplayType.DateTime: - case DisplayType.Time: - retValue = "DATE"; - if (defaultValue) - retValue += " DEFAULT SYSDATE"; - break; - - // Number(10) - case DisplayType.Integer: - retValue = "NUMBER(10)"; - break; - - case DisplayType.List: - retValue = "CHAR(" + precision + ")"; - break; - - // NVARCHAR - case DisplayType.Memo: - case DisplayType.String: - case DisplayType.Text: - retValue = "NVARCHAR(" + precision + ")"; - break; - - case DisplayType.TextLong: - retValue = "CLOB"; - break; - - // Dyn Prec - case DisplayType.Quantity: - retValue = "NUMBER"; - break; - - case DisplayType.YesNo: - retValue = "CHAR(1)"; - break; - - default: - log.severe("Unknown: " + displayType); - break; - } - } - return retValue; - } // getDataType + /** + * Get JDBC Catalog + * @return null - not used + */ + public String getCatalog() + { + return null; + } // getCatalog + + /** + * Get JDBC Schema + * @return user name + */ + public String getSchema() + { + if (m_userName != null) + return m_userName.toUpperCase(); + log.severe("User Name not set (yet) - call getConnectionURL first"); + return null; + } // getSchema + + /** + * Supports BLOB + * @return true if BLOB is supported + */ + public boolean supportsBLOB() + { + return true; + } // supportsBLOB + + /** + * String Representation + * @return info + */ + public String toString() + { + StringBuffer sb = new StringBuffer("DB_Oracle["); + sb.append(m_connectionURL); + try + { + StringBuffer logBuffer = new StringBuffer(50); + logBuffer.append("# Connections: ").append(m_ds.getNumConnections()); + logBuffer.append(" , # Busy Connections: ").append(m_ds.getNumBusyConnections()); + logBuffer.append(" , # Idle Connections: ").append(m_ds.getNumIdleConnections()); + logBuffer.append(" , # Orphaned Connections: ").append(m_ds.getNumUnclosedOrphanedConnections()); + } + catch (Exception e) + { + sb.append("=").append(e.getLocalizedMessage()); + } + sb.append("]"); + return sb.toString(); + } // toString + + /** + * Get Status + * @return status info + */ + public String getStatus() + { + if (m_ds == null) + { + return null; + } + + StringBuffer sb = new StringBuffer(); + try + { + sb.append("# Connections: ").append(m_ds.getNumConnections()); + sb.append(" , # Busy Connections: ").append(m_ds.getNumBusyConnections()); + sb.append(" , # Idle Connections: ").append(m_ds.getNumIdleConnections()); + sb.append(" , # Orphaned Connections: ").append(m_ds.getNumUnclosedOrphanedConnections()); + } + catch (Exception e) + {} + return sb.toString(); + } // getStatus - /** - * Check and generate an alternative SQL - * @reExNo number of re-execution - * @msg previous execution error message - * @sql previous executed SQL - * @return String, the alternative SQL, null if no alternative - */ - public String getAlternativeSQL(int reExNo, String msg, String sql) - { - //check reExNo or based on reExNo to do a decision. Currently none - - 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"; - String myString = ""; - for (int i = 0; i < 99; i++) - myString += myString1 + (char)('a'+i) + "\n"; - System.out.println(myString.length()); - System.out.println(Util.size(myString)); - // - myString = Util.trimSize(myString, 2000); - System.out.println(myString.length()); - System.out.println(Util.size(myString)); - // - 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); // NVARCHAR2 column - pstmt.setString(2, myString); // 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); + /************************************************************************** + * Convert an individual Oracle Style statements to target database statement syntax. + * @param oraStatement oracle statement + * @return converted Statement oracle statement + */ + public String convertStatement (String oraStatement) + { + return oraStatement; + } // convertStatement - 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 + /** + * Check if DBMS support the sql statement + * @sql SQL statement + * @return true: yes + */ + public boolean isSupported(String sql) + { + return true; + //jz temp, modify later + } + + + + /** + * Get constraint type associated with the index + * @tableName table name + * @IXName Index name + * @return String[0] = 0: do not know, 1: Primary Key 2: Foreign Key + * String[1] - String[n] = Constraint Name + */ + public String getConstraintType(Connection conn, String tableName, String IXName) + { + if (IXName == null || IXName.length()==0) + return "0"; + if (IXName.endsWith("_KEY")) + return "1"+IXName; + else + return "0"; + //jz temp, modify later from user.constraints + } + + /** + * Get Name of System User + * @return system + */ + public String getSystemUser() + { + return "system"; + } // getSystemUser + + /** + * Get Name of System Database + * @param databaseName database Name + * @return e.g. master or database Name + */ + public String getSystemDatabase(String databaseName) + { + return databaseName; + } // getSystemDatabase + + + /** + * Create SQL TO Date String from Timestamp + * + * @param time Date to be converted + * @param dayOnly true if time set to 00:00:00 + * + * @return TO_DATE('2001-01-30 18:10:20',''YYYY-MM-DD HH24:MI:SS') + * or TO_DATE('2001-01-30',''YYYY-MM-DD') + */ + public String TO_DATE (Timestamp time, boolean dayOnly) + { + if (time == null) + { + if (dayOnly) + return "TRUNC(SysDate)"; + return "SysDate"; + } + + StringBuffer dateString = new StringBuffer("TO_DATE('"); + // YYYY-MM-DD HH24:MI:SS.mmmm JDBC Timestamp format + String myDate = time.toString(); + if (dayOnly) + { + dateString.append(myDate.substring(0,10)); + dateString.append("','YYYY-MM-DD')"); + } + else + { + dateString.append(myDate.substring(0, myDate.indexOf('.'))); // cut off miliseconds + dateString.append("','YYYY-MM-DD HH24:MI:SS')"); + } + return dateString.toString(); + } // TO_DATE + + /** + * Create SQL for formatted Date, Number + * + * @param columnName the column name in the SQL + * @param displayType Display Type + * @param AD_Language 6 character language setting (from Env.LANG_*) + * + * @return TRIM(TO_CHAR(columnName,'999G999G999G990D00','NLS_NUMERIC_CHARACTERS='',.''')) + * or TRIM(TO_CHAR(columnName,'TM9')) depending on DisplayType and Language + * @see org.compiere.util.DisplayType + * @see org.compiere.util.Env + * + * */ + public String TO_CHAR (String columnName, int displayType, String AD_Language) + { + StringBuffer retValue = new StringBuffer("TRIM(TO_CHAR("); + retValue.append(columnName); + + // Numbers + if (DisplayType.isNumeric(displayType)) + { + if (displayType == DisplayType.Amount) + retValue.append(",'999G999G999G990D00'"); + else + retValue.append(",'TM9'"); + // TO_CHAR(GrandTotal,'999G999G999G990D00','NLS_NUMERIC_CHARACTERS='',.''') + if (!Language.isDecimalPoint(AD_Language)) // reversed + retValue.append(",'NLS_NUMERIC_CHARACTERS='',.'''"); + } + else if (DisplayType.isDate(displayType)) + { + retValue.append(",'") + .append(Language.getLanguage(AD_Language).getDBdatePattern()) + .append("'"); + } + retValue.append("))"); + // + return retValue.toString(); + } // TO_CHAR + + /** + * Return number as string for INSERT statements with correct precision + * @param number number + * @param displayType display Type + * @return number as string + */ + public String TO_NUMBER (BigDecimal number, int displayType) + { + if (number == null) + return "NULL"; + BigDecimal result = number; + int scale = DisplayType.getDefaultPrecision(displayType); + if (scale > number.scale()) + { + try + { + result = number.setScale(scale, BigDecimal.ROUND_HALF_UP); + } + catch (Exception e) + { + // log.severe("Number=" + number + ", Scale=" + " - " + e.getMessage()); + } + } + return result.toString(); + } // TO_NUMBER + + + /** + * Get SQL Commands. + * The following variables are resolved: + * @SystemPassword@, @AdempiereUser@, @AdempierePassword@ + * @SystemPassword@, @DatabaseName@, @DatabaseDevice@ + * @param cmdType CMD_* + * @return array of commands to be executed + */ + public String[] getCommands (int cmdType) + { + if (CMD_CREATE_USER == cmdType) + return new String[] + { + + }; + // + if (CMD_CREATE_DATABASE == cmdType) + return new String[] + { + + }; + // + if (CMD_DROP_DATABASE == cmdType) + return new String[] + { + + }; + // + return null; + } // getCommands + + /** + * Create DataSource + * @param connection connection + * @return data dource + */ + public DataSource getDataSource(CConnection connection) + { + if (m_ds != null) + return m_ds; + + try + { + System.setProperty("com.mchange.v2.log.MLog", "com.mchange.v2.log.FallbackMLog"); + ComboPooledDataSource cpds = new ComboPooledDataSource(); + cpds.setDataSourceName("AdempiereDS"); + cpds.setDriverClass(DRIVER); + //loads the jdbc driver + cpds.setJdbcUrl(getConnectionURL(connection)); + cpds.setUser(connection.getDbUid()); + cpds.setPassword(connection.getDbPwd()); + cpds.setPreferredTestQuery(DEFAULT_CONN_TEST_SQL); + cpds.setIdleConnectionTestPeriod(120); + cpds.setAcquireRetryAttempts(5); + //cpds.setCheckoutTimeout(60); + + if (Ini.isClient()) + { + cpds.setInitialPoolSize(1); + cpds.setMinPoolSize(1); + cpds.setMaxPoolSize(15); + cpds.setMaxIdleTimeExcessConnections(1200); + cpds.setMaxIdleTime(600); + m_maxbusyconnections = 12; + } + else + { + cpds.setInitialPoolSize(10); + cpds.setMinPoolSize(5); + cpds.setMaxPoolSize(150); + cpds.setMaxIdleTimeExcessConnections(1200); + cpds.setMaxIdleTime(900); + m_maxbusyconnections = 120; + } + + cpds.setUnreturnedConnectionTimeout(1200); + cpds.setDebugUnreturnedConnectionStackTraces(true); + + m_ds = cpds; + } + catch (Exception ex) + { + m_ds = null; + log.log(Level.SEVERE, "Could not initialise C3P0 Datasource", ex); + } + + return m_ds; + } // getDataSource + + + /** + * Get Cached Connection + * @param connection info + * @param autoCommit true if autocommit connection + * @param transactionIsolation Connection transaction level + * @return connection or null + * @throws Exception + */ + public Connection getCachedConnection (CConnection connection, + boolean autoCommit, int transactionIsolation) + throws Exception + { + Connection conn = null; + Exception exception = null; + try + { + if (m_ds == null) + getDataSource(connection); + + + // Properties connAttr = new Properties(); + // connAttr.setProperty("TRANSACTION_ISOLATION", CConnection.getTransactionIsolationInfo(transactionIsolation)); + // OracleConnection conn = (OracleConnection)m_ds.getConnection(connAttr); + // + // Try 5 times max + for (int i = 0; i < 5; i++) + { + try + { + conn = (Connection)m_ds.getConnection(); + if (conn != null) + { + if (conn.getTransactionIsolation() != transactionIsolation) + conn.setTransactionIsolation(transactionIsolation); + if (conn.getAutoCommit() != autoCommit) + conn.setAutoCommit(autoCommit); +// conn.setDefaultRowPrefetch(20); // 10 default - reduces round trips + } + } + catch (Exception e) + { + exception = e; + conn = null; + if (e instanceof SQLException + && ((SQLException)e).getErrorCode() == 1017) // invalid username/password + { + log.severe("Cannot connect to database: " + + getConnectionURL(connection) + + " - UserID=" + connection.getDbUid()); + break; + } + } + try + { + if (conn != null && conn.isClosed()) + conn = null; + // OK + if (conn != null && !conn.isClosed()) + break; + if (i == 0) + Thread.yield(); // give some time + else + Thread.sleep(100); + } + catch (Exception e) + { + exception = e; + conn = null; + } + } // 5 tries + + if (conn == null && exception != null) + { + log.log(Level.SEVERE, exception.toString()); + log.fine(toString()); +// log.finest("Reference=" + m_ds.getReference()); + } + // else + // { + // System.out.println(conn + " " + getStatus()); + // conn.registerConnectionCacheCallback(this, "test", OracleConnection.ALL_CONNECTION_CALLBACKS); + // } + } + catch (Exception e) + { + // System.err.println ("DB_Oracle.getCachedConnection"); + // if (!(e instanceof SQLException)) + // e.printStackTrace(); + exception = e; + } + + try + { + int numConnections = m_ds.getNumBusyConnections(); + //if (numConnections > m_maxbusyconnections) + if(numConnections % 10 == 0) + { + log.warning(getStatus()); + } + } + catch (Exception ex) + { + + } + if (exception != null) + throw exception; + return conn; + } // getCachedConnection + + /** + * Get Connection from Driver + * @param connection info + * @return connection or null + * @throws SQLException + */ + public Connection getDriverConnection (CConnection connection) throws SQLException + { + getDriver(); + return DriverManager.getConnection (getConnectionURL (connection), + connection.getDbUid(), connection.getDbPwd()); + } // getDriverConnection + + /** + * Get Driver Connection + * @param dbUrl URL + * @param dbUid user + * @param dbPwd password + * @return connection + * @throws SQLException + */ + public Connection getDriverConnection (String dbUrl, String dbUid, String dbPwd) + throws SQLException + { + getDriver(); + return DriverManager.getConnection (dbUrl, dbUid, dbPwd); + } // getDriverConnection + + /** + * Close + */ + public void close() + { + log.config(toString()); + if (m_ds != null) + { + try + { + m_ds.close(); + } + catch (Exception e) + { + log.log(Level.SEVERE, "Could not close Data Source"); + } + } + m_ds = null; + } // close + + /** + * Clean up + */ + public void cleanup() + { + + } // cleanup + + /** + * Get Data Type + * @param columnName + * @param displayType display type + * @param precision precision + * @param defaultValue if true adds default value + * @return data type + */ + public String getDataType (String columnName, int displayType, int precision, + boolean defaultValue) + { + String retValue = null; + //handle special case, bug [ 1618423 ] + if (columnName != null) + { + if (displayType == DisplayType.Button + && columnName.endsWith("_ID")) + { + retValue = "NUMBER(10)"; + } + } + if (retValue == null) + { + switch (displayType) + { + // IDs + case DisplayType.Account: + case DisplayType.Assignment: + case DisplayType.Color: + case DisplayType.ID: + case DisplayType.Location: + case DisplayType.Locator: + case DisplayType.PAttribute: + case DisplayType.Search: + case DisplayType.Table: + case DisplayType.TableDir: + case DisplayType.Image: + retValue = "NUMBER(10)"; + break; + + // Dynamic Precision + case DisplayType.Amount: + retValue = "NUMBER"; + if (defaultValue) + retValue += " DEFAULT 0"; + break; + + case DisplayType.Binary: + retValue = "BLOB"; + break; + + case DisplayType.Button: + retValue = "CHAR(1)"; + break; + + // Number Dynamic Precision + case DisplayType.CostPrice: + retValue = "NUMBER"; + if (defaultValue) + retValue += " DEFAULT 0"; + break; + + // Date + case DisplayType.Date: + case DisplayType.DateTime: + case DisplayType.Time: + retValue = "DATE"; + if (defaultValue) + retValue += " DEFAULT SYSDATE"; + break; + + // Number(10) + case DisplayType.Integer: + retValue = "NUMBER(10)"; + break; + + case DisplayType.List: + retValue = "CHAR(" + precision + ")"; + break; + + // NVARCHAR + case DisplayType.Memo: + case DisplayType.String: + case DisplayType.Text: + retValue = "NVARCHAR(" + precision + ")"; + break; + + case DisplayType.TextLong: + retValue = "CLOB"; + break; + + // Dyn Prec + case DisplayType.Quantity: + retValue = "NUMBER"; + break; + + case DisplayType.YesNo: + retValue = "CHAR(1)"; + break; + + default: + log.severe("Unknown: " + displayType); + break; + } + } + return retValue; + } // getDataType + + + /** + * Check and generate an alternative SQL + * @reExNo number of re-execution + * @msg previous execution error message + * @sql previous executed SQL + * @return String, the alternative SQL, null if no alternative + */ + public String getAlternativeSQL(int reExNo, String msg, String sql) + { + //check reExNo or based on reExNo to do a decision. Currently none + + 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"; + String myString = ""; + for (int i = 0; i < 99; i++) + myString += myString1 + (char)('a'+i) + "\n"; + System.out.println(myString.length()); + System.out.println(Util.size(myString)); + // + myString = Util.trimSize(myString, 2000); + System.out.println(myString.length()); + System.out.println(Util.size(myString)); + // + 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); // NVARCHAR2 column + pstmt.setString(2, myString); // 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; + } + - public Convert getConvert() { - return m_convert; - } - - } // DB_Oracle diff --git a/base/src/org/compiere/db/DB_PostgreSQL.java b/base/src/org/compiere/db/DB_PostgreSQL.java index 19fdd5a0fd..34d3569215 100755 --- a/base/src/org/compiere/db/DB_PostgreSQL.java +++ b/base/src/org/compiere/db/DB_PostgreSQL.java @@ -1,691 +1,762 @@ -/****************************************************************************** - * Product: Adempiere ERP & CRM Smart Business Solution * - * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. * - * This program is free software; you can redistribute it and/or modify it * - * under the terms version 2 of the GNU General Public License as published * - * by the Free Software Foundation. This program is distributed in the hope * - * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * - * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * - * See the GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along * - * with this program; if not, write to the Free Software Foundation, Inc., * - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * - * For the text or an alternative of this public license, you may reach us * - * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA * - * or via info@compiere.org or http://www.compiere.org/license.html * - * Portions created by Victor Perez are Copyright (C) 1999-2005 e-Evolution,S.C - * Contributor(s): Victor Perez * - *****************************************************************************/ -package org.compiere.db; - -import java.math.BigDecimal; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.sql.Timestamp; -import java.util.logging.Level; - -import javax.sql.ConnectionPoolDataSource; -import javax.sql.DataSource; -import javax.sql.RowSet; - -import org.compiere.dbPort.Convert; -import org.compiere.dbPort.Convert_PostgreSQL; -import org.compiere.util.CLogger; -import org.compiere.util.DisplayType; - -/** - * PostgreSQL Database Port - * - * @author @author Jorg Janke, Victor P�rez - * @version $Id: DB_PostgreSQL.java,v 1.23 2005/03/11 20:29:01 jjanke Exp $ - */ -public class DB_PostgreSQL implements AdempiereDatabase -{ - public Convert getConvert() { - return m_convert; - } - - /** - * PostgreSQL Database - */ - public DB_PostgreSQL() - { - } // DB_PostgreSQL - - /** Driver */ - private org.postgresql.Driver s_driver = null; - - /** Default Port */ - public static final int DEFAULT_PORT = 5432; - - /** Data Source */ - private org.postgresql.ds.PGPoolingDataSource m_ds = null; - - /** Statement Converter */ - private Convert_PostgreSQL m_convert = new Convert_PostgreSQL(); - /** Connection String */ - private String m_connection; - /** Cached Database Name */ - private String m_dbName = null; - - private String m_userName = null; - /** Connection String */ - private String m_connectionURL; - - private boolean m_supportAlias = false; - - /** Logger */ - private static CLogger log = CLogger.getCLogger (DB_PostgreSQL.class); - - /** - * Get Database Name - * @return database short name - */ - public String getName() - { - return Database.DB_POSTGRESQL; - } // getName - - /** - * Get Database Description - * @return database long name and version - */ - public String getDescription() - { //begin vpj-cd e-evolution 30.09.2005 - //return s_driver.toString(); - try - { - if (s_driver == null) - getDriver(); - } - catch (Exception e) - { - } - if (s_driver != null) - return s_driver.toString(); - return "No Driver"; - //end vpj-cd e-evolution 30.09.2005 - } // getDescription - - /** - * Get Standard JDBC Port - * @return standard port - */ - public int getStandardPort() - { - return DEFAULT_PORT; - } // getStandardPort - - /** - * Get and register Database Driver - * @return Driver - */ - public java.sql.Driver getDriver() throws SQLException - { - if (s_driver == null) - { - s_driver = new org.postgresql.Driver(); - DriverManager.registerDriver (s_driver); - DriverManager.setLoginTimeout (Database.CONNECTION_TIMEOUT); - } - return s_driver; - } // getDriver - - /** - * Get Database Connection String. - * Requirements: - * - createdb -E UNICODE compiere - * @param connection Connection Descriptor - * @return connection String - */ - public String getConnectionURL (CConnection connection) - { - // jdbc:postgresql://hostname:portnumber/databasename?encoding=UNICODE - StringBuffer sb = new StringBuffer("jdbc:postgresql:"); - sb.append("//").append(connection.getDbHost()) - .append(":").append(connection.getDbPort()) - .append("/").append(connection.getDbName()) - .append("?encoding=UNICODE"); - m_connection = sb.toString(); - return m_connection; - } // getConnectionString - - /** - * Get Connection URL - * @param dbHost db Host - * @param dbPort db Port - * @param dbName sb Name - * @param userName user name - * @return connection url - */ - public String getConnectionURL (String dbHost, int dbPort, String dbName, - String userName) - { - return "jdbc:postgresql://" - + dbHost + ":" + dbPort + "/" + dbName; - } // getConnectionURL - - /** - * Get Database Connection String - * @param connectionURL Connection URL - * @param userName user name - * @return connection String - */ - public String getConnectionURL (String connectionURL, String userName) - { - m_userName = userName; - m_connectionURL = connectionURL; - return m_connectionURL; - } // getConnectionURL - - /** - * Get JDBC Catalog - * @return catalog (database name) - */ - public String getCatalog() - { - if (m_dbName != null) - return m_dbName; - // log.severe("Database Name not set (yet) - call getConnectionURL first"); - return null; - } // getCatalog - - /** - * Get JDBC Schema - * @return schema (dbo) - */ - public String getSchema() - { - //begin vpj-cd e-evolution 03/04/2005 - return "adempiere"; - //end vpj-cd e-evolution 03/04/2005 - } // getSchema - - /** - * Supports BLOB - * @return true if BLOB is supported - */ - public boolean supportsBLOB() - { - return true; - } // supportsBLOB - - /** - * String Representation - * @return info - */ - public String toString() - { - StringBuffer sb = new StringBuffer("DB_PostgreSQL["); - sb.append(m_connection) - .append("]"); - return sb.toString(); - } // toString - - /** - * Get Status - * @return status info - */ - public String getStatus() - { - return ""; - } // getStatus - - /************************************************************************* - * Convert an individual Oracle Style statements to target database statement syntax - * - * @param oraStatement - * @return converted Statement - * @throws Exception - */ - public String convertStatement (String oraStatement) - { - String retValue[] = m_convert.convert(oraStatement); - - //begin vpj-cd e-evolution 03/14/2005 - if (retValue.length == 0 ) - return oraStatement; - //end vpj-cd e-evolution 03/14/2005 - - if (retValue == null) - //begin vpj-cd 24/06/2005 e-evolution - { - log.log(Level.SEVERE,("DB_PostgreSQL.convertStatement - Not Converted (" + oraStatement + ") - " - + m_convert.getConversionError())); - throw new IllegalArgumentException - ("DB_PostgreSQL.convertStatement - Not Converted (" + oraStatement + ") - " - + m_convert.getConversionError()); - } - // end vpj-cd 24/06/2005 e-evolution - if (retValue.length != 1) - //begin vpj-cd 24/06/2005 e-evolution - { - log.log(Level.SEVERE, ("DB_PostgreSQL.convertStatement - Convert Command Number=" + retValue.length - + " (" + oraStatement + ") - " + m_convert.getConversionError())); - throw new IllegalArgumentException - ("DB_PostgreSQL.convertStatement - Convert Command Number=" + retValue.length - + " (" + oraStatement + ") - " + m_convert.getConversionError()); - } - //end vpj-cd 24/06/2005 e-evolution - // Diagnostics (show changed, but not if AD_Error - if (!oraStatement.equals(retValue[0]) && retValue[0].indexOf("AD_Error") == -1) - //begin vpj-cd 24/06/2005 e-evolution - //System.out.println("PostgreSQL =>" + retValue[0] + "<= <" + oraStatement + ">"); - log.log(Level.FINER, "PostgreSQL =>" + retValue[0] + "<= <" + oraStatement + ">"); - //end vpj-cd 24/06/2005 e-evolution - // - return retValue[0]; - } // convertStatement - - - /** - * Get Name of System User - * @return e.g. sa, system - */ - public String getSystemUser() - { - return "postgres"; - } // getSystemUser - - /** - * Get Name of System Database - * @param databaseName database Name - * @return e.g. master or database Name - */ - public String getSystemDatabase(String databaseName) - { - return "template1"; - } // getSystemDatabase - - - /** - * Create SQL TO Date String from Timestamp - * - * @param time Date to be converted - * @param dayOnly true if time set to 00:00:00 - * - * @return TO_DATE('2001-01-30 18:10:20',''YYYY-MM-DD HH24:MI:SS') - * or TO_DATE('2001-01-30',''YYYY-MM-DD') - */ - public String TO_DATE (Timestamp time, boolean dayOnly) - { - if (time == null) - { - if (dayOnly) - return "current_date()"; - return "current_date()"; - } - - StringBuffer dateString = new StringBuffer("TO_DATE('"); - // YYYY-MM-DD HH24:MI:SS.mmmm JDBC Timestamp format - String myDate = time.toString(); - if (dayOnly) - { - dateString.append(myDate.substring(0,10)); - dateString.append("','YYYY-MM-DD')"); - } - else - { - dateString.append(myDate.substring(0, myDate.indexOf('.'))); // cut off miliseconds - dateString.append("','YYYY-MM-DD HH24:MI:SS')"); - } - return dateString.toString(); - } // TO_DATE - - /** - * Create SQL for formatted Date, Number - * - * @param columnName the column name in the SQL - * @param displayType Display Type - * @param AD_Language 6 character language setting (from Env.LANG_*) - * - * @return TRIM(TO_CHAR(columnName,'999G999G999G990D00','NLS_NUMERIC_CHARACTERS='',.''')) - * or TRIM(TO_CHAR(columnName,'TM9')) depending on DisplayType and Language - * @see org.compiere.util.DisplayType - * @see org.compiere.util.Env - * - **/ - public String TO_CHAR (String columnName, int displayType, String AD_Language) - { - StringBuffer retValue = new StringBuffer("CAST ("); - retValue.append(columnName); - retValue.append(" AS Text)"); - - // Numbers - /* - if (DisplayType.isNumeric(displayType)) - { - if (displayType == DisplayType.Amount) - retValue.append(" AS TEXT"); - else - retValue.append(" AS TEXT"); - //if (!Language.isDecimalPoint(AD_Language)) // reversed - //retValue.append(",'NLS_NUMERIC_CHARACTERS='',.'''"); - } - else if (DisplayType.isDate(displayType)) - { - retValue.append(",'") - .append(Language.getLanguage(AD_Language).getDBdatePattern()) - .append("'"); - } - retValue.append(")"); - //*/ - return retValue.toString(); - } // TO_CHAR - - /** - * Return number as string for INSERT statements with correct precision - * @param number number - * @param displayType display Type - * @return number as string - */ - public String TO_NUMBER (BigDecimal number, int displayType) - { - if (number == null) - return "NULL"; - BigDecimal result = number; - int scale = DisplayType.getDefaultPrecision(displayType); - if (scale > number.scale()) - { - try - { - result = number.setScale(scale, BigDecimal.ROUND_HALF_UP); - } - catch (Exception e) - { - // log.severe("Number=" + number + ", Scale=" + " - " + e.getMessage()); - } - } - return result.toString(); - } // TO_NUMBER - - - /** - * Get SQL Commands - * @param cmdType CMD_* - * @return array of commands to be executed - */ - public String[] getCommands (int cmdType) - { - if (CMD_CREATE_USER == cmdType) - return new String[] - { - "CREATE USER adempiere;", - }; - // - if (CMD_CREATE_DATABASE == cmdType) - return new String[] - { - "CREATE DATABASE adempiere OWNER adempiere;", - "GRANT ALL PRIVILEGES ON adempiere TO adempiere;" , - "CREATE SCHEMA adempiere;", - "SET search_path TO adempiere;" - }; - // - if (CMD_DROP_DATABASE == cmdType) - return new String[] - { - "DROP DATABASE adempiere;" - }; - // - return null; - } // getCommands - - - /************************************************************************** - * Get RowSet - * @param rs ResultSet - * @return RowSet - * @throws SQLException - */ - public RowSet getRowSet (java.sql.ResultSet rs) throws SQLException - { - throw new UnsupportedOperationException("PostgreSQL does not support RowSets"); - } // getRowSet - - - /** - * Get Cached Connection - * @param connection connection - * @param autoCommit auto commit - * @param transactionIsolation trx isolation - * @return Connection - * @throws Exception - */ - public Connection getCachedConnection (CConnection connection, - boolean autoCommit, int transactionIsolation) - throws Exception - { - if (m_ds == null) - getDataSource(connection); - // - Connection conn = m_ds.getConnection(); - // Connection conn = getDriverConnection(connection); - // - conn.setAutoCommit(autoCommit); - conn.setTransactionIsolation(transactionIsolation); - return conn; - } // getCachedConnection - - - /** - * Create DataSource (Client) - * @param connection connection - * @return data dource - */ - public DataSource getDataSource(CConnection connection) - { - //throw new UnsupportedOperationException("Not supported/implemented"); - if (m_ds != null) - return m_ds; - - //org.postgresql.ds.PGPoolingDataSource ds = new org.postgresql.ds.PGPoolingDataSource(); - org.postgresql.jdbc3.Jdbc3PoolingDataSource ds = new org.postgresql.jdbc3.Jdbc3PoolingDataSource(); - - ds.setDataSourceName("AdempiereDS"); - ds.setServerName(connection.getDbHost()); - ds.setDatabaseName(connection.getDbName()); - ds.setUser(connection.getDbUid()); - ds.setPassword(connection.getDbPwd()); - ds.setPortNumber(connection.getDbPort()); - ds.setMaxConnections(50); - ds.setInitialConnections(0); - - //new InitialContext().rebind("DataSource", source); - m_ds = ds; - - return m_ds; - } - - /** - * Create Pooled DataSource (Server) - * @param connection connection - * @return data dource - */ - public ConnectionPoolDataSource createPoolDataSource(CConnection connection) - { - throw new UnsupportedOperationException("Not supported/implemented"); - } - - /** - * Get Connection from Driver - * @param connection info - * @return connection or null - */ - public Connection getDriverConnection (CConnection connection) throws SQLException - { - getDriver(); - return DriverManager.getConnection (getConnectionURL (connection), - connection.getDbUid(), connection.getDbPwd()); - } // getDriverConnection - - /** - * Get Driver Connection - * @param dbUrl URL - * @param dbUid user - * @param dbPwd password - * @return connection - * @throws SQLException - */ - public Connection getDriverConnection (String dbUrl, String dbUid, String dbPwd) - throws SQLException - { - getDriver(); - return DriverManager.getConnection (dbUrl, dbUid, dbPwd); - } // getDriverConnection - - - /** - * Close - */ - public void close() - { - - log.config(toString()); - - if (m_ds != null) - { - try - { - m_ds.close(); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - m_ds = null; - } // close - - - /** - * Check and generate an alternative SQL - * @reExNo number of re-execution - * @msg previous execution error message - * @sql previous executed SQL - * @return String, the alternative SQL, null if no alternative - */ - public String getAlternativeSQL(int reExNo, String msg, String sql) - { - return null; //do not do re-execution of alternative SQL - } - - /** - * Get constraint type associated with the index - * @tableName table name - * @IXName Index name - * @return String[0] = 0: do not know, 1: Primary Key 2: Foreign Key - * String[1] - String[n] = Constraint Name - */ - public String getConstraintType(Connection conn, String tableName, String IXName) - { - if (IXName == null || IXName.length()==0) - return "0"; - if (IXName.toUpperCase().endsWith("_KEY")) - return "1"+IXName; - else - return "0"; - //jz temp, modify later from user.constraints - } - - /** - * Check if DBMS support the sql statement - * @sql SQL statement - * @return true: yes - */ - public boolean isSupported(String sql) - { - return true; - //jz temp, modify later - } - - /** - * Dump table lock info to console for current transaction - * @param conn - */ - public static void dumpLocks(Connection conn) - { - Statement stmt = null; - try { - String sql = "select pg_class.relname,pg_locks.* from pg_class,pg_locks where pg_class.relfilenode=pg_locks.relation order by 1"; - stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery(sql); - int cnt = rs.getMetaData().getColumnCount(); - System.out.println(); - while (rs.next()) - { - for(int i = 0; i < cnt; i++) - { - Object value = rs.getObject(i+1); - if (i > 0) - System.out.print(", "); - System.out.print(value != null ? value.toString() : ""); - } - System.out.println(); - } - System.out.println(); - } catch (Exception e) { - - } finally { - try{ - if (stmt != null) - stmt.close(); - } catch (Exception e) {} - } - } - - /** - * Test - * @param args ignored - */ - public static void main(String[] args) - { - DB_PostgreSQL postgresql = new DB_PostgreSQL(); - // - String databaseName = "adempiere"; - String uid = "adempiere"; - String pwd = "adempiere"; - String jdbcURL = postgresql.getConnectionURL("vpj", DEFAULT_PORT, databaseName, uid); - System.out.println(jdbcURL); - try - { - postgresql.getDriver(); - Connection conn = DriverManager.getConnection (jdbcURL, uid, pwd); - - //CachedRowSetImpl crs = null; - //crs = new CachedRowSetImpl(); - //crs.setSyncProvider("com.sun.rowset.providers.RIOptimisticProvider"); - //crs.setConcurrency(ResultSet.CONCUR_READ_ONLY); - //crs.setType(ResultSet.TYPE_SCROLL_INSENSITIVE); - //crs.setCommand("SELECT * FROM AD_Client"); - // - //crs.execute(conn); - // - conn.close(); - conn = null; - } - catch (Exception ex) - { - ex.printStackTrace(); - } - } // main - - /* - public boolean getSupportAlias() - { - - if (s_driver == null) - { - s_driver = new org.postgresql.Driver(); - if(s_driver.getVersion().indexOf("PostgreSQL 8.2") != -1) - return true; - - } - return false; - - }*/ - -} // DB_PostgreSQL +/****************************************************************************** + * Product: Adempiere ERP & CRM Smart Business Solution * + * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + * For the text or an alternative of this public license, you may reach us * + * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA * + * or via info@compiere.org or http://www.compiere.org/license.html * + * Portions created by Victor Perez are Copyright (C) 1999-2005 e-Evolution,S.C + * Contributor(s): Victor Perez * + *****************************************************************************/ +package org.compiere.db; + +import java.math.BigDecimal; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Timestamp; +import java.util.logging.Level; + +import javax.sql.ConnectionPoolDataSource; +import javax.sql.DataSource; +import javax.sql.RowSet; + +import org.compiere.dbPort.Convert; +import org.compiere.dbPort.Convert_PostgreSQL; +import org.compiere.util.CLogger; +import org.compiere.util.DisplayType; +import org.compiere.util.Ini; + +import com.mchange.v2.c3p0.ComboPooledDataSource; + +/** + * PostgreSQL Database Port + * + * @author @author Jorg Janke, Victor P�rez + * @version $Id: DB_PostgreSQL.java,v 1.23 2005/03/11 20:29:01 jjanke Exp $ + * --- + * Modifications: removed static references to database connection and instead always + * get a new connection from database pool manager which manages all connections + * set rw/ro properties for the connection accordingly. + * @author Ashley Ramdass (Posterita) + */ +public class DB_PostgreSQL implements AdempiereDatabase +{ + + public Convert getConvert() { + return m_convert; + } + + /** + * PostgreSQL Database + */ + public DB_PostgreSQL() + { + } // DB_PostgreSQL + + /** Driver */ + private org.postgresql.Driver s_driver = null; + + /** Driver class */ + public static final String DRIVER = "org.postgresql.Driver"; + + /** Default Port */ + public static final int DEFAULT_PORT = 5432; + + /** Data Source */ + private ComboPooledDataSource m_ds = null; + + /** Statement Converter */ + private Convert_PostgreSQL m_convert = new Convert_PostgreSQL(); + /** Connection String */ + private String m_connection; + /** Cached Database Name */ + private String m_dbName = null; + + private String m_userName = null; + /** Connection String */ + private String m_connectionURL; + + private boolean m_supportAlias = false; + + /** Logger */ + private static CLogger log = CLogger.getCLogger (DB_PostgreSQL.class); + + private static int m_maxbusyconnections = 0; + + /** + * Get Database Name + * @return database short name + */ + public String getName() + { + return Database.DB_POSTGRESQL; + } // getName + + /** + * Get Database Description + * @return database long name and version + */ + public String getDescription() + { //begin vpj-cd e-evolution 30.09.2005 + //return s_driver.toString(); + try + { + if (s_driver == null) + getDriver(); + } + catch (Exception e) + { + } + if (s_driver != null) + return s_driver.toString(); + return "No Driver"; + //end vpj-cd e-evolution 30.09.2005 + } // getDescription + + /** + * Get Standard JDBC Port + * @return standard port + */ + public int getStandardPort() + { + return DEFAULT_PORT; + } // getStandardPort + + /** + * Get and register Database Driver + * @return Driver + */ + public java.sql.Driver getDriver() throws SQLException + { + if (s_driver == null) + { + s_driver = new org.postgresql.Driver(); + DriverManager.registerDriver (s_driver); + DriverManager.setLoginTimeout (Database.CONNECTION_TIMEOUT); + } + return s_driver; + } // getDriver + + /** + * Get Database Connection String. + * Requirements: + * - createdb -E UNICODE compiere + * @param connection Connection Descriptor + * @return connection String + */ + public String getConnectionURL (CConnection connection) + { + // jdbc:postgresql://hostname:portnumber/databasename?encoding=UNICODE + StringBuffer sb = new StringBuffer("jdbc:postgresql:"); + sb.append("//").append(connection.getDbHost()) + .append(":").append(connection.getDbPort()) + .append("/").append(connection.getDbName()) + .append("?encoding=UNICODE"); + m_connection = sb.toString(); + return m_connection; + } // getConnectionString + + /** + * Get Connection URL + * @param dbHost db Host + * @param dbPort db Port + * @param dbName sb Name + * @param userName user name + * @return connection url + */ + public String getConnectionURL (String dbHost, int dbPort, String dbName, + String userName) + { + return "jdbc:postgresql://" + + dbHost + ":" + dbPort + "/" + dbName; + } // getConnectionURL + + /** + * Get Database Connection String + * @param connectionURL Connection URL + * @param userName user name + * @return connection String + */ + public String getConnectionURL (String connectionURL, String userName) + { + m_userName = userName; + m_connectionURL = connectionURL; + return m_connectionURL; + } // getConnectionURL + + /** + * Get JDBC Catalog + * @return catalog (database name) + */ + public String getCatalog() + { + if (m_dbName != null) + return m_dbName; + // log.severe("Database Name not set (yet) - call getConnectionURL first"); + return null; + } // getCatalog + + /** + * Get JDBC Schema + * @return schema (dbo) + */ + public String getSchema() + { + //begin vpj-cd e-evolution 03/04/2005 + return "adempiere"; + //end vpj-cd e-evolution 03/04/2005 + } // getSchema + + /** + * Supports BLOB + * @return true if BLOB is supported + */ + public boolean supportsBLOB() + { + return true; + } // supportsBLOB + + /** + * String Representation + * @return info + */ + public String toString() + { + StringBuffer sb = new StringBuffer("DB_PostgreSQL["); + sb.append(m_connectionURL); + try + { + StringBuffer logBuffer = new StringBuffer(50); + logBuffer.append("# Connections: ").append(m_ds.getNumConnections()); + logBuffer.append(" , # Busy Connections: ").append(m_ds.getNumBusyConnections()); + logBuffer.append(" , # Idle Connections: ").append(m_ds.getNumIdleConnections()); + logBuffer.append(" , # Orphaned Connections: ").append(m_ds.getNumUnclosedOrphanedConnections()); + } + catch (Exception e) + { + sb.append("=").append(e.getLocalizedMessage()); + } + sb.append("]"); + return sb.toString(); + } // toString + + /** + * Get Status + * @return status info + */ + public String getStatus() + { + if (m_ds == null) + { + return null; + } + + StringBuffer sb = new StringBuffer(); + try + { + sb.append("# Connections: ").append(m_ds.getNumConnections()); + sb.append(" , # Busy Connections: ").append(m_ds.getNumBusyConnections()); + sb.append(" , # Idle Connections: ").append(m_ds.getNumIdleConnections()); + sb.append(" , # Orphaned Connections: ").append(m_ds.getNumUnclosedOrphanedConnections()); + } + catch (Exception e) + {} + return sb.toString(); + } // getStatus + + /************************************************************************* + * Convert an individual Oracle Style statements to target database statement syntax + * + * @param oraStatement + * @return converted Statement + * @throws Exception + */ + public String convertStatement (String oraStatement) + { + String retValue[] = m_convert.convert(oraStatement); + + //begin vpj-cd e-evolution 03/14/2005 + if (retValue.length == 0 ) + return oraStatement; + //end vpj-cd e-evolution 03/14/2005 + + if (retValue == null) + //begin vpj-cd 24/06/2005 e-evolution + { + log.log(Level.SEVERE,("DB_PostgreSQL.convertStatement - Not Converted (" + oraStatement + ") - " + + m_convert.getConversionError())); + throw new IllegalArgumentException + ("DB_PostgreSQL.convertStatement - Not Converted (" + oraStatement + ") - " + + m_convert.getConversionError()); + } + // end vpj-cd 24/06/2005 e-evolution + if (retValue.length != 1) + //begin vpj-cd 24/06/2005 e-evolution + { + log.log(Level.SEVERE, ("DB_PostgreSQL.convertStatement - Convert Command Number=" + retValue.length + + " (" + oraStatement + ") - " + m_convert.getConversionError())); + throw new IllegalArgumentException + ("DB_PostgreSQL.convertStatement - Convert Command Number=" + retValue.length + + " (" + oraStatement + ") - " + m_convert.getConversionError()); + } + //end vpj-cd 24/06/2005 e-evolution + // Diagnostics (show changed, but not if AD_Error + if (!oraStatement.equals(retValue[0]) && retValue[0].indexOf("AD_Error") == -1) + //begin vpj-cd 24/06/2005 e-evolution + //System.out.println("PostgreSQL =>" + retValue[0] + "<= <" + oraStatement + ">"); + log.log(Level.FINER, "PostgreSQL =>" + retValue[0] + "<= <" + oraStatement + ">"); + //end vpj-cd 24/06/2005 e-evolution + // + return retValue[0]; + } // convertStatement + + + /** + * Get Name of System User + * @return e.g. sa, system + */ + public String getSystemUser() + { + return "postgres"; + } // getSystemUser + + /** + * Get Name of System Database + * @param databaseName database Name + * @return e.g. master or database Name + */ + public String getSystemDatabase(String databaseName) + { + return "template1"; + } // getSystemDatabase + + + /** + * Create SQL TO Date String from Timestamp + * + * @param time Date to be converted + * @param dayOnly true if time set to 00:00:00 + * + * @return TO_DATE('2001-01-30 18:10:20',''YYYY-MM-DD HH24:MI:SS') + * or TO_DATE('2001-01-30',''YYYY-MM-DD') + */ + public String TO_DATE (Timestamp time, boolean dayOnly) + { + if (time == null) + { + if (dayOnly) + return "current_date()"; + return "current_date()"; + } + + StringBuffer dateString = new StringBuffer("TO_DATE('"); + // YYYY-MM-DD HH24:MI:SS.mmmm JDBC Timestamp format + String myDate = time.toString(); + if (dayOnly) + { + dateString.append(myDate.substring(0,10)); + dateString.append("','YYYY-MM-DD')"); + } + else + { + dateString.append(myDate.substring(0, myDate.indexOf('.'))); // cut off miliseconds + dateString.append("','YYYY-MM-DD HH24:MI:SS')"); + } + return dateString.toString(); + } // TO_DATE + + /** + * Create SQL for formatted Date, Number + * + * @param columnName the column name in the SQL + * @param displayType Display Type + * @param AD_Language 6 character language setting (from Env.LANG_*) + * + * @return TRIM(TO_CHAR(columnName,'999G999G999G990D00','NLS_NUMERIC_CHARACTERS='',.''')) + * or TRIM(TO_CHAR(columnName,'TM9')) depending on DisplayType and Language + * @see org.compiere.util.DisplayType + * @see org.compiere.util.Env + * + **/ + public String TO_CHAR (String columnName, int displayType, String AD_Language) + { + StringBuffer retValue = new StringBuffer("CAST ("); + retValue.append(columnName); + retValue.append(" AS Text)"); + + // Numbers + /* + if (DisplayType.isNumeric(displayType)) + { + if (displayType == DisplayType.Amount) + retValue.append(" AS TEXT"); + else + retValue.append(" AS TEXT"); + //if (!Language.isDecimalPoint(AD_Language)) // reversed + //retValue.append(",'NLS_NUMERIC_CHARACTERS='',.'''"); + } + else if (DisplayType.isDate(displayType)) + { + retValue.append(",'") + .append(Language.getLanguage(AD_Language).getDBdatePattern()) + .append("'"); + } + retValue.append(")"); + //*/ + return retValue.toString(); + } // TO_CHAR + + /** + * Return number as string for INSERT statements with correct precision + * @param number number + * @param displayType display Type + * @return number as string + */ + public String TO_NUMBER (BigDecimal number, int displayType) + { + if (number == null) + return "NULL"; + BigDecimal result = number; + int scale = DisplayType.getDefaultPrecision(displayType); + if (scale > number.scale()) + { + try + { + result = number.setScale(scale, BigDecimal.ROUND_HALF_UP); + } + catch (Exception e) + { + // log.severe("Number=" + number + ", Scale=" + " - " + e.getMessage()); + } + } + return result.toString(); + } // TO_NUMBER + + + /** + * Get SQL Commands + * @param cmdType CMD_* + * @return array of commands to be executed + */ + public String[] getCommands (int cmdType) + { + if (CMD_CREATE_USER == cmdType) + return new String[] + { + "CREATE USER adempiere;", + }; + // + if (CMD_CREATE_DATABASE == cmdType) + return new String[] + { + "CREATE DATABASE adempiere OWNER adempiere;", + "GRANT ALL PRIVILEGES ON adempiere TO adempiere;" , + "CREATE SCHEMA adempiere;", + "SET search_path TO adempiere;" + }; + // + if (CMD_DROP_DATABASE == cmdType) + return new String[] + { + "DROP DATABASE adempiere;" + }; + // + return null; + } // getCommands + + + /************************************************************************** + * Get RowSet + * @param rs ResultSet + * @return RowSet + * @throws SQLException + */ + public RowSet getRowSet (java.sql.ResultSet rs) throws SQLException + { + throw new UnsupportedOperationException("PostgreSQL does not support RowSets"); + } // getRowSet + + + /** + * Get Cached Connection + * @param connection connection + * @param autoCommit auto commit + * @param transactionIsolation trx isolation + * @return Connection + * @throws Exception + */ + public Connection getCachedConnection (CConnection connection, + boolean autoCommit, int transactionIsolation) + throws Exception + { + if (m_ds == null) + getDataSource(connection); + // + Connection conn = m_ds.getConnection(); + // Connection conn = getDriverConnection(connection); + // + conn.setAutoCommit(autoCommit); + conn.setTransactionIsolation(transactionIsolation); + return conn; + } // getCachedConnection + + + /** + * Create DataSource (Client) + * @param connection connection + * @return data dource + */ + public DataSource getDataSource(CConnection connection) + { + //throw new UnsupportedOperationException("Not supported/implemented"); + if (m_ds != null) + return m_ds; + + try + { + System.setProperty("com.mchange.v2.log.MLog", "com.mchange.v2.log.FallbackMLog"); + ComboPooledDataSource cpds = new ComboPooledDataSource(); + cpds.setDataSourceName("AdempiereDS"); + cpds.setDriverClass(DRIVER); + //loads the jdbc driver + cpds.setJdbcUrl(getConnectionURL(connection)); + cpds.setUser(connection.getDbUid()); + cpds.setPassword(connection.getDbPwd()); + cpds.setPreferredTestQuery(DEFAULT_CONN_TEST_SQL); + cpds.setIdleConnectionTestPeriod(120); + cpds.setAcquireRetryAttempts(5); + //cpds.setCheckoutTimeout(60); + + if (Ini.isClient()) + { + cpds.setInitialPoolSize(1); + cpds.setMinPoolSize(1); + cpds.setMaxPoolSize(15); + cpds.setMaxIdleTimeExcessConnections(1200); + cpds.setMaxIdleTime(600); + m_maxbusyconnections = 12; + } + else + { + cpds.setInitialPoolSize(10); + cpds.setMinPoolSize(5); + cpds.setMaxPoolSize(150); + cpds.setMaxIdleTimeExcessConnections(1200); + cpds.setMaxIdleTime(900); + m_maxbusyconnections = 120; + } + + cpds.setUnreturnedConnectionTimeout(1200); + cpds.setDebugUnreturnedConnectionStackTraces(true); + + m_ds = cpds; + } + catch (Exception ex) + { + m_ds = null; + log.log(Level.SEVERE, "Could not initialise C3P0 Datasource", ex); + } + + return m_ds; + } + + /** + * Create Pooled DataSource (Server) + * @param connection connection + * @return data dource + */ + public ConnectionPoolDataSource createPoolDataSource(CConnection connection) + { + throw new UnsupportedOperationException("Not supported/implemented"); + } + + /** + * Get Connection from Driver + * @param connection info + * @return connection or null + */ + public Connection getDriverConnection (CConnection connection) throws SQLException + { + getDriver(); + return DriverManager.getConnection (getConnectionURL (connection), + connection.getDbUid(), connection.getDbPwd()); + } // getDriverConnection + + /** + * Get Driver Connection + * @param dbUrl URL + * @param dbUid user + * @param dbPwd password + * @return connection + * @throws SQLException + */ + public Connection getDriverConnection (String dbUrl, String dbUid, String dbPwd) + throws SQLException + { + getDriver(); + return DriverManager.getConnection (dbUrl, dbUid, dbPwd); + } // getDriverConnection + + + /** + * Close + */ + public void close() + { + + log.config(toString()); + + if (m_ds != null) + { + try + { + m_ds.close(); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + m_ds = null; + } // close + + + /** + * Check and generate an alternative SQL + * @reExNo number of re-execution + * @msg previous execution error message + * @sql previous executed SQL + * @return String, the alternative SQL, null if no alternative + */ + public String getAlternativeSQL(int reExNo, String msg, String sql) + { + return null; //do not do re-execution of alternative SQL + } + + /** + * Get constraint type associated with the index + * @tableName table name + * @IXName Index name + * @return String[0] = 0: do not know, 1: Primary Key 2: Foreign Key + * String[1] - String[n] = Constraint Name + */ + public String getConstraintType(Connection conn, String tableName, String IXName) + { + if (IXName == null || IXName.length()==0) + return "0"; + if (IXName.toUpperCase().endsWith("_KEY")) + return "1"+IXName; + else + return "0"; + //jz temp, modify later from user.constraints + } + + /** + * Check if DBMS support the sql statement + * @sql SQL statement + * @return true: yes + */ + public boolean isSupported(String sql) + { + return true; + //jz temp, modify later + } + + /** + * Dump table lock info to console for current transaction + * @param conn + */ + public static void dumpLocks(Connection conn) + { + Statement stmt = null; + try { + String sql = "select pg_class.relname,pg_locks.* from pg_class,pg_locks where pg_class.relfilenode=pg_locks.relation order by 1"; + stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(sql); + int cnt = rs.getMetaData().getColumnCount(); + System.out.println(); + while (rs.next()) + { + for(int i = 0; i < cnt; i++) + { + Object value = rs.getObject(i+1); + if (i > 0) + System.out.print(", "); + System.out.print(value != null ? value.toString() : ""); + } + System.out.println(); + } + System.out.println(); + } catch (Exception e) { + + } finally { + try{ + if (stmt != null) + stmt.close(); + } catch (Exception e) {} + } + } + + /** + * Test + * @param args ignored + */ + public static void main(String[] args) + { + DB_PostgreSQL postgresql = new DB_PostgreSQL(); + // + String databaseName = "adempiere"; + String uid = "adempiere"; + String pwd = "adempiere"; + String jdbcURL = postgresql.getConnectionURL("vpj", DEFAULT_PORT, databaseName, uid); + System.out.println(jdbcURL); + try + { + postgresql.getDriver(); + Connection conn = DriverManager.getConnection (jdbcURL, uid, pwd); + + //CachedRowSetImpl crs = null; + //crs = new CachedRowSetImpl(); + //crs.setSyncProvider("com.sun.rowset.providers.RIOptimisticProvider"); + //crs.setConcurrency(ResultSet.CONCUR_READ_ONLY); + //crs.setType(ResultSet.TYPE_SCROLL_INSENSITIVE); + //crs.setCommand("SELECT * FROM AD_Client"); + // + //crs.execute(conn); + // + conn.close(); + conn = null; + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } // main + + /* + public boolean getSupportAlias() + { + + if (s_driver == null) + { + s_driver = new org.postgresql.Driver(); + if(s_driver.getVersion().indexOf("PostgreSQL 8.2") != -1) + return true; + + } + return false; + + }*/ + +} // DB_PostgreSQL diff --git a/base/src/org/compiere/model/MSequence.java b/base/src/org/compiere/model/MSequence.java index d9a70292a9..33a803164b 100644 --- a/base/src/org/compiere/model/MSequence.java +++ b/base/src/org/compiere/model/MSequence.java @@ -16,9 +16,13 @@ *****************************************************************************/ package org.compiere.model; +import java.rmi.RemoteException; import java.sql.*; import java.util.*; import java.util.logging.*; + +import org.compiere.db.CConnection; +import org.compiere.interfaces.Server; import org.compiere.util.*; /** @@ -37,6 +41,7 @@ public class MSequence extends X_AD_Sequence /** Log Level for Next ID Call */ private static final Level LOGLEVEL = Level.ALL; + /** * Get next number for Key column = 0 is Error. * @param AD_Client_ID client @@ -48,6 +53,30 @@ public class MSequence extends X_AD_Sequence { if (TableName == null || TableName.length() == 0) throw new IllegalArgumentException("TableName missing"); + + //get from server + if (DB.isRemoteObjects()) + { + Server server = CConnection.get().getServer(); + try + { + if (server != null) + { // See ServerBean + int id = server.getNextID(AD_Client_ID, TableName, trxName); + s_log.finest("server => " + id); + if (id < 0) + throw new DBException("No NextID"); + return id; + } + s_log.log(Level.SEVERE, "AppsServer not found - " + TableName); + } + catch (RemoteException ex) + { + s_log.log(Level.SEVERE, "AppsServer error", ex); + } + // Try locally + } + int retValue = -1; // Check AdempiereSys @@ -146,7 +175,8 @@ public class MSequence extends X_AD_Sequence pstmt = null; conn.setAutoCommit(autocommit); //jz set back // - // conn.close(); + if (trx == null && conn != null) + conn.close(); conn = null; // break; // EXIT @@ -241,6 +271,28 @@ public class MSequence extends X_AD_Sequence if (TableName == null || TableName.length() == 0) throw new IllegalArgumentException("TableName missing"); + //get from server + if (DB.isRemoteObjects()) + { + Server server = CConnection.get().getServer(); + try + { + if (server != null) + { // See ServerBean + String dn = server.getDocumentNo (AD_Client_ID, TableName, trxName); + s_log.finest("Server => " + dn); + if (dn != null) + return dn; + } + s_log.log(Level.SEVERE, "AppsServer not found - " + TableName); + } + catch (RemoteException ex) + { + s_log.log(Level.SEVERE, "AppsServer error", ex); + } + } + + //local // Check AdempiereSys boolean adempiereSys = Ini.isPropertyBool(Ini.P_ADEMPIERESYS); if (adempiereSys && AD_Client_ID > 11) @@ -344,7 +396,7 @@ public class MSequence extends X_AD_Sequence if (trx == null) { conn.commit(); - // conn.close(); + conn.close(); } conn = null; } @@ -359,8 +411,8 @@ public class MSequence extends X_AD_Sequence if (pstmt != null) pstmt.close(); pstmt = null; - // if (conn != null && trx == null) - // conn.close(); + if (trx == null && conn != null) + conn.close(); conn = null; } catch (Exception e) @@ -397,7 +449,30 @@ public class MSequence extends X_AD_Sequence { s_log.severe ("C_DocType_ID=0"); return null; - } + } + + //get from server + if (DB.isRemoteObjects()) + { + Server server = CConnection.get().getServer(); + try + { + if (server != null) + { // See ServerBean + String dn = server.getDocumentNo (C_DocType_ID, trxName); + s_log.finest("Server => " + dn); + if (dn != null) + return dn; + } + s_log.log(Level.SEVERE, "AppsServer not found - " + C_DocType_ID); + } + catch (RemoteException ex) + { + s_log.log(Level.SEVERE, "AppsServer error", ex); + } + } + + //local MDocType dt = MDocType.get (Env.getCtx(), C_DocType_ID); // wrong for SERVER, but r/o if (dt != null && !dt.isDocNoControlled()) { @@ -504,7 +579,7 @@ public class MSequence extends X_AD_Sequence if (trx == null) { conn.commit(); - // conn.close(); + conn.close(); } conn = null; } @@ -519,8 +594,8 @@ public class MSequence extends X_AD_Sequence if (pstmt != null) pstmt.close(); pstmt = null; - // if (conn != null && trx == null) - // conn.close(); + if (trx == null && conn != null) + conn.close(); conn = null; } catch (Exception e) diff --git a/base/src/org/compiere/util/CPreparedStatement.java b/base/src/org/compiere/util/CPreparedStatement.java index 755fbb8038..a92837a9e5 100644 --- a/base/src/org/compiere/util/CPreparedStatement.java +++ b/base/src/org/compiere/util/CPreparedStatement.java @@ -1,1147 +1,1187 @@ -/****************************************************************************** - * Product: Adempiere ERP & CRM Smart Business Solution * - * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. * - * This program is free software; you can redistribute it and/or modify it * - * under the terms version 2 of the GNU General Public License as published * - * by the Free Software Foundation. This program is distributed in the hope * - * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * - * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * - * See the GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along * - * with this program; if not, write to the Free Software Foundation, Inc., * - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * - * For the text or an alternative of this public license, you may reach us * - * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA * - * or via info@compiere.org or http://www.compiere.org/license.html * - *****************************************************************************/ -package org.compiere.util; - -import java.io.*; -import java.math.*; -import java.net.*; -import java.sql.*; -import java.util.*; -import java.util.logging.*; -import javax.sql.*; - -import org.compiere.Adempiere; -import org.compiere.db.*; -import org.compiere.interfaces.*; - -/** - * Adempiere Prepared Statement - * - * @author Jorg Janke - * @version $Id: CPreparedStatement.java,v 1.3 2006/07/30 00:54:36 jjanke Exp $ - */ -public class CPreparedStatement extends CStatement implements PreparedStatement -{ - /** - * Prepared Statement Constructor - * - * @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 sql0 unconverted sql statement - * @param trxName transaction name or null - */ - public CPreparedStatement (int resultSetType, int resultSetConcurrency, - String sql0, String trxName) - { - if (sql0 == null || sql0.length() == 0) - throw new IllegalArgumentException ("sql required"); - - p_vo = new CStatementVO (resultSetType, resultSetConcurrency, - DB.getDatabase().convertStatement(sql0)); - - p_vo.setTrxName(trxName); - - init(); - } // CPreparedStatement - - /** - * Initialise the prepared statement wrapper object - */ - protected void init() - { - //Local access - if (!DB.isRemoteObjects()) - { - try - { - Connection conn = null; - Trx trx = p_vo.getTrxName() == null ? null : Trx.get(p_vo.getTrxName(), true); - if (trx != null) - conn = trx.getConnection(); - else - { - 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(), p_vo.getResultSetType(), p_vo.getResultSetConcurrency()); - return; - } - catch (Exception e) - { - log.log(Level.SEVERE, p_vo.getSql(), e); - } - } - } - - /** - * Remote Constructor - * @param vo value object - */ - public CPreparedStatement (CStatementVO vo) - { - super(vo); - } // CPreparedStatement - - - /** - * Execute Query - * @return ResultSet or RowSet - * @throws SQLException - * @see java.sql.PreparedStatement#executeQuery() - */ - public ResultSet executeQuery () throws SQLException - { - if (p_stmt != null) // local - return ((PreparedStatement)p_stmt).executeQuery(); - // - // Client -> remote sever - log.finest("server => " + p_vo + ", Remote=" + DB.isRemoteObjects()); - try - { - 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) - { - ResultSet rs = server.pstmt_getRowSet (p_vo, SecurityToken.getInstance()); - p_vo.clearParameters(); // re-use of result set - if (rs == null) - log.warning("ResultSet 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, "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 - 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 - - /** - * Execute Query - * @param sql0 unconverted SQL to execute - * @return ResultSet or RowSet - * @throws SQLException - * @see java.sql.Statement#executeQuery(String) - */ - public ResultSet executeQuery (String sql0) throws SQLException - { - // Convert SQL - p_vo.setSql(DB.getDatabase().convertStatement(sql0)); - if (p_stmt != null) // local - return p_stmt.executeQuery(p_vo.getSql()); - // - return executeQuery(); - } // executeQuery - - - /************************************************************************** - * Execute Update - * @return no of updated rows - * @throws SQLException - * @see java.sql.PreparedStatement#executeUpdate() - */ - public int executeUpdate () throws SQLException - { - if (p_stmt != null) - return ((PreparedStatement)p_stmt).executeUpdate(); - // - // Client -> remote sever - log.finest("server => " + p_vo + ", Remote=" + DB.isRemoteObjects()); - try - { - if (DB.isRemoteObjects() && CConnection.get().isAppsServerOK(false)) - { - Server server = CConnection.get().getServer(); - if (server != null) - { - int result = server.stmt_executeUpdate (p_vo, SecurityToken.getInstance()); - p_vo.clearParameters(); // re-use of result set - return result; - } - log.log(Level.SEVERE, "AppsServer not found"); - } - } - 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 - 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 - - /** - * Execute Update - * @param sql0 unconverted sql - * @return no of updated rows - * @throws SQLException - * @see java.sql.Statement#executeUpdate(String) - */ - public int executeUpdate (String sql0) throws SQLException - { - // Convert SQL - p_vo.setSql(DB.getDatabase().convertStatement(sql0)); - if (p_stmt != null) // local - return p_stmt.executeUpdate (p_vo.getSql()); - return executeUpdate(); - } // executeUpdate - - - /** - * Method execute - * @return boolean - * @throws SQLException - * @see java.sql.PreparedStatement#execute() - */ - public boolean execute () throws SQLException - { - if (p_stmt != null) - return ((PreparedStatement)p_stmt).execute(); - throw new java.lang.UnsupportedOperationException ("Method execute() not yet implemented."); - } - - - /** - * Method getMetaData - * @return ResultSetMetaData - * @throws SQLException - * @see java.sql.PreparedStatement#getMetaData() - */ - public ResultSetMetaData getMetaData () throws SQLException - { - if (p_stmt != null) - return ((PreparedStatement)p_stmt).getMetaData (); - else - throw new java.lang.UnsupportedOperationException ("Method getMetaData() not yet implemented."); - } - - /** - * Method getParameterMetaData - * @return ParameterMetaData - * @throws SQLException - * @see java.sql.PreparedStatement#getParameterMetaData() - */ - public ParameterMetaData getParameterMetaData () throws SQLException - { - if (p_stmt != null) - return ((PreparedStatement)p_stmt).getParameterMetaData(); - throw new java.lang.UnsupportedOperationException ("Method getParameterMetaData() not yet implemented."); - } - - /** - * Method addBatch - * @throws SQLException - * @see java.sql.PreparedStatement#addBatch() - */ - public void addBatch () throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).addBatch (); - else - throw new java.lang.UnsupportedOperationException ("Method addBatch() not yet implemented."); - } - - /************************************************************************** - * Set Null - * @param parameterIndex index - * @param sqlType type - * @throws SQLException - */ - public void setNull (int parameterIndex, int sqlType) throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).setNull (parameterIndex, sqlType); - else - p_vo.setParameter(parameterIndex, new NullParameter(sqlType)); - } // setNull - - /** - * Method setNull - * @param parameterIndex int - * @param sqlType int - * @param typeName String - * @throws SQLException - * @see java.sql.PreparedStatement#setNull(int, int, String) - */ - public void setNull (int parameterIndex, int sqlType, String typeName) throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).setNull (parameterIndex, sqlType); - else - p_vo.setParameter(parameterIndex, new NullParameter(sqlType)); - } - - /** - * Method setBoolean - * @param parameterIndex int - * @param x boolean - * @throws SQLException - * @see java.sql.PreparedStatement#setBoolean(int, boolean) - */ - public void setBoolean (int parameterIndex, boolean x) throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).setBoolean (parameterIndex, x); - else - p_vo.setParameter(parameterIndex, new Boolean(x)); - } - - /** - * Method setByte - * @param parameterIndex int - * @param x byte - * @throws SQLException - * @see java.sql.PreparedStatement#setByte(int, byte) - */ - public void setByte (int parameterIndex, byte x) throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).setByte (parameterIndex, x); - else - p_vo.setParameter(parameterIndex, new Byte(x)); - } - - /** - * Method setShort - * @param parameterIndex int - * @param x short - * @throws SQLException - * @see java.sql.PreparedStatement#setShort(int, short) - */ - public void setShort (int parameterIndex, short x) throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).setShort (parameterIndex, x); - else - p_vo.setParameter(parameterIndex, new Short(x)); - } - - /** - * Method setInt - * @param parameterIndex int - * @param x int - * @throws SQLException - * @see java.sql.PreparedStatement#setInt(int, int) - */ - public void setInt (int parameterIndex, int x) throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).setInt (parameterIndex, x); - else - p_vo.setParameter(parameterIndex, new Integer(x)); - } - - /** - * Method setLong - * @param parameterIndex int - * @param x long - * @throws SQLException - * @see java.sql.PreparedStatement#setLong(int, long) - */ - public void setLong (int parameterIndex, long x) throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).setLong (parameterIndex, x); - else - p_vo.setParameter(parameterIndex, new Long(x)); - } - - /** - * Method setFloat - * @param parameterIndex int - * @param x float - * @throws SQLException - * @see java.sql.PreparedStatement#setFloat(int, float) - */ - public void setFloat (int parameterIndex, float x) throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).setFloat (parameterIndex, x); - else - p_vo.setParameter(parameterIndex, new Float(x)); - } - - /** - * Method setDouble - * @param parameterIndex int - * @param x double - * @throws SQLException - * @see java.sql.PreparedStatement#setDouble(int, double) - */ - public void setDouble (int parameterIndex, double x) throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).setDouble (parameterIndex, x); - else - p_vo.setParameter(parameterIndex, new Double(x)); - } - - /** - * Method setBigDecimal - * @param parameterIndex int - * @param x BigDecimal - * @throws SQLException - * @see java.sql.PreparedStatement#setBigDecimal(int, BigDecimal) - */ - public void setBigDecimal (int parameterIndex, BigDecimal x) throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).setBigDecimal (parameterIndex, x); - else - p_vo.setParameter(parameterIndex, x); - } - - /** - * Method setString - * @param parameterIndex int - * @param x String - * @throws SQLException - * @see java.sql.PreparedStatement#setString(int, String) - */ - public void setString (int parameterIndex, String x) throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).setString (parameterIndex, x); - else - p_vo.setParameter(parameterIndex, x); - } - - /** - * Method setBytes - * @param parameterIndex int - * @param x byte[] - * @throws SQLException - * @see java.sql.PreparedStatement#setBytes(int, byte[]) - */ - public void setBytes (int parameterIndex, byte[] x) throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).setBytes (parameterIndex, x); - else - p_vo.setParameter (parameterIndex, x); - } - - /** - * Method setDate - * @param parameterIndex int - * @param x java.sql.Date - * @throws SQLException - * @see java.sql.PreparedStatement#setDate(int, java.sql.Date) - */ - public void setDate (int parameterIndex, java.sql.Date x) throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).setDate (parameterIndex, x); - else - p_vo.setParameter(parameterIndex, x); - } - - /** - * Method setTime - * @param parameterIndex int - * @param x Time - * @throws SQLException - * @see java.sql.PreparedStatement#setTime(int, Time) - */ - public void setTime (int parameterIndex, Time x) throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).setTime (parameterIndex, x); - else - p_vo.setParameter(parameterIndex, x); - } - - /** - * Method setTimestamp - * @param parameterIndex int - * @param x Timestamp - * @throws SQLException - * @see java.sql.PreparedStatement#setTimestamp(int, Timestamp) - */ - public void setTimestamp (int parameterIndex, Timestamp x) throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).setTimestamp (parameterIndex, x); - else - p_vo.setParameter(parameterIndex, x); - } - - /** - * Method setAsciiStream - * @param parameterIndex int - * @param x InputStream - * @param length int - * @throws SQLException - * @see java.sql.PreparedStatement#setAsciiStream(int, InputStream, int) - */ - public void setAsciiStream (int parameterIndex, InputStream x, int length) throws SQLException - { - if (p_stmt != null) - ((PreparedStatement)p_stmt).setAsciiStream (parameterIndex, x, length); - else - throw new java.lang.UnsupportedOperationException ("Method setAsciiStream() not yet implemented."); - } - - /** - * @param parameterIndex the first parameter is 1, the second is 2, ... - * @param x a
java.io.InputStream
object that contains the
- * Unicode parameter value as two-byte Unicode characters
- * @param length the number of bytes in the stream
- * @exception SQLException if a database access error occurs
- * see java.sql.PreparedStatement#setUnicodeStream(int, InputStream, int)
- * @deprecated
- */
- public void setUnicodeStream (int parameterIndex, InputStream x, int length) throws SQLException
- {
- throw new UnsupportedOperationException ("Method setUnicodeStream() not yet implemented.");
- }
-
- /**
- * Method setBinaryStream
- * @param parameterIndex int
- * @param x InputStream
- * @param length int
- * @throws SQLException
- * @see java.sql.PreparedStatement#setBinaryStream(int, InputStream, int)
- */
- public void setBinaryStream (int parameterIndex, InputStream x, int length) throws SQLException
- {
- if (p_stmt != null)
- ((PreparedStatement)p_stmt).setBinaryStream (parameterIndex, x, length);
- else
- throw new java.lang.UnsupportedOperationException ("Method setBinaryStream() not yet implemented.");
- }
-
- /**
- * Method clearParameters
- * @throws SQLException
- * @see java.sql.PreparedStatement#clearParameters()
- */
- public void clearParameters () throws SQLException
- {
- if (p_stmt != null)
- ((PreparedStatement)p_stmt).clearParameters ();
- else
- p_vo.clearParameters();
- }
-
- /**
- * Method setObject
- * @param parameterIndex int
- * @param x Object
- * @param targetSqlType int
- * @param scale int
- * @throws SQLException
- * @see java.sql.PreparedStatement#setObject(int, Object, int, int)
- */
- public void setObject (int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException
- {
- if (p_stmt != null)
- ((PreparedStatement)p_stmt).setObject (parameterIndex, x, targetSqlType, scale);
- else
- throw new java.lang.UnsupportedOperationException ("Method setObject() not yet implemented.");
- }
-
- /**
- * Method setObject
- * @param parameterIndex int
- * @param x Object
- * @param targetSqlType int
- * @throws SQLException
- * @see java.sql.PreparedStatement#setObject(int, Object, int)
- */
- public void setObject (int parameterIndex, Object x, int targetSqlType) throws SQLException
- {
- if (p_stmt != null)
- ((PreparedStatement)p_stmt).setObject (parameterIndex, x);
- else
- throw new java.lang.UnsupportedOperationException ("Method setObject() not yet implemented.");
- }
-
- /**
- * Method setObject
- * @param parameterIndex int
- * @param x Object
- * @throws SQLException
- * @see java.sql.PreparedStatement#setObject(int, Object)
- */
- public void setObject (int parameterIndex, Object x) throws SQLException
- {
- if (p_stmt != null)
- ((PreparedStatement)p_stmt).setObject (parameterIndex, x);
- else
- p_vo.setParameter(parameterIndex, x);
- }
-
- /**
- * Method setCharacterStream
- * @param parameterIndex int
- * @param reader Reader
- * @param length int
- * @throws SQLException
- * @see java.sql.PreparedStatement#setCharacterStream(int, Reader, int)
- */
- public void setCharacterStream (int parameterIndex, Reader reader, int length) throws SQLException
- {
- if (p_stmt != null)
- ((PreparedStatement)p_stmt).setCharacterStream (parameterIndex, reader, length);
- else
- throw new java.lang.UnsupportedOperationException ("Method setCharacterStream() not yet implemented.");
- }
-
- /**
- * Method setRef
- * @param parameterIndex int
- * @param x Ref
- * @throws SQLException
- * @see java.sql.PreparedStatement#setRef(int, Ref)
- */
- public void setRef (int parameterIndex, Ref x) throws SQLException
- {
- if (p_stmt != null)
- ((PreparedStatement)p_stmt).setRef (parameterIndex, x);
- else
- p_vo.setParameter(parameterIndex, x);
- }
-
- /**
- * Method setBlob
- * @param parameterIndex int
- * @param x Blob
- * @throws SQLException
- * @see java.sql.PreparedStatement#setBlob(int, Blob)
- */
- public void setBlob (int parameterIndex, Blob x) throws SQLException
- {
- if (p_stmt != null)
- ((PreparedStatement)p_stmt).setObject (parameterIndex, x);
- else
- p_vo.setParameter(parameterIndex, x);
- }
-
- /**
- * Method setClob
- * @param parameterIndex int
- * @param x Clob
- * @throws SQLException
- * @see java.sql.PreparedStatement#setClob(int, Clob)
- */
- public void setClob (int parameterIndex, Clob x) throws SQLException
- {
- if (p_stmt != null)
- ((PreparedStatement)p_stmt).setObject (parameterIndex, x);
- else
- p_vo.setParameter(parameterIndex, x);
- }
-
- /**
- * Method setArray
- * @param parameterIndex int
- * @param x Array
- * @throws SQLException
- * @see java.sql.PreparedStatement#setArray(int, Array)
- */
- public void setArray (int parameterIndex, Array x) throws SQLException
- {
- if (p_stmt != null)
- ((PreparedStatement)p_stmt).setObject (parameterIndex, x);
- else
- p_vo.setParameter(parameterIndex, x);
- }
-
- /**
- * Method setDate
- * @param parameterIndex int
- * @param x java.sql.Date
- * @param cal Calendar
- * @throws SQLException
- * @see java.sql.PreparedStatement#setDate(int, java.sql.Date, Calendar)
- */
- public void setDate (int parameterIndex, java.sql.Date x, Calendar cal) throws SQLException
- {
- if (p_stmt != null)
- ((PreparedStatement)p_stmt).setDate (parameterIndex, x, cal);
- else
- throw new java.lang.UnsupportedOperationException ("Method setDate() not yet implemented.");
- }
-
- /**
- * Method setTime
- * @param parameterIndex int
- * @param x Time
- * @param cal Calendar
- * @throws SQLException
- * @see java.sql.PreparedStatement#setTime(int, Time, Calendar)
- */
- public void setTime (int parameterIndex, Time x, Calendar cal) throws SQLException
- {
- if (p_stmt != null)
- ((PreparedStatement)p_stmt).setTime (parameterIndex, x, cal);
- else
- throw new java.lang.UnsupportedOperationException ("Method setTime() not yet implemented.");
- }
-
- /**
- * Method setTimestamp
- * @param parameterIndex int
- * @param x Timestamp
- * @param cal Calendar
- * @throws SQLException
- * @see java.sql.PreparedStatement#setTimestamp(int, Timestamp, Calendar)
- */
- public void setTimestamp (int parameterIndex, Timestamp x, Calendar cal) throws SQLException
- {
- if (p_stmt != null)
- ((PreparedStatement)p_stmt).setTimestamp (parameterIndex, x, cal);
- else
- throw new java.lang.UnsupportedOperationException ("Method setTimestamp() not yet implemented.");
- }
-
- /**
- * Method setURL
- * @param parameterIndex int
- * @param x URL
- * @throws SQLException
- * @see java.sql.PreparedStatement#setURL(int, URL)
- */
- public void setURL (int parameterIndex, URL x) throws SQLException
- {
- if (p_stmt != null)
- ((PreparedStatement)p_stmt).setObject (parameterIndex, x);
- else
- p_vo.setParameter(parameterIndex, x);
- }
-
- /**
- * String representation
- * @return info
- */
- public String toString()
- {
- if (p_stmt != null)
- return "CPreparedStatement[Local=" + p_stmt + "]";
- return "CPreparedStatement[" + p_vo + "]";
- } // toString
-
- /**************************************************************************
- * Get Prepared Statement to create RowSet and set parameters.
- * Method called on Remote to execute locally.
- * @param dedicatedConnection if true gets new connection - if false gets anormal RO/RW connection
- * @param trxName transaction
- * @return Prepared Statement
- */
- private PreparedStatement local_getPreparedStatement (boolean dedicatedConnection, String trxName)
- {
- log.finest(p_vo.getSql());
- Connection conn = null;
- Trx trx = trxName == null ? null : Trx.get(trxName, true);
- if (trx != null)
- conn = trx.getConnection();
- else
- {
- if (dedicatedConnection)
- conn = DB.createConnection (false, Connection.TRANSACTION_READ_COMMITTED);
- else
- conn = local_getConnection (trxName);
- }
- if (conn == null)
- throw new IllegalStateException("Local - No Connection");
- PreparedStatement pstmt = null;
- try
- {
- pstmt = conn.prepareStatement(p_vo.getSql(), p_vo.getResultSetType(), p_vo.getResultSetConcurrency());
- // 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 ("Local - 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());
- }
- }
- catch (SQLException ex)
- {
- log.log(Level.SEVERE, "local", ex);
- try
- {
- if (pstmt != null)
- pstmt.close();
- pstmt = null;
- }
- catch (SQLException ex1)
- {
- }
- }
- return pstmt;
- } // local_getPreparedStatement
-
-
- /**
- * 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
- {
- 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, SecurityToken.getInstance());
- 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, "AppsServer error", ex);
- p_remoteErrors++;
- if (ex instanceof RuntimeException)
- throw (RuntimeException)ex;
- else
- throw new RuntimeException(ex);
- }
- // Try locally
- 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");
- }
- }
-
- /**
- * 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
- {
- // 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);
- rs.close();
- pstmt.close();
- pstmt = 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", e);
- }
- return rowSet;
- } // local_getRowSet
-
- /*************************************************************************
- * Execute Update.
- * @return row count
- */
- public int remote_executeUpdate()
- {
- log.finest("Update");
- PreparedStatement pstmt = null;
- try
- {
- AdempiereDatabase db = CConnection.get().getDatabase();
- if (db == null)
- throw new NullPointerException("Remote - No Database");
- //
- pstmt = local_getPreparedStatement (false, p_vo.getTrxName());
- int result = pstmt.executeUpdate();
- //
- return result;
- }
- catch (Exception ex)
- {
- log.log(Level.SEVERE, p_vo.toString(), ex);
- throw new RuntimeException (ex);
- }
- finally {
- if (pstmt != null) {
- try {
- pstmt.close();
- } catch (SQLException e) {}
- pstmt = null;
- }
- }
- } // remote_executeUpdate
-
- //remove this commnet if you want use JAVA 6
-
- public void setAsciiStream(int parameterIndex, java.io.InputStream x, long length)
- throws SQLException
- {
-
- }
-
- //vpj-cd add support java 6
- /*
- public void setBinaryStream(int parameterIndex, java.io.InputStream x,
- long length) throws SQLException
- {
-
- }
-
- public void setCharacterStream(int parameterIndex,
- java.io.Reader reader,
- long length) throws SQLException
- {
-
- }
-
- public void setAsciiStream(int parameterIndex, java.io.InputStream x)
- throws SQLException
- {
-
- }
-
-
- public void setBinaryStream(int parameterIndex, java.io.InputStream x)
- throws SQLException
- {
-
- }
-
- public void setCharacterStream(int parameterIndex,
- java.io.Reader reader) throws SQLException
- {
-
- }
-
- public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException
- {
-
- }
-
- public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException
- {
-
- }
-
- public void setClob(int parameterIndex, Reader reader)
- throws SQLException
- {
-
- }
-
- public void setClob(int parameterIndex, Reader reader, long length)
- throws SQLException
- {
-
- }
-
- public void setNClob(int parameterIndex, Reader reader, long length)
- throws SQLException
- {
-
- }
-
- public void setNClob(int parameterIndex, Reader reader)
- throws SQLException
- {
-
- }
-
- public void setNClob(int i,java.sql.NClob c)
- throws SQLException
- {
-
- }
-
- public void setBlob(int parameterIndex, InputStream inputStream, long length)
- throws SQLException
- {
-
- }
-
-
- public void setBlob(int parameterIndex, InputStream inputStream)
- throws SQLException
- {
-
- }
-
- public void setNString(int parameterIndex, String value) throws SQLException
- {
-
- }
-
- //In order to compile in Java 6 you must add these methods
-
-
- public void setSQLXML(int parameterIndex, java.sql.SQLXML xmlObject) throws SQLException
- {
-
- }
-
- public void setRowId(int parameterIndex, java.sql.RowId x) throws SQLException{}
- */
-
-} // CPreparedStatement
-
+/******************************************************************************
+ * Product: Adempiere ERP & CRM Smart Business Solution *
+ * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. *
+ * This program is free software; you can redistribute it and/or modify it *
+ * under the terms version 2 of the GNU General Public License as published *
+ * by the Free Software Foundation. This program is distributed in the hope *
+ * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
+ * See the GNU General Public License for more details. *
+ * You should have received a copy of the GNU General Public License along *
+ * with this program; if not, write to the Free Software Foundation, Inc., *
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
+ * For the text or an alternative of this public license, you may reach us *
+ * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA *
+ * or via info@compiere.org or http://www.compiere.org/license.html *
+ *****************************************************************************/
+package org.compiere.util;
+
+import java.io.*;
+import java.math.*;
+import java.net.*;
+import java.sql.*;
+import java.util.*;
+import java.util.logging.*;
+
+import javax.sql.*;
+
+import org.compiere.db.*;
+import org.compiere.interfaces.*;
+
+/**
+ * Adempiere Prepared Statement
+ *
+ * @author Jorg Janke
+ * @version $Id: CPreparedStatement.java,v 1.3 2006/07/30 00:54:36 jjanke Exp $
+ * ---
+ * Modifications: Handle connections properly
+ * Reason : Due to changes brought in the connection pooling whereby the system
+ * no more relies upon abandoned connections.
+ * @author Ashley Ramdass (Posterita)
+ */
+public class CPreparedStatement extends CStatement implements PreparedStatement
+{
+ /**
+ * Prepared Statement Constructor
+ *
+ * @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 sql0 unconverted sql statement
+ * @param trxName transaction name or null
+ */
+ public CPreparedStatement (int resultSetType, int resultSetConcurrency,
+ String sql0, String trxName)
+ {
+ if (sql0 == null || sql0.length() == 0)
+ throw new IllegalArgumentException ("sql required");
+
+ p_vo = new CStatementVO (resultSetType, resultSetConcurrency,
+ DB.getDatabase().convertStatement(sql0));
+
+ p_vo.setTrxName(trxName);
+
+ init();
+ } // CPreparedStatement
+
+ /**
+ * Initialise the prepared statement wrapper object
+ */
+ protected void init()
+ {
+ //Local access
+ if (!DB.isRemoteObjects())
+ {
+ try
+ {
+ Connection conn = null;
+ Trx trx = p_vo.getTrxName() == null ? null : Trx.get(p_vo.getTrxName(), true);
+ if (trx != null)
+ conn = trx.getConnection();
+ else
+ {
+ 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(), p_vo.getResultSetType(), p_vo.getResultSetConcurrency());
+ return;
+ }
+ catch (Exception e)
+ {
+ log.log(Level.SEVERE, p_vo.getSql(), e);
+ }
+ }
+ }
+
+ /**
+ * Remote Constructor
+ * @param vo value object
+ */
+ public CPreparedStatement (CStatementVO vo)
+ {
+ super(vo);
+ } // CPreparedStatement
+
+
+ /**
+ * Execute Query
+ * @return ResultSet or RowSet
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#executeQuery()
+ */
+ public ResultSet executeQuery () throws SQLException
+ {
+ if (p_stmt != null) // local
+ return ((PreparedStatement)p_stmt).executeQuery();
+ //
+ // Client -> remote sever
+ log.finest("server => " + p_vo + ", Remote=" + DB.isRemoteObjects());
+ try
+ {
+ 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)
+ {
+ ResultSet rs = server.pstmt_getRowSet (p_vo, SecurityToken.getInstance());
+ p_vo.clearParameters(); // re-use of result set
+ if (rs == null)
+ log.warning("ResultSet 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, "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
+ 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
+
+ /**
+ * Execute Query
+ * @param sql0 unconverted SQL to execute
+ * @return ResultSet or RowSet
+ * @throws SQLException
+ * @see java.sql.Statement#executeQuery(String)
+ */
+ public ResultSet executeQuery (String sql0) throws SQLException
+ {
+ // Convert SQL
+ p_vo.setSql(DB.getDatabase().convertStatement(sql0));
+ if (p_stmt != null) // local
+ return p_stmt.executeQuery(p_vo.getSql());
+ //
+ return executeQuery();
+ } // executeQuery
+
+
+ /**************************************************************************
+ * Execute Update
+ * @return no of updated rows
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#executeUpdate()
+ */
+ public int executeUpdate () throws SQLException
+ {
+ if (p_stmt != null)
+ return ((PreparedStatement)p_stmt).executeUpdate();
+ //
+ // Client -> remote sever
+ log.finest("server => " + p_vo + ", Remote=" + DB.isRemoteObjects());
+ try
+ {
+ if (DB.isRemoteObjects() && CConnection.get().isAppsServerOK(false))
+ {
+ Server server = CConnection.get().getServer();
+ if (server != null)
+ {
+ int result = server.stmt_executeUpdate (p_vo, SecurityToken.getInstance());
+ p_vo.clearParameters(); // re-use of result set
+ return result;
+ }
+ log.log(Level.SEVERE, "AppsServer not found");
+ }
+ }
+ 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
+ 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
+
+ /**
+ * Execute Update
+ * @param sql0 unconverted sql
+ * @return no of updated rows
+ * @throws SQLException
+ * @see java.sql.Statement#executeUpdate(String)
+ */
+ public int executeUpdate (String sql0) throws SQLException
+ {
+ // Convert SQL
+ p_vo.setSql(DB.getDatabase().convertStatement(sql0));
+ if (p_stmt != null) // local
+ return p_stmt.executeUpdate (p_vo.getSql());
+ return executeUpdate();
+ } // executeUpdate
+
+
+ /**
+ * Method execute
+ * @return boolean
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#execute()
+ */
+ public boolean execute () throws SQLException
+ {
+ if (p_stmt != null)
+ return ((PreparedStatement)p_stmt).execute();
+ throw new java.lang.UnsupportedOperationException ("Method execute() not yet implemented.");
+ }
+
+
+ /**
+ * Method getMetaData
+ * @return ResultSetMetaData
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#getMetaData()
+ */
+ public ResultSetMetaData getMetaData () throws SQLException
+ {
+ if (p_stmt != null)
+ return ((PreparedStatement)p_stmt).getMetaData ();
+ else
+ throw new java.lang.UnsupportedOperationException ("Method getMetaData() not yet implemented.");
+ }
+
+ /**
+ * Method getParameterMetaData
+ * @return ParameterMetaData
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#getParameterMetaData()
+ */
+ public ParameterMetaData getParameterMetaData () throws SQLException
+ {
+ if (p_stmt != null)
+ return ((PreparedStatement)p_stmt).getParameterMetaData();
+ throw new java.lang.UnsupportedOperationException ("Method getParameterMetaData() not yet implemented.");
+ }
+
+ /**
+ * Method addBatch
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#addBatch()
+ */
+ public void addBatch () throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).addBatch ();
+ else
+ throw new java.lang.UnsupportedOperationException ("Method addBatch() not yet implemented.");
+ }
+
+ /**************************************************************************
+ * Set Null
+ * @param parameterIndex index
+ * @param sqlType type
+ * @throws SQLException
+ */
+ public void setNull (int parameterIndex, int sqlType) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setNull (parameterIndex, sqlType);
+ else
+ p_vo.setParameter(parameterIndex, new NullParameter(sqlType));
+ } // setNull
+
+ /**
+ * Method setNull
+ * @param parameterIndex int
+ * @param sqlType int
+ * @param typeName String
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setNull(int, int, String)
+ */
+ public void setNull (int parameterIndex, int sqlType, String typeName) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setNull (parameterIndex, sqlType);
+ else
+ p_vo.setParameter(parameterIndex, new NullParameter(sqlType));
+ }
+
+ /**
+ * Method setBoolean
+ * @param parameterIndex int
+ * @param x boolean
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setBoolean(int, boolean)
+ */
+ public void setBoolean (int parameterIndex, boolean x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setBoolean (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, new Boolean(x));
+ }
+
+ /**
+ * Method setByte
+ * @param parameterIndex int
+ * @param x byte
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setByte(int, byte)
+ */
+ public void setByte (int parameterIndex, byte x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setByte (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, new Byte(x));
+ }
+
+ /**
+ * Method setShort
+ * @param parameterIndex int
+ * @param x short
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setShort(int, short)
+ */
+ public void setShort (int parameterIndex, short x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setShort (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, new Short(x));
+ }
+
+ /**
+ * Method setInt
+ * @param parameterIndex int
+ * @param x int
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setInt(int, int)
+ */
+ public void setInt (int parameterIndex, int x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setInt (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, new Integer(x));
+ }
+
+ /**
+ * Method setLong
+ * @param parameterIndex int
+ * @param x long
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setLong(int, long)
+ */
+ public void setLong (int parameterIndex, long x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setLong (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, new Long(x));
+ }
+
+ /**
+ * Method setFloat
+ * @param parameterIndex int
+ * @param x float
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setFloat(int, float)
+ */
+ public void setFloat (int parameterIndex, float x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setFloat (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, new Float(x));
+ }
+
+ /**
+ * Method setDouble
+ * @param parameterIndex int
+ * @param x double
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setDouble(int, double)
+ */
+ public void setDouble (int parameterIndex, double x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setDouble (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, new Double(x));
+ }
+
+ /**
+ * Method setBigDecimal
+ * @param parameterIndex int
+ * @param x BigDecimal
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setBigDecimal(int, BigDecimal)
+ */
+ public void setBigDecimal (int parameterIndex, BigDecimal x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setBigDecimal (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, x);
+ }
+
+ /**
+ * Method setString
+ * @param parameterIndex int
+ * @param x String
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setString(int, String)
+ */
+ public void setString (int parameterIndex, String x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setString (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, x);
+ }
+
+ /**
+ * Method setBytes
+ * @param parameterIndex int
+ * @param x byte[]
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setBytes(int, byte[])
+ */
+ public void setBytes (int parameterIndex, byte[] x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setBytes (parameterIndex, x);
+ else
+ p_vo.setParameter (parameterIndex, x);
+ }
+
+ /**
+ * Method setDate
+ * @param parameterIndex int
+ * @param x java.sql.Date
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setDate(int, java.sql.Date)
+ */
+ public void setDate (int parameterIndex, java.sql.Date x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setDate (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, x);
+ }
+
+ /**
+ * Method setTime
+ * @param parameterIndex int
+ * @param x Time
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setTime(int, Time)
+ */
+ public void setTime (int parameterIndex, Time x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setTime (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, x);
+ }
+
+ /**
+ * Method setTimestamp
+ * @param parameterIndex int
+ * @param x Timestamp
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setTimestamp(int, Timestamp)
+ */
+ public void setTimestamp (int parameterIndex, Timestamp x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setTimestamp (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, x);
+ }
+
+ /**
+ * Method setAsciiStream
+ * @param parameterIndex int
+ * @param x InputStream
+ * @param length int
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setAsciiStream(int, InputStream, int)
+ */
+ public void setAsciiStream (int parameterIndex, InputStream x, int length) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setAsciiStream (parameterIndex, x, length);
+ else
+ throw new java.lang.UnsupportedOperationException ("Method setAsciiStream() not yet implemented.");
+ }
+
+ /**
+ * @param parameterIndex the first parameter is 1, the second is 2, ...
+ * @param x a java.io.InputStream
object that contains the
+ * Unicode parameter value as two-byte Unicode characters
+ * @param length the number of bytes in the stream
+ * @exception SQLException if a database access error occurs
+ * see java.sql.PreparedStatement#setUnicodeStream(int, InputStream, int)
+ * @deprecated
+ */
+ public void setUnicodeStream (int parameterIndex, InputStream x, int length) throws SQLException
+ {
+ throw new UnsupportedOperationException ("Method setUnicodeStream() not yet implemented.");
+ }
+
+ /**
+ * Method setBinaryStream
+ * @param parameterIndex int
+ * @param x InputStream
+ * @param length int
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setBinaryStream(int, InputStream, int)
+ */
+ public void setBinaryStream (int parameterIndex, InputStream x, int length) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setBinaryStream (parameterIndex, x, length);
+ else
+ throw new java.lang.UnsupportedOperationException ("Method setBinaryStream() not yet implemented.");
+ }
+
+ /**
+ * Method clearParameters
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#clearParameters()
+ */
+ public void clearParameters () throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).clearParameters ();
+ else
+ p_vo.clearParameters();
+ }
+
+ /**
+ * Method setObject
+ * @param parameterIndex int
+ * @param x Object
+ * @param targetSqlType int
+ * @param scale int
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setObject(int, Object, int, int)
+ */
+ public void setObject (int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setObject (parameterIndex, x, targetSqlType, scale);
+ else
+ throw new java.lang.UnsupportedOperationException ("Method setObject() not yet implemented.");
+ }
+
+ /**
+ * Method setObject
+ * @param parameterIndex int
+ * @param x Object
+ * @param targetSqlType int
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setObject(int, Object, int)
+ */
+ public void setObject (int parameterIndex, Object x, int targetSqlType) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setObject (parameterIndex, x);
+ else
+ throw new java.lang.UnsupportedOperationException ("Method setObject() not yet implemented.");
+ }
+
+ /**
+ * Method setObject
+ * @param parameterIndex int
+ * @param x Object
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setObject(int, Object)
+ */
+ public void setObject (int parameterIndex, Object x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setObject (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, x);
+ }
+
+ /**
+ * Method setCharacterStream
+ * @param parameterIndex int
+ * @param reader Reader
+ * @param length int
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setCharacterStream(int, Reader, int)
+ */
+ public void setCharacterStream (int parameterIndex, Reader reader, int length) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setCharacterStream (parameterIndex, reader, length);
+ else
+ throw new java.lang.UnsupportedOperationException ("Method setCharacterStream() not yet implemented.");
+ }
+
+ /**
+ * Method setRef
+ * @param parameterIndex int
+ * @param x Ref
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setRef(int, Ref)
+ */
+ public void setRef (int parameterIndex, Ref x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setRef (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, x);
+ }
+
+ /**
+ * Method setBlob
+ * @param parameterIndex int
+ * @param x Blob
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setBlob(int, Blob)
+ */
+ public void setBlob (int parameterIndex, Blob x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setObject (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, x);
+ }
+
+ /**
+ * Method setClob
+ * @param parameterIndex int
+ * @param x Clob
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setClob(int, Clob)
+ */
+ public void setClob (int parameterIndex, Clob x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setObject (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, x);
+ }
+
+ /**
+ * Method setArray
+ * @param parameterIndex int
+ * @param x Array
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setArray(int, Array)
+ */
+ public void setArray (int parameterIndex, Array x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setObject (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, x);
+ }
+
+ /**
+ * Method setDate
+ * @param parameterIndex int
+ * @param x java.sql.Date
+ * @param cal Calendar
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setDate(int, java.sql.Date, Calendar)
+ */
+ public void setDate (int parameterIndex, java.sql.Date x, Calendar cal) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setDate (parameterIndex, x, cal);
+ else
+ throw new java.lang.UnsupportedOperationException ("Method setDate() not yet implemented.");
+ }
+
+ /**
+ * Method setTime
+ * @param parameterIndex int
+ * @param x Time
+ * @param cal Calendar
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setTime(int, Time, Calendar)
+ */
+ public void setTime (int parameterIndex, Time x, Calendar cal) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setTime (parameterIndex, x, cal);
+ else
+ throw new java.lang.UnsupportedOperationException ("Method setTime() not yet implemented.");
+ }
+
+ /**
+ * Method setTimestamp
+ * @param parameterIndex int
+ * @param x Timestamp
+ * @param cal Calendar
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setTimestamp(int, Timestamp, Calendar)
+ */
+ public void setTimestamp (int parameterIndex, Timestamp x, Calendar cal) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setTimestamp (parameterIndex, x, cal);
+ else
+ throw new java.lang.UnsupportedOperationException ("Method setTimestamp() not yet implemented.");
+ }
+
+ /**
+ * Method setURL
+ * @param parameterIndex int
+ * @param x URL
+ * @throws SQLException
+ * @see java.sql.PreparedStatement#setURL(int, URL)
+ */
+ public void setURL (int parameterIndex, URL x) throws SQLException
+ {
+ if (p_stmt != null)
+ ((PreparedStatement)p_stmt).setObject (parameterIndex, x);
+ else
+ p_vo.setParameter(parameterIndex, x);
+ }
+
+ /**
+ * String representation
+ * @return info
+ */
+ public String toString()
+ {
+ if (p_stmt != null)
+ return "CPreparedStatement[Local=" + p_stmt + "]";
+ return "CPreparedStatement[" + p_vo + "]";
+ } // toString
+
+ /**************************************************************************
+ * Get Prepared Statement to create RowSet and set parameters.
+ * Method called on Remote to execute locally.
+ * @param dedicatedConnection if true gets new connection - if false gets anormal RO/RW connection
+ * @param trxName transaction
+ * @return Prepared Statement
+ */
+ private PreparedStatement local_getPreparedStatement (boolean dedicatedConnection, String trxName)
+ {
+ log.finest(p_vo.getSql());
+ Connection conn = null;
+ Trx trx = trxName == null ? null : Trx.get(trxName, true);
+ if (trx != null)
+ conn = trx.getConnection();
+ else
+ {
+ if (dedicatedConnection)
+ conn = DB.createConnection (false, Connection.TRANSACTION_READ_COMMITTED);
+ else
+ conn = local_getConnection (trxName);
+ }
+ if (conn == null)
+ throw new IllegalStateException("Local - No Connection");
+ PreparedStatement pstmt = null;
+ try
+ {
+ pstmt = conn.prepareStatement(p_vo.getSql(), p_vo.getResultSetType(), p_vo.getResultSetConcurrency());
+ // 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 ("Local - 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 if (o instanceof java.util.Date)
+ {
+ pstmt.setTimestamp(i+1, new Timestamp(((java.util.Date)o).getTime()));
+ log.finest("#" + (i+1) + " - Date=" + o);
+ }
+ else if (o instanceof java.sql.Date)
+ {
+ pstmt.setTimestamp(i+1, new Timestamp(((java.sql.Date)o).getTime()));
+ log.finest("#" + (i+1) + " - Date=" + o);
+ }
+ else
+ throw new java.lang.UnsupportedOperationException ("Unknown Parameter Class=" + o.getClass());
+ }
+ }
+ catch (SQLException ex)
+ {
+ log.log(Level.SEVERE, "local", ex);
+ if (pstmt != null)
+ {
+ try
+ {
+ pstmt.close();
+ }
+ catch (Exception e)
+ {
+ log.log(Level.SEVERE, "Could not close prepared statement", e);
+ }
+ }
+
+ if (conn != null && p_vo.getTrxName() == null)
+ {
+ try
+ {
+ conn.close();
+ }
+ catch (Exception e)
+ {
+ log.log(Level.SEVERE, "Could not close connection", e);
+ }
+ }
+
+ pstmt = null;
+ }
+ return pstmt;
+ } // local_getPreparedStatement
+
+
+ /**
+ * 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
+ {
+ 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, SecurityToken.getInstance());
+ 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, "AppsServer error", ex);
+ p_remoteErrors++;
+ if (ex instanceof RuntimeException)
+ throw (RuntimeException)ex;
+ else
+ throw new RuntimeException(ex);
+ }
+ // Try locally
+ 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");
+ }
+ }
+
+ /**
+ * 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
+ {
+ // 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 if (o instanceof java.util.Date)
+ {
+ pstmt.setTimestamp(i+1, new Timestamp(((java.util.Date)o).getTime()));
+ log.finest("#" + (i+1) + " - Date=" + o);
+ }
+ else if (o instanceof java.sql.Date)
+ {
+ pstmt.setTimestamp(i+1, new Timestamp(((java.sql.Date)o).getTime()));
+ log.finest("#" + (i+1) + " - Date=" + o);
+ }
+ else
+ throw new java.lang.UnsupportedOperationException ("Unknown Parameter Class=" + o.getClass());
+ }
+ //
+ ResultSet rs = pstmt.executeQuery();
+ rowSet = CCachedRowSet.getRowSet(rs);
+ rs.close();
+ }
+ catch (Exception ex)
+ {
+ log.log(Level.SEVERE, p_vo.toString(), ex);
+ throw new RuntimeException (ex);
+ }
+ return rowSet;
+ } // local_getRowSet
+
+ /*************************************************************************
+ * Execute Update.
+ * @return row count
+ */
+ public int remote_executeUpdate()
+ {
+ log.finest("Update");
+ PreparedStatement pstmt = null;
+ try
+ {
+ AdempiereDatabase db = CConnection.get().getDatabase();
+ if (db == null)
+ throw new NullPointerException("Remote - No Database");
+ //
+ pstmt = local_getPreparedStatement (false, p_vo.getTrxName());
+ int result = pstmt.executeUpdate();
+ //
+ return result;
+ }
+ catch (Exception ex)
+ {
+ log.log(Level.SEVERE, p_vo.toString(), ex);
+ throw new RuntimeException (ex);
+ }
+ finally
+ {
+ if (pstmt != null)
+ {
+ try
+ {
+ Connection conn = pstmt.getConnection();
+ pstmt.close();
+ if (p_vo.getTrxName() == null && !conn.isClosed())
+ {
+ conn.close();
+ }
+ }
+ catch (SQLException e)
+ {
+ log.log(Level.SEVERE, e.getMessage(), e);
+ }
+ pstmt = null;
+ }
+ }
+ } // remote_executeUpdate
+
+ //remove this commnet if you want use JAVA 6
+
+ public void setAsciiStream(int parameterIndex, java.io.InputStream x, long length)
+ throws SQLException
+ {
+
+ }
+
+ //vpj-cd add support java 6
+ /*
+ public void setBinaryStream(int parameterIndex, java.io.InputStream x,
+ long length) throws SQLException
+ {
+
+ }
+
+ public void setCharacterStream(int parameterIndex,
+ java.io.Reader reader,
+ long length) throws SQLException
+ {
+
+ }
+
+ public void setAsciiStream(int parameterIndex, java.io.InputStream x)
+ throws SQLException
+ {
+
+ }
+
+
+ public void setBinaryStream(int parameterIndex, java.io.InputStream x)
+ throws SQLException
+ {
+
+ }
+
+ public void setCharacterStream(int parameterIndex,
+ java.io.Reader reader) throws SQLException
+ {
+
+ }
+
+ public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException
+ {
+
+ }
+
+ public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException
+ {
+
+ }
+
+ public void setClob(int parameterIndex, Reader reader)
+ throws SQLException
+ {
+
+ }
+
+ public void setClob(int parameterIndex, Reader reader, long length)
+ throws SQLException
+ {
+
+ }
+
+ public void setNClob(int parameterIndex, Reader reader, long length)
+ throws SQLException
+ {
+
+ }
+
+ public void setNClob(int parameterIndex, Reader reader)
+ throws SQLException
+ {
+
+ }
+
+ public void setNClob(int i,java.sql.NClob c)
+ throws SQLException
+ {
+
+ }
+
+ public void setBlob(int parameterIndex, InputStream inputStream, long length)
+ throws SQLException
+ {
+
+ }
+
+
+ public void setBlob(int parameterIndex, InputStream inputStream)
+ throws SQLException
+ {
+
+ }
+
+ public void setNString(int parameterIndex, String value) throws SQLException
+ {
+
+ }
+
+ //In order to compile in Java 6 you must add these methods
+
+
+ public void setSQLXML(int parameterIndex, java.sql.SQLXML xmlObject) throws SQLException
+ {
+
+ }
+
+ public void setRowId(int parameterIndex, java.sql.RowId x) throws SQLException{}
+ */
+
+} // CPreparedStatement
+
diff --git a/base/src/org/compiere/util/CStatement.java b/base/src/org/compiere/util/CStatement.java
index 19b3d82e19..c45e54cc40 100644
--- a/base/src/org/compiere/util/CStatement.java
+++ b/base/src/org/compiere/util/CStatement.java
@@ -21,7 +21,6 @@ import java.util.logging.*;
import javax.sql.*;
-import org.compiere.Adempiere;
import org.compiere.db.*;
import org.compiere.interfaces.*;
@@ -30,6 +29,12 @@ import org.compiere.interfaces.*;
*
* @author Jorg Janke
* @version $Id: CStatement.java,v 1.3 2006/07/30 00:54:36 jjanke Exp $
+ * ---
+ * Modifications: Handle connections properly
+ * Close the associated connection when the statement is closed
+ * Reason : Due to changes brought in the connection pooling whereby the system
+ * no more relies upon abandoned connections.
+ * @author Ashley Ramdass (Posterita)
*/
public class CStatement implements Statement
{
@@ -742,8 +747,16 @@ public class CStatement implements Statement
*/
public void close () throws SQLException
{
- if (p_stmt != null)
- p_stmt.close();
+ if (p_stmt != null)
+ {
+ Connection conn = p_stmt.getConnection();
+ p_stmt.close();
+
+ if (!conn.isClosed() && conn.getAutoCommit())
+ {
+ conn.close();
+ }
+ }
} // close
/*************************************************************************
@@ -770,15 +783,25 @@ public class CStatement implements Statement
log.log(Level.SEVERE, p_vo.toString(), ex);
throw new RuntimeException (ex);
}
- finally {
- if (pstmt != null)
- {
- try
- {
- pstmt.close();
- } catch (SQLException e){}
- pstmt = null;
- }
+ finally
+ {
+ if (pstmt != null)
+ {
+ try
+ {
+ Connection conn = pstmt.getConnection();
+ pstmt.close();
+ if (p_vo.getTrxName() == null && !conn.isClosed())
+ {
+ conn.close();
+ }
+ }
+ catch (SQLException e)
+ {
+ log.log(Level.SEVERE, e.getMessage(), e);
+ }
+ pstmt = null;
+ }
}
} // remote_executeUpdate
@@ -811,15 +834,30 @@ public class CStatement implements Statement
catch (SQLException ex)
{
log.log(Level.SEVERE, "local", ex);
- try
- {
- if (stmt != null)
- stmt.close();
- stmt = null;
- }
- catch (SQLException ex1)
- {
- }
+ if (stmt != null)
+ {
+ try
+ {
+ stmt.close();
+ }
+ catch (Exception e)
+ {
+ log.log(Level.SEVERE, "Could not close statement", e);
+ }
+ stmt = null;
+ }
+
+ if (conn != null && p_vo.getTrxName() == null)
+ {
+ try
+ {
+ conn.close();
+ }
+ catch (Exception e)
+ {
+ log.log(Level.SEVERE, "Could not close connection", e);
+ }
+ }
}
return stmt;
} // local_getStatement
@@ -925,18 +963,7 @@ public class CStatement implements Statement
{
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;
} // local_getRowSet
diff --git a/base/src/org/compiere/util/DB.java b/base/src/org/compiere/util/DB.java
index de5dd34b49..2a34da6694 100644
--- a/base/src/org/compiere/util/DB.java
+++ b/base/src/org/compiere/util/DB.java
@@ -23,9 +23,9 @@ import java.sql.*;
import java.text.*;
import java.util.*;
import java.util.logging.*;
+
import javax.sql.*;
import javax.swing.*;
-import oracle.jdbc.*;
//
import org.compiere.*;
import org.compiere.db.*;
@@ -43,6 +43,11 @@ import org.compiere.process.*;
*
* @author Jorg Janke
* @version $Id: DB.java,v 1.8 2006/10/09 00:22:29 jjanke Exp $
+ * ---
+ * Modifications: removed static references to database connection and instead always
+ * get a new connection from database pool manager which manages all connections
+ * set rw/ro properties for the connection accordingly.
+ * @author Ashley Ramdass (Posterita)
*/
public final class DB
{
@@ -83,34 +88,28 @@ public final class DB
log.info("Role");
String sql = "SELECT * FROM AD_Role";
PreparedStatement pstmt = null;
+ ResultSet rs = null;
try
{
pstmt = DB.prepareStatement (sql, null);
- ResultSet rs = pstmt.executeQuery ();
+ rs = pstmt.executeQuery ();
while (rs.next ())
{
MRole role = new MRole (ctx, rs, null);
role.updateAccessRecords();
- }
- rs.close ();
- pstmt.close ();
- pstmt = null;
+ }
}
catch (Exception e)
{
log.log(Level.SEVERE, "(1)", e);
}
- try
- {
- if (pstmt != null)
- pstmt.close ();
- pstmt = null;
- }
- catch (Exception e)
- {
- pstmt = null;
- }
-
+ finally
+ {
+ close(rs);
+ close(pstmt);
+ rs= null;
+ pstmt = null;
+ }
// Release Specif stuff & Print Format
try
{
@@ -232,10 +231,7 @@ public final class DB
//
synchronized(s_ccLock)
{
- s_cc = cc;
- s_connections = null;
- s_connectionRW = null;
- s_connectionID = null;
+ s_cc = cc;
}
if ( isRemoteObjects() == false)
s_cc.setDataSource();
@@ -260,14 +256,31 @@ public final class DB
//direct connection
boolean success =false;
try
+ {
+ Connection connRW = getConnectionRW();
+ if (connRW != null)
+ {
+ s_cc.readInfo(connRW);
+ connRW.close();
+ }
+
+ Connection connRO = getConnectionRO();
+ if (connRO != null)
+ {
+ connRO.close();
+ }
+
+ Connection connID = getConnectionID();
+ if (connID != null)
+ {
+ connID.close();
+ }
+ success = ((connRW != null) && (connRO != null) && (connID != null));
+ }
+ catch (Exception e)
{
- success = getConnectionRW() != null;
- if (success) success = getConnectionRO() != null;
- if (success) success = getConnectionID() != null;
- s_cc.readInfo(getConnectionRW());
- } catch (Exception e)
- {
- success = false;
+ log.log(Level.SEVERE, "Could not connect to DB", e);
+ success = false;
}
return success;
}
@@ -307,7 +320,12 @@ public final class DB
eb = null; // don't reset
try
{
- success = getConnectionRW(createNew) != null; // try to get a connection
+ Connection conn = getConnectionRW(createNew); // try to get a connection
+ if (conn != null)
+ {
+ conn.close();
+ }
+ success = (conn != null);
}
catch (Exception e)
{
@@ -334,49 +352,7 @@ public final class DB
*/
public static Connection getConnectionRW (boolean createNew)
{
- //wan profile
- if (CConnection.get().isRMIoverHTTP()) return null;
-
- // check health of connection
- try
- {
- if (s_connectionRW == null)
- ;
- else if (s_connectionRW.isClosed())
- {
- log.finest("Closed");
- s_connectionRW = null;
- }
- else if (s_connectionRW instanceof OracleConnection && ((OracleConnection)s_connectionRW).pingDatabase(1) < 0)
- {
- log.warning("No ping");
- s_connectionRW = null;
- }
- else
- {
- if (s_connectionRW.getTransactionIsolation() != Connection.TRANSACTION_READ_COMMITTED)
- s_connectionRW.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
- }
- }
- catch (Exception e)
- {
- s_connectionRW = null;
- }
- // Get new
- if (s_connectionRW == null)
- {
- if (createNew)
- {
- s_connectionRW = s_cc.getConnection (true, Connection.TRANSACTION_READ_COMMITTED);
- log.finest("Con=" + s_connectionRW);
- }
- }
- if (s_connectionRW == null && createNew)
- throw new UnsupportedOperationException("No DBConnection");
- //
- // System.err.println ("DB.getConnectionRW - " + s_connectionRW);
- // Trace.printStack();
- return s_connectionRW;
+ return createConnection(true, false, Connection.TRANSACTION_READ_COMMITTED);
} // getConnectionRW
/**
@@ -386,29 +362,7 @@ public final class DB
*/
public static Connection getConnectionID ()
{
- //wan profile
- if (CConnection.get().isRMIoverHTTP()) return null;
-
- if (s_connectionID != null)
- {
- try
- {
- if (s_connectionID.isClosed())
- s_connectionID = null;
- }
- catch (Exception e)
- {
- s_connectionID = null;
- }
- }
- if (s_connectionID == null)
- {
- s_connectionID = s_cc.getConnection (false, Connection.TRANSACTION_READ_COMMITTED);
- }
- if (s_connectionID == null)
- throw new UnsupportedOperationException("No DBConnection");
- log.log(Level.ALL, s_connectionID.toString());
- return s_connectionID;
+ return createConnection(false, false, Connection.TRANSACTION_READ_COMMITTED);
} // getConnectionID
/**
@@ -417,75 +371,7 @@ public final class DB
*/
public static Connection getConnectionRO ()
{
- //wan profile
- if (CConnection.get().isRMIoverHTTP()) return null;
-
- try
- {
- synchronized(s_ccLock)
- {
- if (s_connections == null)
- s_connections = createConnections (Connection.TRANSACTION_READ_COMMITTED); // see below
- }
- }
- catch (Exception e)
- {
- log.log(Level.SEVERE, "RO", e);
- }
-
- // check health of connection
- int pos = s_conCount++;
- int connectionNo = pos % s_conCacheSize;
- Connection connection = s_connections[connectionNo];
- try
- {
- if (connection == null)
- ;
- else if (connection.isClosed())
- {
- // RowSet.close also closes connection!
- // System.out.println("DB.getConnectionRO - closed #" + connectionNo);
- connection = null;
- }
- else if (connection instanceof OracleConnection && ((OracleConnection)connection).pingDatabase(1) < 0)
- {
- log.warning("No ping #" + connectionNo);
- connection = null;
- }
- else
- {
- if (!connection.isReadOnly())
- connection.setReadOnly(true);
- if (connection.getTransactionIsolation() != Connection.TRANSACTION_READ_COMMITTED)
- connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
- }
- }
- catch (Exception e)
- {
- log.severe("#" + connectionNo + " - " + e.toString());
- connection = null;
- }
- // Get new
- if (connection == null)
- {
- log.finest("Replacing connection #" + connectionNo);
- connection = s_cc.getConnection (true, Connection.TRANSACTION_READ_COMMITTED); // see above
- try
- {
- if (connection != null)
- connection.setReadOnly(true);
- }
- catch (Exception e)
- {
- log.severe("Cannot set to R/O - " + e);
- }
- s_connections[connectionNo] = connection;
- }
- if (connection == null)
- throw new UnsupportedOperationException("DB.getConnectionRO - @NoDBConnection@");
- log.log(Level.ALL, "#" + connectionNo + " - " + connection);
- // System.err.println ("DB.getConnectionRO - " + connection);
- return connection;
+ return createConnection(true, true, Connection.TRANSACTION_READ_COMMITTED); // see below
} // getConnectionRO
/**
@@ -520,34 +406,41 @@ public final class DB
return conn;
} // createConnection
- /**
- * Create new set of r/o Connections.
- * R/O connection might not be supported by DB
- *
- * @param trxLevel - Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_REPEATABLE_READ, or Connection.TRANSACTION_READ_COMMITTED.
- * @return Array of Connections (size based on s_conCacheSize)
- */
- private static Connection[] createConnections (int trxLevel)
- {
- log.finest("(" + s_conCacheSize + ") " + s_cc.getConnectionURL()
- + ", UserID=" + s_cc.getDbUid()
- + ", TrxLevel=" + CConnection.getTransactionIsolationInfo(trxLevel));
- Connection cons[] = new Connection[s_conCacheSize];
- try
- {
- for (int i = 0; i < s_conCacheSize; i++)
- {
- cons[i] = s_cc.getConnection (true, trxLevel); // auto commit
- if (cons[i] == null)
- log.warning("Connection is NULL"); // don't use log
- }
- }
- catch (Exception e)
- {
- log.severe(e.getMessage());
- }
- return cons;
- } // createConnections
+ /**
+ * Create new Connection.
+ * The connection must be closed explicitly by the application
+ *
+ * @param autoCommit auto commit
+ * @param trxLevel - Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_REPEATABLE_READ, or Connection.TRANSACTION_READ_COMMITTED.
+ * @return Connection connection
+ */
+ public static Connection createConnection (boolean autoCommit, boolean readOnly, int trxLevel)
+ {
+ //wan profile
+ if (CConnection.get().isRMIoverHTTP()) return null;
+
+ Connection conn = s_cc.getConnection (autoCommit, trxLevel);
+
+ 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@");
+ }
+
+ return conn;
+ } // createConnection
/**
* Get Database Driver.
@@ -618,45 +511,52 @@ public final class DB
*/
public static boolean isDatabaseOK (Properties ctx)
{
- // Check Version
- String version = "?";
- String sql = "SELECT Version FROM AD_System";
- try
- {
- PreparedStatement pstmt = prepareStatement(sql, null);
- ResultSet rs = pstmt.executeQuery();
- if (rs.next())
- version = rs.getString(1);
- rs.close();
- pstmt.close();
- }
- catch (SQLException e)
- {
- log.log(Level.SEVERE, "Problem with AD_System Table - Run system.sql script - " + e.toString());
- return false;
- }
- log.info("DB_Version=" + version);
- // Identical DB version
- if (Adempiere.DB_VERSION.equals(version))
- return true;
+// Check Version
+ String version = "?";
+ String sql = "SELECT Version FROM AD_System";
+ PreparedStatement pstmt = null;
+ ResultSet rs = null;
+ try
+ {
+ pstmt = prepareStatement(sql, null);
+ rs = pstmt.executeQuery();
+ if (rs.next())
+ version = rs.getString(1);
+ }
+ catch (SQLException e)
+ {
+ log.log(Level.SEVERE, "Problem with AD_System Table - Run system.sql script - " + e.toString());
+ return false;
+ }
+ finally
+ {
+ close(rs);
+ close(pstmt);
+ rs= null;
+ pstmt = null;
+ }
+ log.info("DB_Version=" + version);
+ // Identical DB version
+ if (Adempiere.DB_VERSION.equals(version))
+ 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);
- }
- return false;
+ 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);
+ }
+ return false;
} // isDatabaseOK
@@ -665,66 +565,18 @@ public final class DB
*/
public static void closeTarget()
{
- boolean closed = false;
- // RO connection
- if (s_connections != null)
- {
- for (int i = 0; i < s_conCacheSize; i++)
- {
- try
- {
- if (s_connections[i] != null)
- {
- closed = true;
- s_connections[i].close();
- }
- }
- catch (SQLException e)
- {
- log.warning("#" + i + " - " + e.getMessage());
- }
- s_connections[i] = null;
- }
- }
- s_connections = null;
-
- // RW connection
- try
- {
- if (s_connectionRW != null)
- {
- closed = true;
- s_connectionRW.close();
- }
- }
- catch (SQLException e)
- {
- log.log(Level.SEVERE, "R/W", e);
- }
- s_connectionRW = null;
-
- //ID Connection
- try
- {
- if (s_connectionID != null)
- {
- s_connectionID.close();
- }
- } catch (SQLException e)
- {
- log.log(Level.SEVERE, "Id", e);
- }
- s_connectionID = null;
-
- // CConnection
- if (s_cc != null)
- {
- closed = true;
- s_cc.setDataSource(null);
- }
- s_cc = null;
- if (closed)
- log.fine("closed");
+
+ boolean closed = false;
+
+ // CConnection
+ if (s_cc != null)
+ {
+ closed = true;
+ s_cc.setDataSource(null);
+ }
+ s_cc = null;
+ if (closed)
+ log.fine("closed");
} // closeTarget
/**************************************************************************
@@ -1080,18 +932,28 @@ public final class DB
* @return true if not needed or success
* @throws SQLException
*/
- public static boolean commit (boolean throwException, String trxName) throws SQLException
+ public static boolean commit (boolean throwException, String trxName) throws SQLException,IllegalStateException
{
+ // Not on transaction scope, Connection are thus auto commit
+ 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.commit(true);
- else
- conn = DB.getConnectionRW ();
- if (conn != null && !conn.getAutoCommit())
- conn.commit();
+
+ if (throwException)
+ {
+ throw new IllegalStateException("Could not load transation with identifier: " + trxName);
+ }
+ else
+ {
+ return false;
+ }
}
catch (SQLException e)
{
@@ -1099,8 +961,7 @@ public final class DB
if (throwException)
throw e;
return false;
- }
- return true;
+ }
} // commit
/**
@@ -1156,304 +1017,261 @@ public final class DB
return retValue;
} // getRowSet
- /**
- * Get Value from sql
- * @param trxName trx
- * @param sql sql
- * @return first value or -1
- */
- public static int getSQLValue (String trxName, String sql)
- {
- int retValue = -1;
- PreparedStatement pstmt = null;
- try
- {
- pstmt = prepareStatement(sql, trxName);
- ResultSet rs = pstmt.executeQuery();
- if (rs.next())
- retValue = rs.getInt(1);
- else
- log.fine("No Value " + sql);
- rs.close();
- pstmt.close();
- pstmt = null;
- }
- catch (Exception e)
- {
- log.log(Level.SEVERE, sql, e);
- }
- finally
- {
- try
- {
- if (pstmt != null)
- pstmt.close ();
- }
- catch (Exception e)
- {}
- pstmt = null;
- }
- return retValue;
- } // getSQLValue
+ /**
+ * Get Value from sql
+ * @param trxName trx
+ * @param sql sql
+ * @return first value or -1
+ */
+ public static int getSQLValue (String trxName, String sql)
+ {
+ int retValue = -1;
+ PreparedStatement pstmt = null;
+ ResultSet rs = null;
+ try
+ {
+ pstmt = prepareStatement(sql, trxName);
+ rs = pstmt.executeQuery();
+ if (rs.next())
+ retValue = rs.getInt(1);
+ else
+ log.fine("No Value " + sql);
+ }
+ catch (Exception e)
+ {
+ log.log(Level.SEVERE, sql, e);
+ }
+ finally
+ {
+ close(rs);
+ close(pstmt);
+ rs= null;
+ pstmt = null;
+ }
+ return retValue;
+ } // getSQLValue
- /**
- * Get Value from sql
- * @param trxName trx
- * @param sql sql
- * @param int_param1 parameter 1
- * @return first value or -1
- */
- public static int getSQLValue (String trxName, String sql, int int_param1)
- {
- int retValue = -1;
- PreparedStatement pstmt = null;
- try
- {
- pstmt = prepareStatement(sql, trxName);
- pstmt.setInt(1, int_param1);
- ResultSet rs = pstmt.executeQuery();
- if (rs.next())
- retValue = rs.getInt(1);
- else
- log.config("No Value " + sql + " - Param1=" + int_param1);
- rs.close();
- pstmt.close();
- pstmt = null;
- }
- catch (Exception e)
- {
- log.log(Level.SEVERE, sql + " - Param1=" + int_param1 + " [" + trxName + "]", e);
- }
- finally
- {
- try
- {
- if (pstmt != null)
- pstmt.close ();
- }
- catch (Exception e)
- {}
- pstmt = null;
- }
- return retValue;
- } // getSQLValue
+ /**
+ * Get Value from sql
+ * @param trxName trx
+ * @param sql sql
+ * @param int_param1 parameter 1
+ * @return first value or -1
+ */
+ public static int getSQLValue (String trxName, String sql, int int_param1)
+ {
+ int retValue = -1;
+ PreparedStatement pstmt = null;
+ ResultSet rs = null;
+ try
+ {
+ pstmt = prepareStatement(sql, trxName);
+ pstmt.setInt(1, int_param1);
+ rs = pstmt.executeQuery();
+ if (rs.next())
+ retValue = rs.getInt(1);
+ else
+ log.config("No Value " + sql + " - Param1=" + int_param1);
+ }
+ catch (Exception e)
+ {
+ log.log(Level.SEVERE, sql + " - Param1=" + int_param1 + " [" + trxName + "]", e);
+ }
+ finally
+ {
+ close(rs);
+ close(pstmt);
+ rs= null;
+ pstmt = null;
+ }
+ return retValue;
+ } // getSQLValue
- /**
- * Get Value from sql
- * @param trxName trx
- * @param sql sql
- * @param int_param1 parameter 1
- * @param int_param2 parameter 2
- * @return first value or -1
- */
- public static int getSQLValue (String trxName, String sql, int int_param1, int int_param2)
- {
- int retValue = -1;
- PreparedStatement pstmt = null;
- try
- {
- pstmt = prepareStatement(sql, trxName);
- pstmt.setInt(1, int_param1);
- pstmt.setInt(2, int_param2);
- ResultSet rs = pstmt.executeQuery();
- if (rs.next())
- retValue = rs.getInt(1);
- else
- log.info("No Value " + sql
- + " - Param1=" + int_param1 + ",Param2=" + int_param2);
- rs.close();
- pstmt.close();
- pstmt = null;
- }
- catch (Exception e)
- {
- log.log(Level.SEVERE, sql + " - Param1=" + int_param1 + ",Param2=" + int_param2
- + " [" + trxName + "]", e);
- }
- finally
- {
- try
- {
- if (pstmt != null)
- pstmt.close ();
- }
- catch (Exception e)
- {}
- pstmt = null;
- }
- return retValue;
- } // getSQLValue
+ /**
+ * Get Value from sql
+ * @param trxName trx
+ * @param sql sql
+ * @param int_param1 parameter 1
+ * @param int_param2 parameter 2
+ * @return first value or -1
+ */
+ public static int getSQLValue (String trxName, String sql, int int_param1, int int_param2)
+ {
+ int retValue = -1;
+ PreparedStatement pstmt = null;
+ ResultSet rs = null;
+ try
+ {
+ pstmt = prepareStatement(sql, trxName);
+ pstmt.setInt(1, int_param1);
+ pstmt.setInt(2, int_param2);
+ rs = pstmt.executeQuery();
+ if (rs.next())
+ retValue = rs.getInt(1);
+ else
+ log.info("No Value " + sql
+ + " - Param1=" + int_param1 + ",Param2=" + int_param2);
+ }
+ catch (Exception e)
+ {
+ log.log(Level.SEVERE, sql + " - Param1=" + int_param1 + ",Param2=" + int_param2
+ + " [" + trxName + "]", e);
+ }
+ finally
+ {
+ close(rs);
+ close(pstmt);
+ rs= null;
+ pstmt = null;
+ }
+ return retValue;
+ } // getSQLValue
- /**
- * Get Value from sql
- * @param trxName trx
- * @param sql sql
- * @param str_param1 parameter 1
- * @return first value or -1
- */
- public static int getSQLValue (String trxName, String sql, String str_param1)
- {
- int retValue = -1;
- PreparedStatement pstmt = null;
- try
- {
- pstmt = prepareStatement(sql, trxName);
- pstmt.setString(1, str_param1);
- ResultSet rs = pstmt.executeQuery();
- if (rs.next())
- retValue = rs.getInt(1);
- else
- log.info("No Value " + sql + " - Param1=" + str_param1);
- rs.close();
- pstmt.close();
- pstmt = null;
- }
- catch (Exception e)
- {
- log.log(Level.SEVERE, sql + " - Param1=" + str_param1, e);
- }
- finally
- {
- try
- {
- if (pstmt != null)
- pstmt.close ();
- }
- catch (Exception e)
- {}
- pstmt = null;
- }
- return retValue;
- } // getSQLValue
+ /**
+ * Get Value from sql
+ * @param trxName trx
+ * @param sql sql
+ * @param str_param1 parameter 1
+ * @return first value or -1
+ */
+ public static int getSQLValue (String trxName, String sql, String str_param1)
+ {
+ int retValue = -1;
+ PreparedStatement pstmt = null;
+ ResultSet rs = null;
+ try
+ {
+ pstmt = prepareStatement(sql, trxName);
+ pstmt.setString(1, str_param1);
+ rs = pstmt.executeQuery();
+ if (rs.next())
+ retValue = rs.getInt(1);
+ else
+ log.info("No Value " + sql + " - Param1=" + str_param1);
+ }
+ catch (Exception e)
+ {
+ log.log(Level.SEVERE, sql + " - Param1=" + str_param1, e);
+ }
+ finally
+ {
+ close(rs);
+ close(pstmt);
+ rs= null;
+ pstmt = null;
+ }
+ return retValue;
+ } // getSQLValue
- /**
- * Get Value from sql
- * @param trxName trx
- * @param sql sql
- * @param int_param1 parameter 1
- * @param s_param2 parameter 2
- * @return first value or -1
- */
- public static int getSQLValue (String trxName, String sql, int int_param1, String s_param2)
- {
- int retValue = -1;
- PreparedStatement pstmt = null;
- try
- {
- pstmt = prepareStatement(sql, trxName);
- pstmt.setInt(1, int_param1);
- pstmt.setString(2, s_param2);
- ResultSet rs = pstmt.executeQuery();
- if (rs.next())
- retValue = rs.getInt(1);
- else
- log.info("No Value: " + sql + " - Param1=" + int_param1 + ",Param2=" + s_param2);
- rs.close();
- pstmt.close();
- pstmt = null;
- }
- catch (Exception e)
- {
- log.log(Level.SEVERE, sql + " - Param1=" + int_param1 + ",Param2=" + s_param2, e);
- }
- finally
- {
- try
- {
- if (pstmt != null)
- pstmt.close ();
- }
- catch (Exception e)
- {}
- pstmt = null;
- }
- return retValue;
- } // getSQLValue
+ /**
+ * Get Value from sql
+ * @param trxName trx
+ * @param sql sql
+ * @param int_param1 parameter 1
+ * @param s_param2 parameter 2
+ * @return first value or -1
+ */
+ public static int getSQLValue (String trxName, String sql, int int_param1, String s_param2)
+ {
+ int retValue = -1;
+ PreparedStatement pstmt = null;
+ ResultSet rs = null;
+ try
+ {
+ pstmt = prepareStatement(sql, trxName);
+ pstmt.setInt(1, int_param1);
+ pstmt.setString(2, s_param2);
+ rs = pstmt.executeQuery();
+ if (rs.next())
+ retValue = rs.getInt(1);
+ else
+ log.info("No Value: " + sql + " - Param1=" + int_param1 + ",Param2=" + s_param2);
+ }
+ catch (Exception e)
+ {
+ log.log(Level.SEVERE, sql + " - Param1=" + int_param1 + ",Param2=" + s_param2, e);
+ }
+ finally
+ {
+ close(rs);
+ close(pstmt);
+ rs= null;
+ pstmt = null;
+ }
+ return retValue;
+ } // getSQLValue
- /**
- * Get String Value from sql
- * @param trxName trx
- * @param sql sql
- * @param int_param1 parameter 1
- * @return first value or null
- */
- public static String getSQLValueString (String trxName, String sql, int int_param1)
- {
- String retValue = null;
- PreparedStatement pstmt = null;
- try
- {
- pstmt = prepareStatement(sql, trxName);
- pstmt.setInt(1, int_param1);
- ResultSet rs = pstmt.executeQuery();
- if (rs.next())
- retValue = rs.getString(1);
- else
- log.info("No Value " + sql + " - Param1=" + int_param1);
- rs.close();
- pstmt.close();
- pstmt = null;
- }
- catch (Exception e)
- {
- log.log(Level.SEVERE, sql + " - Param1=" + int_param1, e);
- }
- finally
- {
- try
- {
- if (pstmt != null)
- pstmt.close ();
- }
- catch (Exception e)
- {}
- pstmt = null;
- }
- return retValue;
- } // getSQLValueString
-
- /**
- * Get BigDecimal Value from sql
- * @param trxName trx
- * @param sql sql
- * @param int_param1 parameter 1
- * @return first value or null
- */
- public static BigDecimal getSQLValueBD (String trxName, String sql, int int_param1)
- {
- BigDecimal retValue = null;
- PreparedStatement pstmt = null;
- try
- {
- pstmt = prepareStatement(sql, trxName);
- pstmt.setInt(1, int_param1);
- ResultSet rs = pstmt.executeQuery();
- if (rs.next())
- retValue = rs.getBigDecimal(1);
- else
- log.info("No Value " + sql + " - Param1=" + int_param1);
- rs.close();
- pstmt.close();
- pstmt = null;
- }
- catch (Exception e)
- {
- log.log(Level.SEVERE, sql + " - Param1=" + int_param1 + " [" + trxName + "]", e);
- }
- finally
- {
- try
- {
- if (pstmt != null)
- pstmt.close ();
- }
- catch (Exception e)
- {}
- pstmt = null;
- }
- return retValue;
- } // getSQLValueBD
+ /**
+ * Get String Value from sql
+ * @param trxName trx
+ * @param sql sql
+ * @param int_param1 parameter 1
+ * @return first value or null
+ */
+ public static String getSQLValueString (String trxName, String sql, int int_param1)
+ {
+ String retValue = null;
+ PreparedStatement pstmt = null;
+ ResultSet rs = null;
+ try
+ {
+ pstmt = prepareStatement(sql, trxName);
+ pstmt.setInt(1, int_param1);
+ rs = pstmt.executeQuery();
+ if (rs.next())
+ retValue = rs.getString(1);
+ else
+ log.info("No Value " + sql + " - Param1=" + int_param1);
+ }
+ catch (Exception e)
+ {
+ log.log(Level.SEVERE, sql + " - Param1=" + int_param1, e);
+ }
+ finally
+ {
+ close(rs);
+ close(pstmt);
+ rs= null;
+ pstmt = null;
+ }
+ return retValue;
+ } // getSQLValueString
+ /**
+ * Get BigDecimal Value from sql
+ * @param trxName trx
+ * @param sql sql
+ * @param int_param1 parameter 1
+ * @return first value or null
+ */
+ public static BigDecimal getSQLValueBD (String trxName, String sql, int int_param1)
+ {
+ BigDecimal retValue = null;
+ PreparedStatement pstmt = null;
+ ResultSet rs = null;
+ try
+ {
+ pstmt = prepareStatement(sql, trxName);
+ pstmt.setInt(1, int_param1);
+ rs = pstmt.executeQuery();
+ if (rs.next())
+ retValue = rs.getBigDecimal(1);
+ else
+ log.info("No Value " + sql + " - Param1=" + int_param1);
+ }
+ catch (Exception e)
+ {
+ log.log(Level.SEVERE, sql + " - Param1=" + int_param1 + " [" + trxName + "]", e);
+ }
+ finally
+ {
+ close(rs);
+ close(pstmt);
+ rs= null;
+ pstmt = null;
+ }
+ return retValue;
+ } // getSQLValueBD
/**
* Get Array of Key Name Pairs
@@ -1463,38 +1281,33 @@ public final class DB
*/
public static KeyNamePair[] getKeyNamePairs(String sql, boolean optional)
{
- PreparedStatement pstmt = null;
- ArrayList