@@ -80,6 +80,7 @@ public class Transaction {
8080 public static final short CLOUD_DB = 0 ;
8181 public static final short USAGE_DB = 1 ;
8282 public static final short AWSAPI_DB = 2 ;
83+ public static final short SIMULATOR_DB = 3 ;
8384 public static final short CONNECTED_DB = -1 ;
8485
8586 private static AtomicLong s_id = new AtomicLong ();
@@ -224,6 +225,7 @@ public static Connection getStandaloneUsageConnection() {
224225 return null ;
225226 }
226227 }
228+
227229 public static Connection getStandaloneAwsapiConnection () {
228230 try {
229231 Connection conn = s_awsapiDS .getConnection ();
@@ -235,7 +237,21 @@ public static Connection getStandaloneAwsapiConnection() {
235237 s_logger .warn ("Unexpected exception: " , e );
236238 return null ;
237239 }
238- }
240+ }
241+
242+ public static Connection getStandaloneSimulatorConnection () {
243+ try {
244+ Connection conn = s_simulatorDS .getConnection ();
245+ if (s_connLogger .isTraceEnabled ()) {
246+ s_connLogger .trace ("Retrieving a standalone connection for simulator: dbconn" + System .identityHashCode (conn ));
247+ }
248+ return conn ;
249+ } catch (SQLException e ) {
250+ s_logger .warn ("Unexpected exception: " , e );
251+ return null ;
252+ }
253+ }
254+
239255 protected void attach (TransactionAttachment value ) {
240256 _stack .push (new StackElement (ATTACHMENT , value ));
241257 }
@@ -546,6 +562,14 @@ public Connection getConnection() throws SQLException {
546562 }
547563 break ;
548564
565+ case SIMULATOR_DB :
566+ if (s_simulatorDS != null ) {
567+ _conn = s_simulatorDS .getConnection ();
568+ } else {
569+ s_logger .warn ("A static-initialized variable becomes null, process is dying?" );
570+ throw new CloudRuntimeException ("Database is not initialized, process is dying?" );
571+ }
572+ break ;
549573 default :
550574
551575 throw new CloudRuntimeException ("No database selected for the transaction" );
@@ -976,6 +1000,7 @@ public String toString() {
9761000 private static DataSource s_ds ;
9771001 private static DataSource s_usageDS ;
9781002 private static DataSource s_awsapiDS ;
1003+ private static DataSource s_simulatorDS ;
9791004 static {
9801005 try {
9811006 final File dbPropsFile = PropertiesUtil .findConfigFile ("db.properties" );
@@ -1069,6 +1094,27 @@ public String toString() {
10691094 new StackKeyedObjectPoolFactory (), null , false , false );
10701095 s_awsapiDS = new PoolingDataSource (awsapiPoolableConnectionFactory .getPool ());
10711096
1097+ try {
1098+ // configure the simulator db
1099+ final int simulatorMaxActive = Integer .parseInt (dbProps .getProperty ("db.simulator.maxActive" ));
1100+ final int simulatorMaxIdle = Integer .parseInt (dbProps .getProperty ("db.simulator.maxIdle" ));
1101+ final long simulatorMaxWait = Long .parseLong (dbProps .getProperty ("db.simulator.maxWait" ));
1102+ final String simulatorUsername = dbProps .getProperty ("db.simulator.username" );
1103+ final String simulatorPassword = dbProps .getProperty ("db.simulator.password" );
1104+ final String simulatorHost = dbProps .getProperty ("db.simulator.host" );
1105+ final int simulatorPort = Integer .parseInt (dbProps .getProperty ("db.simulator.port" ));
1106+ final String simulatorDbName = dbProps .getProperty ("db.simulator.name" );
1107+ final boolean simulatorAutoReconnect = Boolean .parseBoolean (dbProps .getProperty ("db.simulator.autoReconnect" ));
1108+ final GenericObjectPool simulatorConnectionPool = new GenericObjectPool (null , simulatorMaxActive , GenericObjectPool .DEFAULT_WHEN_EXHAUSTED_ACTION ,
1109+ simulatorMaxWait , simulatorMaxIdle );
1110+ final ConnectionFactory simulatorConnectionFactory = new DriverManagerConnectionFactory ("jdbc:mysql://" +simulatorHost + ":" + simulatorPort + "/" + simulatorDbName +
1111+ "?autoReconnect=" +simulatorAutoReconnect , simulatorUsername , simulatorPassword );
1112+ final PoolableConnectionFactory simulatorPoolableConnectionFactory = new PoolableConnectionFactory (simulatorConnectionFactory , simulatorConnectionPool ,
1113+ new StackKeyedObjectPoolFactory (), null , false , false );
1114+ s_simulatorDS = new PoolingDataSource (simulatorPoolableConnectionFactory .getPool ());
1115+ } catch (Exception e ){
1116+ s_logger .debug ("Simulator DB properties are not available. Not initializing simulator DS" );
1117+ }
10721118 } catch (final Exception e ) {
10731119 final GenericObjectPool connectionPool = new GenericObjectPool (null , 5 );
10741120 final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory ("jdbc:mysql://localhost:3306/cloud" , "cloud" , "cloud" );
@@ -1079,6 +1125,11 @@ public String toString() {
10791125 final ConnectionFactory connectionFactoryUsage = new DriverManagerConnectionFactory ("jdbc:mysql://localhost:3306/cloud_usage" , "cloud" , "cloud" );
10801126 final PoolableConnectionFactory poolableConnectionFactoryUsage = new PoolableConnectionFactory (connectionFactoryUsage , connectionPoolUsage , null , null , false , true );
10811127 s_usageDS = new PoolingDataSource (poolableConnectionFactoryUsage .getPool ());
1128+
1129+ final GenericObjectPool connectionPoolsimulator = new GenericObjectPool (null , 5 );
1130+ final ConnectionFactory connectionFactorysimulator = new DriverManagerConnectionFactory ("jdbc:mysql://localhost:3306/cloud_simulator" , "cloud" , "cloud" );
1131+ final PoolableConnectionFactory poolableConnectionFactorysimulator = new PoolableConnectionFactory (connectionFactorysimulator , connectionPoolsimulator , null , null , false , true );
1132+ s_simulatorDS = new PoolingDataSource (poolableConnectionFactorysimulator .getPool ());
10821133 s_logger .warn ("Unable to load db configuration, using defaults with 5 connections. Please check your configuration" , e );
10831134 }
10841135 }
0 commit comments