public class CpiDaoBase {
protected Connection con = null;
public void test(){
cpiDAOBase.openDbConnection();
ps = new cpiDAOBase().con.prepareStatement(INSERT_CARRIER);
ps.executeQuery();
... blah blah blah
}
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 CpiDataSource {
public static Connection getNonTxConnection() throws CpiSystemException {
try {
if (nonTxDs == null)
{
if(log.isDebugEnabled()){
log.debug("nonTxDs is null");
}
init();
}
return nonTxDs.getConnection();
} catch (NamingException e)
{
log.error("cpiPoolDataSource is not available !" + e);
throw new CpiSystemException("cpiPoolDataSource is not available !");
} catch (SQLException e)
{
log.error("Failed to get connection from datasource !" + e);
throw new CpiSystemException("Failed to get connection from datasource !");
} catch (Exception e)
{
log.error("Exception \n" + e);
throw new CpiSystemException(e.getMessage());
}
}
}
我得到以下異常:值java.sql.SQLException:語句已經關閉
值java.sql.SQLException:語句已經關閉] [[活動] ExecuteThread: '8' for queue:'weblogic.kernel.Default(self-tuning)'] [2014-10-28 05:54:17,918] [錯誤] [com.uprr.app.cpi.dao.CpiCustomerPipelinePreferencesDao:104] [SQL Exception : java.sql.SQLException:executeQuery,Exception = null] [[ACTIVE] ExecuteThread:'5'for queue:'weblogic.kernel.Default(self-tuning)'] [2014-10-28 05:54: 17,918] [錯誤] [com.uprr.app.cpi.web.action。 PatternSelectionAction:112] [在訪問CPI_CUST_PILN_PREF表時獲得SQLException:executeQuery,Exception = null] [[ACTIVE] ExecuteThread:'5'for隊列:'weblogic.kernel.Default(self-tuning)']
我的疑問很簡單,但我很困惑:
從上面的代碼我使用本地連接對象或全球?請建議。
我同意你的第一句話。然而,由於從OP的代碼示例中不清楚cpiDAOBase是一個變量,一個字段還是一個類,它不同於他聲明的類名「CpiDaoBase」,我不太確定代碼*應該是什麼*是。 – RealSkeptic 2014-10-28 14:22:56
@RealSkeptic也許*看起來像它*應該注意,'openDbConnection();'似乎設置了一個本地'Connection con'字段。 – 2014-10-28 14:28:01