Transaction: Fix static, add method to reinit static datasource off a db props file

Signed-off-by: Rohit Yadav <bhaisaab@apache.org>
This commit is contained in:
Rohit Yadav 2013-01-18 18:08:11 -08:00
parent e688fbfc24
commit 7fbe935c36
1 changed files with 76 additions and 44 deletions

View File

@ -18,6 +18,7 @@ package com.cloud.utils.db;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -996,19 +997,32 @@ public class Transaction {
private static DataSource s_usageDS;
private static DataSource s_awsapiDS;
private static DataSource s_simulatorDS;
static {
// Initialize with assumed db.properties file
initDataSource("db.properties");
}
public static void initDataSource(String propsFileName) {
try {
final File dbPropsFile = PropertiesUtil.findConfigFile("db.properties");
final Properties dbProps;
if(EncryptionSecretKeyChecker.useEncryption()){
StandardPBEStringEncryptor encryptor = EncryptionSecretKeyChecker.getEncryptor();
dbProps = new EncryptableProperties(encryptor);
} else {
dbProps = new Properties();
File dbPropsFile = new File(propsFileName);
if (!dbPropsFile.exists()) {
dbPropsFile = PropertiesUtil.findConfigFile(propsFileName);
}
dbProps.load(new FileInputStream(dbPropsFile));
final Properties dbProps;
if (EncryptionSecretKeyChecker.useEncryption()) {
StandardPBEStringEncryptor encryptor = EncryptionSecretKeyChecker.getEncryptor();
dbProps = new EncryptableProperties(encryptor);
} else {
dbProps = new Properties();
}
try {
dbProps.load(new FileInputStream(dbPropsFile));
} catch (IOException e) {
s_logger.fatal("Unable to load db properties file, pl. check the classpath and file path configuration", e);
return;
}
// FIXME: If params are missing...default them????
final int cloudMaxActive = Integer.parseInt(dbProps.getProperty("db.cloud.maxActive"));
final int cloudMaxIdle = Integer.parseInt(dbProps.getProperty("db.cloud.maxIdle"));
@ -1021,6 +1035,7 @@ public class Transaction {
final boolean cloudAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.cloud.autoReconnect"));
final String cloudValidationQuery = dbProps.getProperty("db.cloud.validationQuery");
final String cloudIsolationLevel = dbProps.getProperty("db.cloud.isolation.level");
int isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
if (cloudIsolationLevel == null) {
isolationLevel = Connection.TRANSACTION_READ_COMMITTED;
@ -1035,14 +1050,16 @@ public class Transaction {
} else {
s_logger.warn("Unknown isolation level " + cloudIsolationLevel + ". Using read uncommitted");
}
final boolean cloudTestOnBorrow = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testOnBorrow"));
final boolean cloudTestWhileIdle = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testWhileIdle"));
final long cloudTimeBtwEvictionRunsMillis = Long.parseLong(dbProps.getProperty("db.cloud.timeBetweenEvictionRunsMillis"));
final long cloudMinEvcitableIdleTimeMillis = Long.parseLong(dbProps.getProperty("db.cloud.minEvictableIdleTimeMillis"));
final boolean cloudPoolPreparedStatements = Boolean.parseBoolean(dbProps.getProperty("db.cloud.poolPreparedStatements"));
final String url = dbProps.getProperty("db.cloud.url.params");
final boolean useSSL = Boolean.parseBoolean(dbProps.getProperty("db.cloud.useSSL"));
if(useSSL){
if (useSSL) {
System.setProperty("javax.net.ssl.keyStore", dbProps.getProperty("db.cloud.keyStore"));
System.setProperty("javax.net.ssl.keyStorePassword", dbProps.getProperty("db.cloud.keyStorePassword"));
System.setProperty("javax.net.ssl.trustStore", dbProps.getProperty("db.cloud.trustStore"));
@ -1051,14 +1068,19 @@ public class Transaction {
final GenericObjectPool cloudConnectionPool = new GenericObjectPool(null, cloudMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
cloudMaxWait, cloudMaxIdle, cloudTestOnBorrow, false, cloudTimeBtwEvictionRunsMillis, 1, cloudMinEvcitableIdleTimeMillis, cloudTestWhileIdle);
final ConnectionFactory cloudConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://"+cloudHost + ":" + cloudPort + "/" + cloudDbName +
"?autoReconnect="+cloudAutoReconnect + (url != null ? "&" + url : "")+ (useSSL ? "&useSSL=true" : ""), cloudUsername, cloudPassword);
final ConnectionFactory cloudConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + cloudHost + ":" + cloudPort + "/" + cloudDbName +
"?autoReconnect=" + cloudAutoReconnect + (url != null ? "&" + url : "") + (useSSL ? "&useSSL=true" : ""), cloudUsername, cloudPassword);
final KeyedObjectPoolFactory poolableObjFactory = (cloudPoolPreparedStatements ? new StackKeyedObjectPoolFactory() : null);
final PoolableConnectionFactory cloudPoolableConnectionFactory = new PoolableConnectionFactory(cloudConnectionFactory, cloudConnectionPool, poolableObjFactory,
cloudValidationQuery, false, false, isolationLevel);
// Default Data Source for CloudStack
s_ds = new PoolingDataSource(cloudPoolableConnectionFactory.getPool());
// configure the usage db
// Configure the usage db
final int usageMaxActive = Integer.parseInt(dbProps.getProperty("db.usage.maxActive"));
final int usageMaxIdle = Integer.parseInt(dbProps.getProperty("db.usage.maxIdle"));
final long usageMaxWait = Long.parseLong(dbProps.getProperty("db.usage.maxWait"));
@ -1068,50 +1090,60 @@ public class Transaction {
final int usagePort = Integer.parseInt(dbProps.getProperty("db.usage.port"));
final String usageDbName = dbProps.getProperty("db.usage.name");
final boolean usageAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.usage.autoReconnect"));
final GenericObjectPool usageConnectionPool = new GenericObjectPool(null, usageMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
usageMaxWait, usageMaxIdle);
final ConnectionFactory usageConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://"+usageHost + ":" + usagePort + "/" + usageDbName +
"?autoReconnect="+usageAutoReconnect, usageUsername, usagePassword);
final ConnectionFactory usageConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + usageHost + ":" + usagePort + "/" + usageDbName +
"?autoReconnect=" + usageAutoReconnect, usageUsername, usagePassword);
final PoolableConnectionFactory usagePoolableConnectionFactory = new PoolableConnectionFactory(usageConnectionFactory, usageConnectionPool,
new StackKeyedObjectPoolFactory(), null, false, false);
// Data Source for usage server
s_usageDS = new PoolingDataSource(usagePoolableConnectionFactory.getPool());
//configure awsapi db
// Configure awsapi db
final String awsapiDbName = dbProps.getProperty("db.awsapi.name");
final GenericObjectPool awsapiConnectionPool = new GenericObjectPool(null, usageMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
usageMaxWait, usageMaxIdle);
final ConnectionFactory awsapiConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://"+cloudHost + ":" + cloudPort + "/" + awsapiDbName +
"?autoReconnect="+usageAutoReconnect, cloudUsername, cloudPassword);
usageMaxWait, usageMaxIdle);
final ConnectionFactory awsapiConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + cloudHost + ":" + cloudPort + "/" + awsapiDbName +
"?autoReconnect=" + usageAutoReconnect, cloudUsername, cloudPassword);
final PoolableConnectionFactory awsapiPoolableConnectionFactory = new PoolableConnectionFactory(awsapiConnectionFactory, awsapiConnectionPool,
new StackKeyedObjectPoolFactory(), null, false, false);
new StackKeyedObjectPoolFactory(), null, false, false);
// Data Source for awsapi
s_awsapiDS = new PoolingDataSource(awsapiPoolableConnectionFactory.getPool());
try{
// configure the simulator db
final int simulatorMaxActive = Integer.parseInt(dbProps.getProperty("db.simulator.maxActive"));
final int simulatorMaxIdle = Integer.parseInt(dbProps.getProperty("db.simulator.maxIdle"));
final long simulatorMaxWait = Long.parseLong(dbProps.getProperty("db.simulator.maxWait"));
final String simulatorUsername = dbProps.getProperty("db.simulator.username");
final String simulatorPassword = dbProps.getProperty("db.simulator.password");
final String simulatorHost = dbProps.getProperty("db.simulator.host");
final int simulatorPort = Integer.parseInt(dbProps.getProperty("db.simulator.port"));
final String simulatorDbName = dbProps.getProperty("db.simulator.name");
final boolean simulatorAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.simulator.autoReconnect"));
final GenericObjectPool simulatorConnectionPool = new GenericObjectPool(null, simulatorMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
simulatorMaxWait, simulatorMaxIdle);
final ConnectionFactory simulatorConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://"+simulatorHost + ":" + simulatorPort + "/" + simulatorDbName +
"?autoReconnect="+simulatorAutoReconnect, simulatorUsername, simulatorPassword);
final PoolableConnectionFactory simulatorPoolableConnectionFactory = new PoolableConnectionFactory(simulatorConnectionFactory, simulatorConnectionPool,
new StackKeyedObjectPoolFactory(), null, false, false);
s_simulatorDS = new PoolingDataSource(simulatorPoolableConnectionFactory.getPool());
} catch (Exception e){
s_logger.debug("Simulator DB properties are not available. Not initializing simulator DS");
try {
// Configure the simulator db
final int simulatorMaxActive = Integer.parseInt(dbProps.getProperty("db.simulator.maxActive"));
final int simulatorMaxIdle = Integer.parseInt(dbProps.getProperty("db.simulator.maxIdle"));
final long simulatorMaxWait = Long.parseLong(dbProps.getProperty("db.simulator.maxWait"));
final String simulatorUsername = dbProps.getProperty("db.simulator.username");
final String simulatorPassword = dbProps.getProperty("db.simulator.password");
final String simulatorHost = dbProps.getProperty("db.simulator.host");
final int simulatorPort = Integer.parseInt(dbProps.getProperty("db.simulator.port"));
final String simulatorDbName = dbProps.getProperty("db.simulator.name");
final boolean simulatorAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.simulator.autoReconnect"));
final GenericObjectPool simulatorConnectionPool = new GenericObjectPool(null, simulatorMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION,
simulatorMaxWait, simulatorMaxIdle);
final ConnectionFactory simulatorConnectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://" + simulatorHost + ":" + simulatorPort + "/" + simulatorDbName +
"?autoReconnect=" + simulatorAutoReconnect, simulatorUsername, simulatorPassword);
final PoolableConnectionFactory simulatorPoolableConnectionFactory = new PoolableConnectionFactory(simulatorConnectionFactory, simulatorConnectionPool,
new StackKeyedObjectPoolFactory(), null, false, false);
s_simulatorDS = new PoolingDataSource(simulatorPoolableConnectionFactory.getPool());
} catch (Exception e) {
s_logger.debug("Simulator DB properties are not available. Not initializing simulator DS");
}
} catch (final Exception e) {
s_ds = getDefaultDataSource("cloud");
s_usageDS = getDefaultDataSource("cloud_usage");
s_simulatorDS = getDefaultDataSource("cloud_simulator");
s_logger.warn("Unable to load db configuration, using defaults with 5 connections. Please check your configuration", e);
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);
}
}