简体   繁体   中英

DBCP Pool using same connection?

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.)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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