Hi,
In my application someone has written code which reads data from multiple tables. The code looks like this:
...........
...........
readTable(TABLE1);
readTable(TABLE2);
readTable(TABLE3);
readTable(TABLE4);
readTable(TABLE5);
readTable(TABLE6);
..........
void readTable(
String tableName){
InitialContext initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/");
dataSource = (DataSource) envContext.lookup(STORE_DS_NAME);
Connection conn = dataSource.getConnection();
selectStatement = connection.prepareStatement(SELECT_UNSYNC_RECORDS);
resultSet = selectStatement.executeQuery();
.......... //process result set
..........
finally{
conn.rollback();
conn.close();
}
}
The problem I see here in this code is : its creating/getting connection object from DataSource for each method call and closing it in finally . Isn't it an overhead. thats is multiple DB calls !! I understand that Datasource take connections from pool but still getting connection from pool and returning it for each method call ?? I mean come on.. there must be a better way.
Any better solution for this kinda situation ??