简体   繁体   English

DBCP池使用相同的连接?

[英]DBCP Pool using same connection?

I'm using JMetric to test my DBCP pool. 我正在使用JMetric测试我的DBCP池。 Using one test with 20 threads I receive nullPointerException when I'm trying to createStatement from one Connection. 尝试从20个线程使用一个测试时,当我尝试从一个Connection创建createStatement时,我收到nullPointerException。

My context have this conf: 我的上下文有这个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. 为了接收NullPointerException,我认为DataSource的getConnection()正在撤消一个仍在使用的连接。

Is a problem have one static synchronized method to get the connection from the pool? 是否有一种静态同步方法来从池中获取连接是否有问题?

The NullPointerException: NullPointerException:

Statement st = conexao.createStatement();

EDIT: I'm trying the tomcat-jdbc now. 编辑:我现在正在尝试tomcat-jdbc。 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.) 他似乎可以更好地处理打开的连接,但在并发用户中仍然会失败(相同的NullPointerException或有时是java.sql.SQLException:连接已关闭。)

After some fight I saw that I had one private attribute of type Connection inside my Spring Controller. 经过一番战斗之后,我发现我的Spring Controller中有一个Connection类型的私有属性。 I commented this attribute and declared connection inside my method and this solved my problem. 我注释了此属性,并在方法内部声明了连接,这解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM