Skip to content

Commit

Permalink
adempiere#3638 [Feature Request] Allow configuration of Database conn…
Browse files Browse the repository at this point in the history
…ection pool from container server (Tomcat, Jetty, Wildfly) adempiere#3638

- Add template for the database pool configuration from the application server
- Add data source settings to web applications
- Add support for wildfly 24.0.1.Final
  https://www.wildfly.org/
- Add support for Jetty
- Add support for Tomcat 9
- Add configuration files to configure the connection pool for each application server (Tomcat, Jetty, WildFly)
- Update startup shell scripts for new services
- Add support for Java 17 TLS
- Remove obsolete Tomcat configuration
- Remove obsolete JBoss configuration
- Remove obsolete configuration of Glassfish
- Remove support for Java 8
- Rename the JVM Sun to JVM Oracle
  • Loading branch information
e-Evolution committed Oct 2, 2021
1 parent b5eb57d commit 3378796
Show file tree
Hide file tree
Showing 47 changed files with 1,276 additions and 892 deletions.
136 changes: 71 additions & 65 deletions base/src/org/compiere/db/DB_MariaDB.java
Expand Up @@ -23,14 +23,18 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Optional;
import java.util.logging.Level;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.DataSource;
import javax.sql.RowSet;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.DBException;
import org.compiere.Adempiere;
import org.compiere.dbPort.Convert;
Expand Down Expand Up @@ -463,85 +467,87 @@ public Connection getCachedConnection(CConnection connection,

/**
* Create DataSource (Client)
*
*
* @param connection connection
* @return data source
*/
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");
// System.setProperty("com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL",
// "ALL");
HikariConfig config = new HikariConfig();
config.setDriverClassName(DRIVER);
config.setJdbcUrl(getConnectionURL(connection));
config.setUsername(connection.getDbUid());
config.setPassword(connection.getDbPwd());

config.addDataSourceProperty( "poolName" , "AdempiereDS" );
config.addDataSourceProperty( "cachePrepStmts" , "true" );
config.addDataSourceProperty( "prepStmtCacheSize" , "250" );
config.addDataSourceProperty( "prepStmtCacheSqlLimit" , "2048" );
config.addDataSourceProperty("connectionTestQuery", DEFAULT_CONN_TEST_SQL);

try
{
if (Ini.isClient()) {
log.warning("Config Hikari Connection Pool Datasource");
HikariConfig config = new HikariConfig();
config.setDriverClassName(DRIVER);
config.setJdbcUrl(getConnectionURL(connection));
config.setUsername(connection.getDbUid());
config.setPassword(connection.getDbPwd());
config.addDataSourceProperty( "poolName" , "AdempiereDS" );
config.addDataSourceProperty( "cachePrepStmts" , "true" );
config.addDataSourceProperty( "prepStmtCacheSize" , "250" );
config.addDataSourceProperty( "prepStmtCacheSqlLimit" , "2048" );
config.addDataSourceProperty("connectionTestQuery", DEFAULT_CONN_TEST_SQL);
config.addDataSourceProperty( "connectionInitSql" , "1" );
config.addDataSourceProperty( "maximumPoolSize" , "15" );
config.addDataSourceProperty( "idleTimeout" , "1200" );
m_maxbusyconnections = 10;
config.addDataSourceProperty("maximumPoolSize", "15");
HikariDataSource cpds = new HikariDataSource(config);
m_ds = cpds;
log.warning("Starting Hikari Connection Pool based on client");
} else {
config.addDataSourceProperty( "connectionInitSql" , "1" );
config.addDataSourceProperty( "maximumPoolSize" , "150" );
config.addDataSourceProperty( "idleTimeout" , "1200" );
m_maxbusyconnections = 10;
m_maxbusyconnections = 120;
Optional<String> maybeApplicationType = Optional.ofNullable(System.getenv("ADEMPIERE_APPS_TYPE"));
m_ds = maybeApplicationType
.map(applicationType -> {
if ("wildfly".equals(applicationType)) {
try {
Context initCtx = new InitialContext();
DataSource dataSource = (DataSource) initCtx.lookup("java:/AdempiereDS");
log.warning("Connection Lookup JNDI Datasource for java:/AdempiereDS Hikari Connection Pool");
HikariConfig config = new HikariConfig();
config.addDataSourceProperty("maximumPoolSize", "150");
config.setDataSource(dataSource);
return new HikariDataSource(config);
} catch (Exception namingException) {
m_ds = null;
log.log(Level.SEVERE, "Could not initialise Hikari Connection Pool", namingException);
namingException.printStackTrace();
}
}
try {
DataSource dataSource = InitialContext.doLookup("java:comp/env/java/AdempiereDS");
log.warning("Connection Lookup JNDI Datasource for java:comp/env/java/AdempiereDS Hikari Connection Pool");
HikariConfig config = new HikariConfig();
config.addDataSourceProperty("maximumPoolSize", "150");
config.setDataSource(dataSource);
return new HikariDataSource(config);
} catch (Exception namingException) {
m_ds = null;
log.log(Level.SEVERE, "Could not initialise Hikari Connection Pool", namingException);
namingException.printStackTrace();
}
log.warning("Connection successful using Standalone Hikari Config Connection Pool");
HikariConfig config = new HikariConfig();
config.setDriverClassName(DRIVER);
config.setJdbcUrl(getConnectionURL(connection));
config.setUsername(connection.getDbUid());
config.setPassword(connection.getDbPwd());
config.addDataSourceProperty("poolName", "AdempiereDS");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
config.addDataSourceProperty("connectionTestQuery", DEFAULT_CONN_TEST_SQL);
config.addDataSourceProperty("connectionInitSql", "1");
config.addDataSourceProperty("idleTimeout", "1200");
config.addDataSourceProperty("maximumPoolSize", "150");
return new HikariDataSource(config);
}).orElseThrow(() -> new AdempiereException("Could not initialise Hikari Connection Pool"));
}
HikariDataSource cpds = new HikariDataSource(config);
m_ds = cpds;
//
// 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(1200);
// // cpds.setTestConnectionOnCheckin(true);
// // cpds.setTestConnectionOnCheckout(true);
// cpds.setAcquireRetryAttempts(2);
// // cpds.setCheckoutTimeout(60);
//
// if (Ini.isClient()) {
// cpds.setInitialPoolSize(1);
// cpds.setMinPoolSize(1);
// cpds.setMaxPoolSize(15);
// cpds.setMaxIdleTimeExcessConnections(1200);
// cpds.setMaxIdleTime(900);
// m_maxbusyconnections = 10;
// } else {
// cpds.setInitialPoolSize(10);
// cpds.setMinPoolSize(5);
// cpds.setMaxPoolSize(150);
// cpds.setMaxIdleTimeExcessConnections(1200);
// cpds.setMaxIdleTime(1200);
// m_maxbusyconnections = 120;
// }
//
// // the following sometimes kill active connection!
// // cpds.setUnreturnedConnectionTimeout(1200);
// // cpds.setDebugUnreturnedConnectionStackTraces(true);
//
// m_ds = cpds;
} catch (Exception ex) {
} catch (Exception exception) {
m_ds = null;
log.log(Level.SEVERE, "Could not initialise C3P0 Datasource", ex);
log.log(Level.SEVERE, "Could not initialise Hikari Connection Pool", exception);
exception.printStackTrace();
}

return m_ds;
}

