【问题标题】:java.sql.SQLException: Statement has already been closedjava.sql.SQLException:语句已关闭
【发布时间】:2014-10-28 14:01:51
【问题描述】:
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: 语句已经关闭] ​​[[ACTIVE] ExecuteThread: '8' for queue: 'weblogic.kernel.Default (self-tuning)'] [2014-10-28 05:54:17,918] [错误] [com.uprr.app.cpi.dao.CpiCustomerPipelinePreferencesDao:104] [SQL 异常: 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 queue: 'weblogic.kernel.Default (self-tuning)']

我的疑问很简单,但我很困惑:

从上面的代码中,我使用的是本地连接对象还是全局?请提出建议。

【问题讨论】:

    标签: java weblogic-10.x


    【解决方案1】:

    你没有使用Connectionopen。这个,

    cpiDAOBase.openDbConnection();
    ps = new cpiDAOBase().con.prepareStatement(INSERT_CARRIER);
    

    应该是

    cpiDAOBase.openDbConnection();
    ps = cpiDAOBase.con.prepareStatement(INSERT_CARRIER);
    

    因为您使用new cpiDAOBase() 创建的实例没有openDbConnection

    【讨论】:

    • 我同意你的第一句话。但是,由于从 OP 的代码示例中不清楚 cpiDAOBase 是变量、字段还是类,并且与他声明的类名“CpiDaoBase”不同,所以我不太确定代码 应该是。
    • @RealSkeptic 也许看起来像应该是,注意openDbConnection();似乎设置了一个本地Connection con字段。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 2015-11-03
    • 1970-01-01
    • 2016-07-01
    • 2011-08-02
    • 2012-04-14
    相关资源
    最近更新 更多