1

我正在使用 JMetric 来测试我的 DBCP 池。当我尝试从一个连接创建Statement 时,使用一项包含 20 个线程的测试会收到 nullPointerException。

我的上下文有这个conf:

<Context path="/MyApp" docBase="mypath..." crossContext="true" debug="1" reloadable="true" privileged="true" >
        <Resource name="jdbc/orcl"
           auth="Container"
           type="oracle.jdbc.pool.OracleDataSource"
           driverClassName="oracle.jdbc.driver.OracleDriver"
           factory="oracle.jdbc.pool.OracleDataSourceFactory"
           url="jdbc:oracle:thin:@192.168.1.11:1521:orcl"
           user="....."
           password="....."
           implicitCachingEnabled="true"
           connectionCachingEnabled="true"
           connectionCacheProperties="{InitialLimit=20, MinLimit=50, MaxLimit=350, MaxStatementsLimit=0, ConnectionWaitTimeout=10}"
           connectionCacheName="cacheOrcl"
           validationQuery="select 1 from dual"
           removeAbandoned="true"
           maxIdle="350"
           removeAbandonedTimeout="45"
           logAbandoned="true"  
            />

    </Context>

我有一个过滤器可以连接并执行一些选择。为了重用逻辑来获得连接,我创建了一个静态方法:

public static synchronized Connection getConnection() throws ConnectionException {

    Connection con = null;
    try {
        Object o = new InitialContext().lookup("java:comp/env/jdbc/orcl");
        if( o instanceof DataSource ) {
            DataSource ds = (DataSource) o;
            con = ds.getConnection();
            LOGGER.debug("conn:" + con);
        }
    }catch( Exception e ) {
        LOGGER.error(LogError.logError(e));
    }

    if( con == null ) {
        throw new ConnectionException("Conn null");
    }

    return con;
}

还有我的过滤器:

try {
        if( session.getAttribute(PARAM) == null ) {
            conexao = ConnectionUtil.getConnection();
            //call DAOS... (ommited)
        }
    }catch( Exception e ) {
        LOGGER.error( LogError.logError(e) );
    } finally {
        try{ 
            conexao.close();
            conexao = null;
        }catch( Exception e ){}
    }

要接收 NullPointerException,我认为来自 DataSource 的 getConnection() 正在回收一个仍在使用的连接。

问题是否有一种静态同步方法来从池中获取连接?

空指针异常:

Statement st = conexao.createStatement();

编辑:我现在正在尝试 tomcat-jdbc。他似乎可以更好地处理打开的连接,但在并发用户中仍然失败(相同的 NullPointerException 或有时 java.sql.SQLException:连接已关闭。)

4

1 回答 1

0

经过一番斗争,我发现我的 Spring Controller 中有一个 Connection 类型的私有属性。我评论了这个属性并在我的方法中声明了连接,这解决了我的问题。

于 2011-09-15T14:33:35.727 回答