Skip to content

Commit 9fb36e7

Browse files
nvazquezrohityadavcloud
authored andcommitted
db: Update Apache DBCP version (apache#2718)
* Update Apache DBCP version * Fix DB connection * Prevent hang on the db creation
1 parent fc1874c commit 9fb36e7

File tree

5 files changed

+84
-56
lines changed

5 files changed

+84
-56
lines changed

developer/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
</parent>
2323
<dependencies>
2424
<dependency>
25-
<groupId>commons-dbcp</groupId>
26-
<artifactId>commons-dbcp</artifactId>
25+
<groupId>org.apache.commons</groupId>
26+
<artifactId>commons-dbcp2</artifactId>
2727
</dependency>
2828
<dependency>
29-
<groupId>commons-pool</groupId>
30-
<artifactId>commons-pool</artifactId>
29+
<groupId>org.apache.commons</groupId>
30+
<artifactId>commons-pool2</artifactId>
3131
</dependency>
3232
<dependency>
3333
<groupId>org.jasypt</groupId>

engine/schema/src/main/java/com/cloud/upgrade/DatabaseCreator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,5 +230,6 @@ public static void main(String[] args) {
230230
} finally {
231231
txn.close();
232232
}
233+
System.exit(0);
233234
}
234235
}

framework/db/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@
2828
<artifactId>javax.persistence</artifactId>
2929
</dependency>
3030
<dependency>
31-
<groupId>commons-dbcp</groupId>
32-
<artifactId>commons-dbcp</artifactId>
31+
<groupId>org.apache.commons</groupId>
32+
<artifactId>commons-dbcp2</artifactId>
3333
</dependency>
3434
<dependency>
3535
<groupId>commons-io</groupId>
3636
<artifactId>commons-io</artifactId>
3737
<version>${cs.commons-io.version}</version>
3838
</dependency>
3939
<dependency>
40-
<groupId>commons-pool</groupId>
41-
<artifactId>commons-pool</artifactId>
40+
<groupId>org.apache.commons</groupId>
41+
<artifactId>commons-pool2</artifactId>
4242
</dependency>
4343
<dependency>
4444
<groupId>org.apache.cloudstack</groupId>

framework/db/src/main/java/com/cloud/utils/db/TransactionLegacy.java

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333

3434
import javax.sql.DataSource;
3535

36-
import org.apache.commons.dbcp.ConnectionFactory;
37-
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
38-
import org.apache.commons.dbcp.PoolableConnectionFactory;
39-
import org.apache.commons.dbcp.PoolingDataSource;
40-
import org.apache.commons.pool.KeyedObjectPoolFactory;
41-
import org.apache.commons.pool.impl.GenericObjectPool;
42-
import org.apache.commons.pool.impl.StackKeyedObjectPoolFactory;
36+
import org.apache.commons.dbcp2.ConnectionFactory;
37+
import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
38+
import org.apache.commons.dbcp2.PoolableConnection;
39+
import org.apache.commons.dbcp2.PoolableConnectionFactory;
40+
import org.apache.commons.dbcp2.PoolingDataSource;
41+
import org.apache.commons.pool2.ObjectPool;
42+
import org.apache.commons.pool2.impl.GenericObjectPool;
43+
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
4344
import org.apache.log4j.Logger;
4445

4546
import com.cloud.utils.Pair;
@@ -1079,24 +1080,15 @@ public static void initDataSource(Properties dbProps) {
10791080
System.setProperty("javax.net.ssl.trustStorePassword", dbProps.getProperty("db.cloud.trustStorePassword"));
10801081
}
10811082

1082-
final GenericObjectPool cloudConnectionPool =
1083-
new GenericObjectPool(null, cloudMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, cloudMaxWait, cloudMaxIdle, cloudTestOnBorrow, false,
1084-
cloudTimeBtwEvictionRunsMillis, 1, cloudMinEvcitableIdleTimeMillis, cloudTestWhileIdle);
1085-
10861083
final String cloudConnectionUri = cloudDriver + "://" + cloudHost + (s_dbHAEnabled ? "," + cloudSlaves : "") + ":" + cloudPort + "/" + cloudDbName +
10871084
"?autoReconnect=" + cloudAutoReconnect + (url != null ? "&" + url : "") + (useSSL ? "&useSSL=true" : "") +
10881085
(s_dbHAEnabled ? "&" + cloudDbHAParams : "") + (s_dbHAEnabled ? "&loadBalanceStrategy=" + loadBalanceStrategy : "");
10891086
DriverLoader.loadDriver(cloudDriver);
10901087

