dyma82
asked on
Proper use of Datasources
I am working on a J2EE application in WebSphere 5.1
My application uses a datasource to access its database.
This is the method executed every time the app needs to acquire a connection to the databse:
public static Connection getDBConnection(boolean autoCommit) throws NamingException, SQLException
{
Hashtable parms = new Hashtable();
parms.put(Context.INITIAL_ CONTEXT_FA CTORY,"com .ibm.websp here.namin g.WsnIniti alContextF actory");
InitialContext ctx = new InitialContext(parms);
DataSource ds = (DataSource)ctx.lookup("ja va:comp/en v/MyDataSo urce");
Connection conn = ds.getConnection();
if (!autoCommit)
{
conn.setAutoCommit(false);
}
return conn;
}
Question:
Shouldn't it be way more efficient and logical to save the acquired datasource permanently to be used in the future?
Is there any problem with that approach?
I think the following modification of the original code is way more efficient:
public static Connection getDBConnection2(boolean autoCommit) throws NamingException, SQLException
{
Hashtable parms = null;
if(savedDataSource == null) //savedDataSource is a private static field in this class
{
parms = new Hashtable();
parms.put(Context.INITIAL_ CONTEXT_FA CTORY,"com .ibm.websp here.namin g.WsnIniti alContextF actory");
InitialContext ctx = new InitialContext(parms);
savedDataSource = (DataSource)ctx.lookup("ja va:comp/en v/MainAppD ataSource" );
}
Connection conn = savedDataSource.getConnect ion();
if (!autoCommit)
{
conn.setAutoCommit(false);
}
return conn;
}
Does somebody sees any problem with the second version, which keeps the datasource reference for the rest of the application's life?
My application uses a datasource to access its database.
This is the method executed every time the app needs to acquire a connection to the databse:
public static Connection getDBConnection(boolean autoCommit) throws NamingException, SQLException
{
Hashtable parms = new Hashtable();
parms.put(Context.INITIAL_
InitialContext ctx = new InitialContext(parms);
DataSource ds = (DataSource)ctx.lookup("ja
Connection conn = ds.getConnection();
if (!autoCommit)
{
conn.setAutoCommit(false);
}
return conn;
}
Question:
Shouldn't it be way more efficient and logical to save the acquired datasource permanently to be used in the future?
Is there any problem with that approach?
I think the following modification of the original code is way more efficient:
public static Connection getDBConnection2(boolean autoCommit) throws NamingException, SQLException
{
Hashtable parms = null;
if(savedDataSource == null) //savedDataSource is a private static field in this class
{
parms = new Hashtable();
parms.put(Context.INITIAL_
InitialContext ctx = new InitialContext(parms);
savedDataSource = (DataSource)ctx.lookup("ja
}
Connection conn = savedDataSource.getConnect
if (!autoCommit)
{
conn.setAutoCommit(false);
}
return conn;
}
Does somebody sees any problem with the second version, which keeps the datasource reference for the rest of the application's life?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No reason you shouldn't do it that way. DataSource is a factory ... all of your lookups return a reference to the same object anyway. So your way is fine.