I have a stand alone Java Program [ No application server/session involved] using Oracle DB connection and BoneCP . I need to run different Queries from different methods and I don't want the DB connection/pool created in each call.
I have a DBManager class where the connection pool is created. I would like to know how exactly I can reuse the connection object created to run mutiple queries from different classes/methods.
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e) {
e.printStackTrace();
return;
}
try {
// setup the connection pool
BoneCPConfig config = new BoneCPConfig();
config.setJdbcUrl( "jdbc:oracle:thin:@"+prop.getProperty(DB_HOST)+":"+prop.getProperty(DB_PORT)+":"+prop.getProperty(DB_INSTANCE)); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
config.setUsername( prop.getProperty(APP_DB_USER));
config.setPassword(prop.getProperty(APP_DB_PWD));
config.setMinConnectionsPerPartition(5);
config.setMaxConnectionsPerPartition(10);
config.setPartitionCount(1);
connectionPool = new BoneCP(config); // setup the connection pool
connection = connectionPool.getConnection(); // fetch a connection
If I call the object of this class / even if its a static class won't the CP get created every time ? Hoew exactly I can reuse the connection object by creating the connection only once ?
Thanks in advance.