1091-
final ConnectionFactory cloudConnectionFactory = new DriverManagerConnectionFactory(cloudConnectionUri, cloudUsername, cloudPassword);
1092-
1093-
final KeyedObjectPoolFactory poolableObjFactory = (cloudPoolPreparedStatements ? new StackKeyedObjectPoolFactory() : null);
1094-
1095-
final PoolableConnectionFactory cloudPoolableConnectionFactory =
1096-
new PoolableConnectionFactory(cloudConnectionFactory, cloudConnectionPool, poolableObjFactory, cloudValidationQuery, false, false, isolationLevel);
1097-
10981088
// Default Data Source for CloudStack
1099-
s_ds = new PoolingDataSource(cloudPoolableConnectionFactory.getPool());
1089+
s_ds = createDataSource(cloudConnectionUri, cloudUsername, cloudPassword, cloudMaxActive, cloudMaxIdle, cloudMaxWait,
1090+
cloudTimeBtwEvictionRunsMillis, cloudMinEvcitableIdleTimeMillis, cloudTestWhileIdle, cloudTestOnBorrow,
1091+
cloudValidationQuery, isolationLevel);
11001092

11011093
// Configure the usage db
11021094
final int usageMaxActive = Integer.parseInt(dbProps.getProperty("db.usage.maxActive"));
@@ -1111,21 +1103,15 @@ public static void initDataSource(Properties dbProps) {
11111103
final boolean usageAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.usage.autoReconnect"));
11121104
final String usageUrl = dbProps.getProperty("db.usage.url.params");
11131105

1114-
final GenericObjectPool usageConnectionPool =
1115-
new GenericObjectPool(null, usageMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, usageMaxWait, usageMaxIdle);
1116-
11171106
final String usageConnectionUri = usageDriver + "://" + usageHost + (s_dbHAEnabled ? "," + dbProps.getProperty("db.cloud.slaves") : "") + ":" + usagePort +
11181107
"/" + usageDbName + "?autoReconnect=" + usageAutoReconnect + (usageUrl != null ? "&" + usageUrl : "") +
11191108
(s_dbHAEnabled ? "&" + getDBHAParams("usage", dbProps) : "") + (s_dbHAEnabled ? "&loadBalanceStrategy=" + loadBalanceStrategy : "");
11201109
DriverLoader.loadDriver(usageDriver);
11211110

1122-
final ConnectionFactory usageConnectionFactory = new DriverManagerConnectionFactory(usageConnectionUri, usageUsername, usagePassword);
1123-
1124-
final PoolableConnectionFactory usagePoolableConnectionFactory =
1125-
new PoolableConnectionFactory(usageConnectionFactory, usageConnectionPool, new StackKeyedObjectPoolFactory(), null, false, false);
1126-
11271111
// Data Source for usage server
1128-
s_usageDS = new PoolingDataSource(usagePoolableConnectionFactory.getPool());
1112+
s_usageDS = createDataSource(usageConnectionUri, usageUsername, usagePassword,
1113+
usageMaxActive, usageMaxIdle, usageMaxWait, null, null, null, null,
1114+
null, isolationLevel);
11291115

11301116
try {
11311117
// Configure the simulator db
@@ -1140,18 +1126,12 @@ public static void initDataSource(Properties dbProps) {
11401126
final String simulatorDbName = dbProps.getProperty("db.simulator.name");
11411127
final boolean simulatorAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.simulator.autoReconnect"));
11421128

1143-
final GenericObjectPool simulatorConnectionPool =
1144-
new GenericObjectPool(null, simulatorMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, simulatorMaxWait, simulatorMaxIdle);
1145-
11461129
final String simulatorConnectionUri = simulatorDriver + "://" + simulatorHost + ":" + simulatorPort + "/" + simulatorDbName + "?autoReconnect=" +
11471130
simulatorAutoReconnect;
11481131
DriverLoader.loadDriver(simulatorDriver);
11491132

1150-
final ConnectionFactory simulatorConnectionFactory = new DriverManagerConnectionFactory(simulatorConnectionUri, simulatorUsername, simulatorPassword);
1151-
1152-
final PoolableConnectionFactory simulatorPoolableConnectionFactory =
1153-
new PoolableConnectionFactory(simulatorConnectionFactory, simulatorConnectionPool, new StackKeyedObjectPoolFactory(), null, false, false);
1154-
s_simulatorDS = new PoolingDataSource(simulatorPoolableConnectionFactory.getPool());
1133+
s_simulatorDS = createDataSource(simulatorConnectionUri, simulatorUsername, simulatorPassword,
1134+
simulatorMaxActive, simulatorMaxIdle, simulatorMaxWait, null, null, null, null, cloudValidationQuery, isolationLevel);
11551135
} catch (Exception e) {
11561136
s_logger.debug("Simulator DB properties are not available. Not initializing simulator DS");
11571137
}
@@ -1165,6 +1145,54 @@ public static void initDataSource(Properties dbProps) {
11651145
}
11661146
}
11671147

1148+
/**
1149+
* Creates a data source
1150+
*/
1151+
private static DataSource createDataSource(String uri, String username, String password,
1152+
Integer maxActive, Integer maxIdle, Long maxWait,
1153+
Long timeBtwnEvictionRuns, Long minEvictableIdleTime,
1154+
Boolean testWhileIdle, Boolean testOnBorrow,
1155+
String validationQuery, Integer isolationLevel) {
1156+
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(uri, username, password);
1157+
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
1158+
GenericObjectPoolConfig config = createPoolConfig(maxActive, maxIdle, maxWait, timeBtwnEvictionRuns, minEvictableIdleTime, testWhileIdle, testOnBorrow);
1159+
ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, config);
1160+
poolableConnectionFactory.setPool(connectionPool);
1161+
if (validationQuery != null) {
1162+
poolableConnectionFactory.setValidationQuery(validationQuery);
1163+
}
1164+
if (isolationLevel != null) {
1165+
poolableConnectionFactory.setDefaultTransactionIsolation(isolationLevel);
1166+
}
1167+
return new PoolingDataSource<>(connectionPool);
1168+
}
1169+
1170+
/**
1171+
* Return a GenericObjectPoolConfig configuration usable on connection pool creation
1172+
*/
1173+
private static GenericObjectPoolConfig createPoolConfig(Integer maxActive, Integer maxIdle, Long maxWait,
1174+
Long timeBtwnEvictionRuns, Long minEvictableIdleTime,
1175+
Boolean testWhileIdle, Boolean testOnBorrow) {
1176+
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
1177+
config.setMaxTotal(maxActive);
1178+
config.setMaxIdle(maxIdle);
1179+
config.setMaxWaitMillis(maxWait);
1180+
1181+
if (timeBtwnEvictionRuns != null) {
1182+
config.setTimeBetweenEvictionRunsMillis(timeBtwnEvictionRuns);
1183+
}
1184+
if (minEvictableIdleTime != null) {
1185+
config.setMinEvictableIdleTimeMillis(minEvictableIdleTime);
1186+
}
1187+
if (testWhileIdle != null) {
1188+
config.setTestWhileIdle(testWhileIdle);
1189+
}
1190+
if (testOnBorrow != null) {
1191+
config.setTestOnBorrow(testOnBorrow);
1192+
}
1193+
return config;
1194+
}
1195+
11681196
private static String getDBHAParams(String dbName, Properties dbProps) {
11691197
StringBuilder sb = new StringBuilder();
11701198
sb.append("failOverReadOnly=" + dbProps.getProperty("db." + dbName + ".failOverReadOnly"));
@@ -1178,11 +1206,10 @@ private static String getDBHAParams(String dbName, Properties dbProps) {
11781206

11791207
@SuppressWarnings({"unchecked", "rawtypes"})
11801208
private static DataSource getDefaultDataSource(final String database) {
1181-
final GenericObjectPool connectionPool = new GenericObjectPool(null, 5);
11821209
final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://localhost:3306/" + database, "cloud", "cloud");
1183-
final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
1184-
return new PoolingDataSource(
1185-
/* connectionPool */poolableConnectionFactory.getPool());
1210+
final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
1211+
final GenericObjectPool connectionPool = new GenericObjectPool(poolableConnectionFactory);
1212+
return new PoolingDataSource(connectionPool);
11861213
}
11871214

11881215
/**

pom.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
<cs.log4j.version>1.2.17</cs.log4j.version>
5151
<cs.log4j.extras.version>1.2.17</cs.log4j.extras.version>
5252
<cs.cglib.version>3.2.5</cs.cglib.version>
53-
<cs.dbcp.version>1.4</cs.dbcp.version>
54-
<cs.pool.version>1.6</cs.pool.version>
53+
<cs.dbcp.version>2.2.0</cs.dbcp.version>
54+
<cs.pool.version>2.4.3</cs.pool.version>
5555
<cs.codec.version>1.11</cs.codec.version>
5656
<cs.configuration.version>1.10</cs.configuration.version>
5757
<cs.logging.version>1.1.1</cs.logging.version>
@@ -271,13 +271,13 @@
271271
<version>${cs.cglib.version}</version>
272272
</dependency>
273273
<dependency>
274-
<groupId>commons-dbcp</groupId>
275-
<artifactId>commons-dbcp</artifactId>
274+
<groupId>org.apache.commons</groupId>
275+
<artifactId>commons-dbcp2</artifactId>
276276
<version>${cs.dbcp.version}</version>
277277
<exclusions>
278278
<exclusion>
279-
<artifactId>commons-pool</artifactId>
280-
<groupId>commons-pool</groupId>
279+
<artifactId>org.apache.commons</artifactId>
280+
<groupId>commons-pool2</groupId>
281281
</exclusion>
282282
</exclusions>
283283
</dependency>
@@ -297,8 +297,8 @@
297297
<version>${cs.commons-fileupload.version}</version>
298298
</dependency>
299299
<dependency>
300-
<groupId>commons-pool</groupId>
301-
<artifactId>commons-pool</artifactId>
300+
<groupId>org.apache.commons</groupId>
301+
<artifactId>commons-pool2</artifactId>
302302
<version>${cs.pool.version}</version>
303303
</dependency>
304304
<dependency>

0 commit comments

Comments
 (0)