引用:
a. 如果设为true则tomcat自动检查恢复重新利用,没有正常关闭的Connection.(默认是false)
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
b. 设定连接在多少秒内被认为是放弃的连接,即可进行恢复利用。
<parameter>
<name>removeAbandonedTimeout</name>
<value>60</value>
</parameter>
c. 输出回收的日志,可以详细打印出异常从而发现是在那里发生了泄漏
<parameter>
<name>logAbandoned</name>
<value>true</value>
</parameter>
代码如下:
<parameter>
<name>maxActive</name>
<value>5</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>1</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>20000</value>
</parameter>
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeout</name>
<value>60</value>
</parameter>
<parameter>
<name>logAbandoned</name>
<value>true</value>
</parameter>
<name>maxActive</name>
<value>5</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>1</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>20000</value>
</parameter>
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeout</name>
<value>60</value>
</parameter>
<parameter>
<name>logAbandoned</name>
<value>true</value>
</parameter>
try {
Connection con = getJdbcDAO().getDataSource().getConnection();
ResultSet rs = con.createStatement().executeQuery("select * from K_MS..B_MSPBXX");
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
Connection con = getJdbcDAO().getDataSource().getConnection();
ResultSet rs = con.createStatement().executeQuery("select * from K_MS..B_MSPBXX");
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
当该连续执行5次之后,后台就报连接池满的错:
2008-09-06 14:31:02,471 [org.hibernate.util.JDBCExceptionReporter]-[WARN] SQL Error: 0, SQLState: null
2008-09-06 14:31:02,471 [org.hibernate.util.JDBCExceptionReporter]-[ERROR] Cannot get a connection, pool exhausted
2008-09-06 14:31:02,580 [org.hibernate.util.JDBCExceptionReporter]-[WARN] SQL Error: 0, SQLState: null
2008-09-06 14:31:02,580 [org.hibernate.util.JDBCExceptionReporter]-[ERROR] Cannot get a connection, pool exhausted
declare cur_spid cursor
for
select spid from sysprocesses where ipaddr=172.16.16.145 and program_name <> SQL_Advantage
go
declare @spid Integer
open cur_spid
fetch cur_spid into @spid
while @@sqlstatus=0
begin
print %1! , @spid
dbcc traceon(3604)
dbcc sqltext(@spid )
fetch cur_spid into @spid
end
close cur_spid
declare cur_spid cursor
for
select spid from sysprocesses where ipaddr=172.16.16.145 and program_name <> SQL_Advantage
go
declare @spid Integer
open cur_spid
fetch cur_spid into @spid
while @@sqlstatus=0
begin
print %1! , @spid
dbcc traceon(3604)
dbcc sqltext(@spid )
fetch cur_spid into @spid
end
close cur_spid
for
select spid from sysprocesses where ipaddr=172.16.16.145 and program_name <> SQL_Advantage
go
declare @spid Integer
open cur_spid
fetch cur_spid into @spid
while @@sqlstatus=0
begin
print %1! , @spid
dbcc traceon(3604)
dbcc sqltext(@spid )
fetch cur_spid into @spid
end
close cur_spid
引用:
95
DBCC execution completed. If DBCC printed error messages, contact a user with System Administrator (SA) role.
SQL Text: select * from K_MS..B_MSPBXX
DBCC execution completed. If DBCC printed error messages, contact a user with System Administrator (SA) role.
DBCP object created 2008-09-06 14:27:32 by the following code was never closed:
java.lang.Exception
at org.apache.commons.dbcp.AbandonedTrace.init(AbandonedTrace.java:96)
at org.apache.commons.dbcp.AbandonedTrace.<init>(AbandonedTrace.java:79)
at org.apache.commons.dbcp.DelegatingResultSet.<init>(DelegatingResultSet.java:71)
at org.apache.commons.dbcp.DelegatingResultSet.wrapResultSet(DelegatingResultSet.java:80)
at org.apache.commons.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:205)
at com.thunisoft.fy.spxt.SpxtBaseLogic.createPbxx(SpxtBaseLogic.java:5772)
然后同样的java代码,把tomcat连接池的那三个参数去掉之后,执行5次之后也报连接池满,但是再次执行就不能获取新的连接,并且后台的日志都是连接池满的信息,而没有具体那一行代码的连接没有释放的异常信息。
因为大兴实际环境还是出现连接池满的问题(基本上两天报一次),准备把这三个参数放到实际环境中试试然后看看后台日志,一是想得到具体是哪些地方的代码没有释放,二是如果设置removeAbandonedTimeout参数,可以避免连接没有释放的问题。当然个人认为三期代码的最终部署环境是不需要该参数的,在程序中把连接释放才是解决问题的最根本办法,另外目前还不知道这三个参数会对tomcat性能造成什么影响,应该不大。