public class A {
protected Connection con = null;
public void openDbConnection() throws CpiSystemException {
try {
if (con == null || con.isClosed()) {
con = CpiDataSource.getNonTxConnection();
}
} catch (SQLException e) {
log.error(e.getMessage(), e);
new CpiSystemException("SQLException caused by con.isClosed(): " + e.getMessage());
}
}
}
public class B extends A {
private Connection con;
// if two servlets requests came to execute below method, both will use same connection object or will create different objects?
public void executeQuery(){
openDbConnection();
con.prepareStatement(SELECT_CUST_PILN_PREF_BY_CRTN_USER_ID);
}
}
public class C extends HttpServlet {
B b = null;
init(){
}
doget(request, response){
b = new B();
b.executeQuery();
}
}
我的懷疑是: 對於每一個servlet請求一個連接對象將被創建,或所有servlet請求相同的連接對象(我們知道,每個servlet請求時,它會創建線程)類級變量將由所有servlet請求共享嗎?
您必須閱讀這篇文章http://yiyujia.blogspot.in/2011/12/on-my-post-about-java-class-loader-i.html – 2014-10-29 09:32:28