2012년 2월 20일 월요일

Preventing database connection pool leaks

Preventing database connection pool leaks

http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html


A database connection pool creates and manages a pool of connections to a database. Recycling and reusing already existing connections to a database is more efficient than opening a new connection.
There is one problem with connection pooling. A web application has to explicitly close ResultSet's, Statement's, and Connection's. Failure of a web application to close these resources can result in them never being available again for reuse, a database connection pool "leak". This can eventually result in your web application database connections failing if there are no more available connections.
There is a solution to this problem. The Apache Commons DBCP can be configured to track and recover these abandoned database connections. Not only can it recover them, but also generate a stack trace for the code which opened these resources and never closed them.
To configure a DBCP DataSource so that abandoned database connections are removed and recycled add the following attribute to the Resource configuration for your DBCP DataSource:
removeAbandoned="true"
When available database connections run low DBCP will recover and recycle any abandoned database connections it finds. The default is false.
Use the removeAbandonedTimeout attribute to set the number of seconds a database connection has been idle before it is considered abandoned.
removeAbandonedTimeout="60"
The default timeout for removing abandoned connections is 300 seconds.
The logAbandoned attribute can be set to true if you want DBCP to log a stack trace of the code which abandoned the database connection resources.
logAbandoned="true"
The default is false.



POJO class sample.
-------------------------------------------
BasicDataSource bds = new BasicDataSource();
..
DB설정 부분..
..
..
아래 내용 추가.

bds.setRemoveAbandoned( true );
bds.setRemoveAbandonedTimeout( 60 );
bds.setLogAbandoned( true ); 

댓글 없음:

댓글 쓰기