Skip to content

Commit 7fbe935

Browse files
committed
Transaction: Fix static, add method to reinit static datasource off a db props file
Signed-off-by: Rohit Yadav <[email protected]>
1 parent e688fbf commit 7fbe935

1 file changed

Lines changed: 75 additions & 43 deletions

File tree

utils/src/com/cloud/utils/db/Transaction.java

Lines changed: 75 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.FileInputStream;
21+
import java.io.IOException;
2122
import java.sql.Connection;
2223
import java.sql.PreparedStatement;
2324
import java.sql.ResultSet;
@@ -996,19 +997,32 @@ public String toString() {
996997
private static DataSource s_usageDS;
997998
private static DataSource s_awsapiDS;
998999
private static DataSource s_simulatorDS;
1000+
9991001
static {
1002+
// Initialize with assumed db.properties file
1003+
initDataSource("db.properties");
1004+
}
1005+
1006+
public static void initDataSource(String propsFileName) {
10001007
try {
1001-
final File dbPropsFile = PropertiesUtil.findConfigFile("db.properties");
1002-
final Properties dbProps;
1003-
1004-
if(EncryptionSecretKeyChecker.useEncryption()){
1005-
StandardPBEStringEncryptor encryptor = EncryptionSecretKeyChecker.getEncryptor();
1006-
dbProps = new EncryptableProperties(encryptor);
1008+
File dbPropsFile = new File(propsFileName);
1009+
if (!dbPropsFile.exists()) {
1010+
dbPropsFile = PropertiesUtil.findConfigFile(propsFileName);
1011+
}
1012+
final Properties dbProps;
1013+
if (EncryptionSecretKeyChecker.useEncryption()) {
1014+
StandardPBEStringEncryptor encryptor = EncryptionSecretKeyChecker.getEncryptor();
1015+
dbProps = new EncryptableProperties(encryptor);
10071016
} else {
1008-
dbProps = new Properties();
1017+
dbProps = new Properties();
1018+
}
1019+
try {
1020+
dbProps.load(new FileInputStream(dbPropsFile));
1021+
} catch (IOException e) {
1022+
s_logger.fatal("Unable to load db properties file, pl. check the classpath and file path configuration", e);
1023+
return;
10091024
}
1010-
dbProps.load(new FileInputStream(dbPropsFile));
1011-
1025+
10121026
// FIXME: If params are missing...default them????
10131027
final int cloudMaxActive = Integer.parseInt(dbProps.getProperty("db.cloud.maxActive"));
10141028
final int cloudMaxIdle = Integer.parseInt(dbProps.getProperty("db.cloud.maxIdle"));
@@ -1021,6 +1035,7 @@ public String toString() {
10211035
final boolean cloudAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.cloud.autoReconnect"));
10221036
final String cloudValidationQuery = dbProps.getProperty("db.cloud.validationQuery");
10231037
final String cloudIsolationLevel = dbProps.getProperty("db.cloud.isolation.level");
1038+
10241039
int isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
10251040
if (cloudIsolationLevel == null) {
10261041
isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
@@ -1035,14 +1050,16 @@ public String toString() {
10351050
} else {
10361051
s_logger.warn("Unknown isolation level " + cloudIsolationLevel + ". Using read uncommitted");
10371052
}
1053+
10381054
final boolean cloudTestOnBorrow = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testOnBorrow"));
10391055
final boolean cloudTestWhileIdle = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testWhileIdle"));
10401056
final long cloudTimeBtwEvictionRunsMillis = Long.parseLong(dbProps.getProperty("db.cloud.timeBetweenEvictionRunsMillis"));
10411057
final long cloudMinEvcitableIdleTimeMillis = Long.parseLong(dbProps.getProperty("db.cloud.minEvictableIdleTimeMillis"));
10421058
final boolean cloudPoolPreparedStatements = Boolean.parseBoolean(dbProps.getProperty("db.cloud.poolPreparedStatements"));
10431059
final String url = dbProps.getProperty("db.cloud.url.params");
1060+
10441061
final boolean useSSL = Boolean.parseBoolean(dbProps.getProperty("db.cloud.useSSL"));
1045-
if(useSSL){
1062+
if (useSSL) {
10461063
System.setProperty("javax.net.ssl.keyStore", dbProps.getProperty("db.cloud.keyStore"));
10471064
System.setProperty("javax.net.ssl.keyStorePassword", dbProps.getProperty("db.cloud.keyStorePassword"));
10481065
System.setProperty("javax.net.ssl.trustStore", dbProps.getProperty("db.cloud.trustStore"));
@@ -1051,14 +1068,19 @@ public String toString() {
10511068

10521069
final GenericObjectPool cloudConnectionPool = new GenericObjectPool(null, cloudMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
10531070
cloudMaxWait, cloudMaxIdle, cloudTestOnBorrow, false, cloudTimeBtwEvictionRunsMillis, 1, cloudMinEvcitableIdleTimeMillis, cloudTestWhileIdle);
1054-
final ConnectionFactory cloudConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://"+cloudHost + ":" + cloudPort + "/" + cloudDbName +
1055-
"?autoReconnect="+cloudAutoReconnect + (url != null ? "&" + url : "")+ (useSSL ? "&useSSL=true" : ""), cloudUsername, cloudPassword);
1071+
1072+
final ConnectionFactory cloudConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + cloudHost + ":" + cloudPort + "/" + cloudDbName +
1073+
"?autoReconnect=" + cloudAutoReconnect + (url != null ? "&" + url : "") + (useSSL ? "&useSSL=true" : ""), cloudUsername, cloudPassword);
1074+
10561075
final KeyedObjectPoolFactory poolableObjFactory = (cloudPoolPreparedStatements ? new StackKeyedObjectPoolFactory() : null);
1076+
10571077
final PoolableConnectionFactory cloudPoolableConnectionFactory = new PoolableConnectionFactory(cloudConnectionFactory, cloudConnectionPool, poolableObjFactory,
10581078
cloudValidationQuery, false, false, isolationLevel);
1079+
1080+
// Default Data Source for CloudStack
10591081
s_ds = new PoolingDataSource(cloudPoolableConnectionFactory.getPool());
10601082

1061-
// configure the usage db
1083+
// Configure the usage db
10621084
final int usageMaxActive = Integer.parseInt(dbProps.getProperty("db.usage.maxActive"));
10631085
final int usageMaxIdle = Integer.parseInt(dbProps.getProperty("db.usage.maxIdle"));
10641086
final long usageMaxWait = Long.parseLong(dbProps.getProperty("db.usage.maxWait"));
@@ -1068,50 +1090,60 @@ public String toString() {
10681090
final int usagePort = Integer.parseInt(dbProps.getProperty("db.usage.port"));
10691091
final String usageDbName = dbProps.getProperty("db.usage.name");
10701092
final boolean usageAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.usage.autoReconnect"));
1093+
10711094
final GenericObjectPool usageConnectionPool = new GenericObjectPool(null, usageMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
10721095
usageMaxWait, usageMaxIdle);
1073-
final ConnectionFactory usageConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://"+usageHost + ":" + usagePort + "/" + usageDbName +
1074-
"?autoReconnect="+usageAutoReconnect, usageUsername, usagePassword);
1096+
1097+
final ConnectionFactory usageConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + usageHost + ":" + usagePort + "/" + usageDbName +
1098+
"?autoReconnect=" + usageAutoReconnect, usageUsername, usagePassword);
1099+
10751100
final PoolableConnectionFactory usagePoolableConnectionFactory = new PoolableConnectionFactory(usageConnectionFactory, usageConnectionPool,
10761101
new StackKeyedObjectPoolFactory(), null, false, false);
1102+
1103+
// Data Source for usage server
10771104
s_usageDS = new PoolingDataSource(usagePoolableConnectionFactory.getPool());
1078-
1079-
//configure awsapi db
1105+
1106+
// Configure awsapi db
10801107
final String awsapiDbName = dbProps.getProperty("db.awsapi.name");
10811108
final GenericObjectPool awsapiConnectionPool = new GenericObjectPool(null, usageMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
1082-
usageMaxWait, usageMaxIdle);
1083-
final ConnectionFactory awsapiConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://"+cloudHost + ":" + cloudPort + "/" + awsapiDbName +
1084-
"?autoReconnect="+usageAutoReconnect, cloudUsername, cloudPassword);
1109+
usageMaxWait, usageMaxIdle);
1110+
final ConnectionFactory awsapiConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + cloudHost + ":" + cloudPort + "/" + awsapiDbName +
1111+
"?autoReconnect=" + usageAutoReconnect, cloudUsername, cloudPassword);
10851112
final PoolableConnectionFactory awsapiPoolableConnectionFactory = new PoolableConnectionFactory(awsapiConnectionFactory, awsapiConnectionPool,
1086-
new StackKeyedObjectPoolFactory(), null, false, false);
1113+
new StackKeyedObjectPoolFactory(), null, false, false);
1114+
1115+
// Data Source for awsapi
10871116
s_awsapiDS = new PoolingDataSource(awsapiPoolableConnectionFactory.getPool());
1088-
1089-
try{
1090-
// configure the simulator db
1091-
final int simulatorMaxActive = Integer.parseInt(dbProps.getProperty("db.simulator.maxActive"));
1092-
final int simulatorMaxIdle = Integer.parseInt(dbProps.getProperty("db.simulator.maxIdle"));
1093-
final long simulatorMaxWait = Long.parseLong(dbProps.getProperty("db.simulator.maxWait"));
1094-
final String simulatorUsername = dbProps.getProperty("db.simulator.username");
1095-
final String simulatorPassword = dbProps.getProperty("db.simulator.password");
1096-
final String simulatorHost = dbProps.getProperty("db.simulator.host");
1097-
final int simulatorPort = Integer.parseInt(dbProps.getProperty("db.simulator.port"));
1098-
final String simulatorDbName = dbProps.getProperty("db.simulator.name");
1099-
final boolean simulatorAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.simulator.autoReconnect"));
1100-
final GenericObjectPool simulatorConnectionPool = new GenericObjectPool(null, simulatorMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
1101-
simulatorMaxWait, simulatorMaxIdle);
1102-
final ConnectionFactory simulatorConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://"+simulatorHost + ":" + simulatorPort + "/" + simulatorDbName +
1103-
"?autoReconnect="+simulatorAutoReconnect, simulatorUsername, simulatorPassword);
1104-
final PoolableConnectionFactory simulatorPoolableConnectionFactory = new PoolableConnectionFactory(simulatorConnectionFactory, simulatorConnectionPool,
1105-
new StackKeyedObjectPoolFactory(), null, false, false);
1106-
s_simulatorDS = new PoolingDataSource(simulatorPoolableConnectionFactory.getPool());
1107-
} catch (Exception e){
1108-
s_logger.debug("Simulator DB properties are not available. Not initializing simulator DS");
1117+
1118+
try {
1119+
// Configure the simulator db
1120+
final int simulatorMaxActive = Integer.parseInt(dbProps.getProperty("db.simulator.maxActive"));
1121+
final int simulatorMaxIdle = Integer.parseInt(dbProps.getProperty("db.simulator.maxIdle"));
1122+
final long simulatorMaxWait = Long.parseLong(dbProps.getProperty("db.simulator.maxWait"));
1123+
final String simulatorUsername = dbProps.getProperty("db.simulator.username");
1124+
final String simulatorPassword = dbProps.getProperty("db.simulator.password");
1125+
final String simulatorHost = dbProps.getProperty("db.simulator.host");
1126+
final int simulatorPort = Integer.parseInt(dbProps.getProperty("db.simulator.port"));
1127+
final String simulatorDbName = dbProps.getProperty("db.simulator.name");
1128+
final boolean simulatorAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.simulator.autoReconnect"));
1129+
1130+
final GenericObjectPool simulatorConnectionPool = new GenericObjectPool(null, simulatorMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
1131+
simulatorMaxWait, simulatorMaxIdle);
1132+
1133+
final ConnectionFactory simulatorConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + simulatorHost + ":" + simulatorPort + "/" + simulatorDbName +
1134+
"?autoReconnect=" + simulatorAutoReconnect, simulatorUsername, simulatorPassword);
1135+
1136+
final PoolableConnectionFactory simulatorPoolableConnectionFactory = new PoolableConnectionFactory(simulatorConnectionFactory, simulatorConnectionPool,
1137+
new StackKeyedObjectPoolFactory(), null, false, false);
1138+
s_simulatorDS = new PoolingDataSource(simulatorPoolableConnectionFactory.getPool());
1139+
} catch (Exception e) {
1140+
s_logger.debug("Simulator DB properties are not available. Not initializing simulator DS");
11091141
}
11101142
} catch (final Exception e) {
11111143
s_ds = getDefaultDataSource("cloud");
11121144
s_usageDS = getDefaultDataSource("cloud_usage");
11131145
s_simulatorDS = getDefaultDataSource("cloud_simulator");
1114-
s_logger.warn("Unable to load db configuration, using defaults with 5 connections. Please check your configuration", e);
1146+
s_logger.warn("Unable to load db configuration, using defaults with 5 connections. Falling back on assumed datasource on localhost:3306 using username:password=cloud:cloud. Please check your configuration", e);
11151147
}
11161148
}
11171149

0 commit comments

Comments
 (0)