[Turn] DBCP's validationquery

Source: Internet
Author: User
Tags connection pooling

Original address: http://blog.csdn.net/fgakjfd/article/details/5600462

Many comments on the internet said dbcp have a lot of bugs, but did not indicate what is the bug, only a few people say that the database if for some reason, and then dbcp by the connection is a failed connection, but not re-fetch. I studied the code of DBCP and shared it.

Analysis

DBCP uses the Apache object pool Objectpool As the connection pool implementation, there are the following main methods

Object Borrowobject () throws Exception; Get a valid object from the object pool

void Returnobject (Object obj) throws Exception; Put the finished object back into the object pool

void Invalidateobject (Object obj) throws Exception; Invalidate the object

void AddObject () throws Exception; Generate a new object


One implementation of Objectpool is Genericobjectpool, which uses object factory Poolableobjectfactory to implement object generation, failure checking, etc. The main methods of implementing database connection factory Poolableconnectionfactory are as follows:

Object Makeobject () throws Exception; To generate a new connection using ConnectionFactory

void Destroyobject (Object obj) throws Exception; close connection

Boolean validateobject (Object obj); Verify that the connection is valid, and if _validationquery is not empty, use this property as an SQL statement to verify that the connection is valid, and query the database

void Activateobject (Object obj) throws Exception; Activating Connection object

void Passivateobject (Object obj) throws Exception; Close connections generated by statement and ResultSet to make the connection inactive

And Genericobjectpool has several main properties

_timebetweenevictionrunsmillis: Failure Check thread run time interval, default-1

_maxidle: Maximum number of objects in an object pool

_minidle: Minimum number of objects in the object pool

_maxactive: Maximum number of objects that can be fetched from the object pool, 0 means no limit, default is 8

When constructing the Genericobjectpool, an inline class Evictor is generated to implement the self-runnable interface. If _timebetweenevictionrunsmillis is greater than 0, every _timebetweenevictionrunsmillis milliseconds Evictor calls the evict () method to check if the object is idle longer than _ Minevictableidletimemillis milliseconds (_minevictableidletimemillis is less than or equal to 0 o'clock is ignored, the default is 30 minutes), the object is destroyed, otherwise the object is activated and validated, Then call the Ensureminidle method to check to make sure that the number of objects in the pool is not less than _minidle. When you call the Returnobject method to put the object back into the object pool, first check that the object is valid, and then call Poolableobjectfactory's Passivateobject method to make the object inactive. If the number of objects in the object pool is less than _maxidle, then you can put this object back into the object pool or destroy the object

There are also several important properties, _testonborrow, _testonreturn, and _testwhileidle, which are meant to be obtained, returned to the object and whether it is validated when idle, checking that the object is valid, and false by default. So when using DBCP, when the database connection is broken for some reason, and then gets a connection from the connection pool and does not validate, the connection that was made is actually an invalid database connection. Online a lot of said DBCP bug should be so, only to set these properties to true, and then provide the _validationquery statement to ensure that the database connection is always valid, the Oracle database can use the Select COUNT (*) from DUAL, However, DBCP requires _validationquery statement query Recordset must not be empty, perhaps this can also be counted as a small bug, in fact, as long as the _validationquery statement execution through it can be.

Precautions

So when you use DBCP connection pooling, you must pay attention to constructing Genericobjectpool objects

Validationquery:select COUNT (*) from DUAL

_testonborrow, _testonreturn, _testwhileidle: Best set to True

_minevictableidletimemillis: Greater than 0, the connection idle time is judged, or 0, the idle connection is not verified

_timebetweenevictionrunsmillis: Failure Check thread run time interval, if less than equals 0, check thread does not start

[Turn] DBCP's validationquery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.