DB连接关闭后无法关闭迭代器

时间:2016-11-11 10:24:33

标签: java intellij-idea iterator database-connection

当我开始使用Intellij IDEA时发生了以下问题。 在Eclipse中,一切正常,迭代器关闭。

我认为这是一个配置问题。你能提出什么解决方案?

SiteFeatureData类选择方法片段:

    Connection dbconn = null;
    try{
        dbconn = ConnCacheImpl.getConnection();
        ctx = Data.getContext(dbconn);
        etx = new ExecutionContext();
        #sql [ctx,etx] iter = {
            SELECT id, ...
        };
    if ( iter.next() ) {
            populate( iter, ...
                    , ctx.getConnection()
                    );
        } 
    }finally{
        try{
            dbconn.close();
            returnContext(ctx,etx,iter);
        } catch(SQLException ex){};
        ctx = null;
        etx = null;
        iter = null;
    }

数据类returnContext方法:

protected static void returnContext(DefaultContext ctx
                                   ,ExecutionContext etx
                                   ,ResultSetIterImpl iter) throws SQLException{
    if(iter != null){
        iter.close();  // PROBLEM IS HERE
    }
    if(etx != null){
        etx.close();
    }
    if(ctx != null){
        ctx.close(ConnectionContext.KEEP_CONNECTION);
    }
}


Caused by: java.lang.reflect.UndeclaredThrowableException
    at com.sun.proxy.$Proxy5.getStmtCacheSize(Unknown Source)
    at sqlj.runtime.ref.OraRTResultSet.close(OraRTResultSet.java:1018)
    at sqlj.runtime.ref.ResultSetIterImpl.close(ResultSetIterImpl.java:187)
    at com.softdesign.persistence.Data.returnContext(Data.java:60)
    at com.requestcom.geofire.domain.data.SiteFeatureData.select(SiteFeatureData.java:2326)
    at com.softdesign.persistence.Persistent.exists(Persistent.java:343)
    at com.softdesign.persistence.Persistent.exists(Persistent.java:326)
    at com.requestcom.geofire.context.ServletRequestContext.getSiteFeature(ServletRequestContext.java:3976)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.doInvoke(UberspectImpl.java:395)
    at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.invoke(UberspectImpl.java:384)
    at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:173)
    ... 38 more
Caused by: java.sql.SQLException: The connection is closed: The connection is closed
    at oracle.ucp.util.UCPErrorHandler.newSQLException(UCPErrorHandler.java:473)
    at oracle.ucp.util.UCPErrorHandler.newSQLException(UCPErrorHandler.java:457)
    at oracle.ucp.jdbc.proxy.JDBCConnectionProxyFactory.invoke(JDBCConnectionProxyFactory.java:255)
    ... 53 more

2 个答案:

答案 0 :(得分:1)

在跟踪中可以看到您在调用Connection.close()之后调用了OraRTResultSet.close(),它是java.sql.ResultSet.close()的一个实现。 这将永远引发异常。 您必须在关闭连接之前关闭ResultSet。 所以翻转线

returnContext(ctx,etx,iter);
dbconn.close();

答案 1 :(得分:0)

在调用iter.close()方法之前,需要检查连接是否已关闭。

if(iter != null && !iter.isClosed()){
    iter.close(); 
}

您可以在DBConnection之前尝试关闭迭代器。这也应该解决问题。