DBCP 池使用相同的连接吗?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番斗争,我发现我的 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.