Expand Down
100 changes: 71 additions & 29 deletions base/src/org/compiere/db/DB_MySQL.java
Expand Up @@ -34,14 +34,18 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Optional;
import java.util.logging.Level;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.DataSource;
import javax.sql.RowSet;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.DBException;
import org.compiere.Adempiere;
import org.compiere.dbPort.Convert;
Expand Down Expand Up @@ -498,49 +502,87 @@ public Connection getCachedConnection(CConnection connection,

/**
* Create DataSource (Client)
*
*
* @param connection connection
* @return data source
*/
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");
// System.setProperty("com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL",
// "ALL");
HikariConfig config = new HikariConfig();
config.setDriverClassName(DRIVER);
config.setJdbcUrl(getConnectionURL(connection));
config.setUsername(connection.getDbUid());
config.setPassword(connection.getDbPwd());

config.addDataSourceProperty( "poolName" , "AdempiereDS" );
config.addDataSourceProperty( "cachePrepStmts" , "true" );
config.addDataSourceProperty( "prepStmtCacheSize" , "250" );
config.addDataSourceProperty( "prepStmtCacheSqlLimit" , "2048" );
config.addDataSourceProperty("connectionTestQuery", DEFAULT_CONN_TEST_SQL);

try
{
if (Ini.isClient()) {
log.warning("Config Hikari Connection Pool Datasource");
HikariConfig config = new HikariConfig();
config.setDriverClassName(DRIVER);
config.setJdbcUrl(getConnectionURL(connection));
config.setUsername(connection.getDbUid());
config.setPassword(connection.getDbPwd());
config.addDataSourceProperty( "poolName" , "AdempiereDS" );
config.addDataSourceProperty( "cachePrepStmts" , "true" );
config.addDataSourceProperty( "prepStmtCacheSize" , "250" );
config.addDataSourceProperty( "prepStmtCacheSqlLimit" , "2048" );
config.addDataSourceProperty("connectionTestQuery", DEFAULT_CONN_TEST_SQL);
config.addDataSourceProperty( "connectionInitSql" , "1" );
config.addDataSourceProperty( "maximumPoolSize" , "15" );
config.addDataSourceProperty( "idleTimeout" , "1200" );
m_maxbusyconnections = 10;
config.addDataSourceProperty("maximumPoolSize", "15");
HikariDataSource cpds = new HikariDataSource(config);
m_ds = cpds;
log.warning("Starting Hikari Connection Pool based on client");
} else {
config.addDataSourceProperty( "connectionInitSql" , "1" );
config.addDataSourceProperty( "maximumPoolSize" , "150" );
config.addDataSourceProperty( "idleTimeout" , "1200" );
m_maxbusyconnections = 10;
m_maxbusyconnections = 120;
Optional<String> maybeApplicationType = Optional.ofNullable(System.getenv("ADEMPIERE_APPS_TYPE"));
m_ds = maybeApplicationType
.map(applicationType -> {
if ("wildfly".equals(applicationType)) {
try {
Context initCtx = new InitialContext();
DataSource dataSource = (DataSource) initCtx.lookup("java:/AdempiereDS");
log.warning("Connection Lookup JNDI Datasource for java:/AdempiereDS Hikari Connection Pool");
HikariConfig config = new HikariConfig();
config.addDataSourceProperty("maximumPoolSize", "150");
config.setDataSource(dataSource);
return new HikariDataSource(config);
} catch (Exception namingException) {
m_ds = null;
log.log(Level.SEVERE, "Could not initialise Hikari Connection Pool", namingException);
namingException.printStackTrace();
}
}
try {
DataSource dataSource = InitialContext.doLookup("java:comp/env/java/AdempiereDS");
log.warning("Connection Lookup JNDI Datasource for java:comp/env/java/AdempiereDS Hikari Connection Pool");
HikariConfig config = new HikariConfig();
config.addDataSourceProperty("maximumPoolSize", "150");
config.setDataSource(dataSource);
return new HikariDataSource(config);
} catch (Exception namingException) {
m_ds = null;
log.log(Level.SEVERE, "Could not initialise Hikari Connection Pool", namingException);
namingException.printStackTrace();
}
log.warning("Connection successful using Standalone Hikari Config Connection Pool");
HikariConfig config = new HikariConfig();
config.setDriverClassName(DRIVER);
config.setJdbcUrl(getConnectionURL(connection));
config.setUsername(connection.getDbUid());
config.setPassword(connection.getDbPwd());
config.addDataSourceProperty("poolName", "AdempiereDS");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
config.addDataSourceProperty("connectionTestQuery", DEFAULT_CONN_TEST_SQL);
config.addDataSourceProperty("connectionInitSql", "1");
config.addDataSourceProperty("idleTimeout", "1200");
config.addDataSourceProperty("maximumPoolSize", "150");
return new HikariDataSource(config);
}).orElseThrow(() -> new AdempiereException("Could not initialise Hikari Connection Pool"));
}
HikariDataSource cpds = new HikariDataSource(config);
m_ds = cpds;
} catch (Exception ex) {
} catch (Exception exception) {
m_ds = null;
log.log(Level.SEVERE, "Could not initialise C3P0 Datasource", ex);
log.log(Level.SEVERE, "Could not initialise Hikari Connection Pool", exception);
exception.printStackTrace();
}

return m_ds;
}

Expand Down

0 comments on commit 3378796

Please sign in to comment.