Thread Local in Session
Hi you all,
I've been trying to understand wether one should use ThreadLocal when working with session objects.
I read this this:
http://nikhilb020875.wordpress.com/tag/session/
But this didnt help me either:
Supposed I create a Database Class, that helps run database related code, that has a thread scoped variable (i.e., a ThreadLocal), which is static, this means that the same connection is going to be use across the context application... wouldn磘 it be better to have one conn per session?
publicclass Database
{
//////////////////////////////////////////////////////////////
// Class objects
//////////////////////////////////////////////////////////////
privatestatic ThreadLocal<Database> instance =new ThreadLocal<Database>();
//////////////////////////////////////////////////////////////
// Class constructors
//////////////////////////////////////////////////////////////
private Database(){}
//////////////////////////////////////////////////////////////
// Class functions
//////////////////////////////////////////////////////////////
privateint openConn(){}
//////////////////////////////////////////////////////////////
publicboolean isOpen(){}
//////////////////////////////////////////////////////////////
publicvoid setTransaction(){}
//////////////////////////////////////////////////////////////
publicvoid commit(){}
//////////////////////////////////////////////////////////////
publicvoid roolback(){}
//////////////////////////////////////////////////////////////
public Boolean runStatement(String pStrStmt, Object[] values){}
//////////////////////////////////////////////////////////////
privatevoid setParameters(Object[] values, PreparedStatement pstmt){}
//////////////////////////////////////////////////////////////
publicboolean runStatementUpdate(String pStrStmt){}
//////////////////////////////////////////////////////////////
publicboolean runStatementInsert(String pStrStmt){}
//////////////////////////////////////////////////////////////
publicint runStatementInsert(String pStrStmt,boolean returnIdentity){}
//////////////////////////////////////////////////////////////
publicint runStatementInsert(String pStrStmt, Object[] values){}
//////////////////////////////////////////////////////////////
public ResultSet runStatementQuery(String pStrStmt){}
//////////////////////////////////////////////////////////////
public ResultSet runStatementQuery(String pStrStmt, Object[] values){}
//////////////////////////////////////////////////////////////
publicvoid closeConn(){}
//////////////////////////////////////////////////////////////
private Boolean runStatement(String pStrStmt){}
//////////////////////////////////////////////////////////////
publicstatic Database getInstance(){
if (instance.get() ==null){
Database database =new Database();
instance.set(database);
}
return instance.get();
}
}
This is one class that I am using, does it make any sense for this class to use threadLocal ?
I know its not good to pass a connection as an arg to classes that not extend the HttpSession, but to do the otherwise is having only one connection per context..
Any help is more then welcome ;)
Thanks,
MeTitus

