DBCP 池使用相同的连接吗?

发布于 2024-12-04 13:51:54 字数 2462 浏览 1 评论 0原文

我正在使用 JMetric 来测试我的 DBCP 池。使用具有 20 个线程的一项测试,当我尝试从一个连接创建语句时,我收到 nullPointerException。

我的上下文有这样的配置:

<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() 正在检索仍在使用的一个连接。

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

NullPointerException:

Statement st = conexao.createStatement();

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

I'm using JMetric to test my DBCP pool. Using one test with 20 threads I receive nullPointerException when I'm trying to createStatement from one Connection.

My context have this 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>

I have one filter that get a connection and perform some selects. To reuse the logic to get the connection I create one static method:

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;
}

And my filter:

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 ){}
    }

To receive the NullPointerException I think that the getConnection() from DataSource is retreaving one connection that still in use.

Is a problem have one static synchronized method to get the connection from the pool?

The NullPointerException:

Statement st = conexao.createStatement();

EDIT: I'm trying the tomcat-jdbc now. He seems to handle better the opened connections but still fails in concurrent users (same NullPointerException or sometimes java.sql.SQLException: Connection has already been closed.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隐诗 2024-12-11 13:51:54

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

After some fight I saw that I had one private attribute of type Connection inside my Spring Controller. I commented this attribute and declared connection inside my method and this solved my problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文