Connection Validation in GlassFish JDBC Connection Pool

 

Connection Pool manages a pool of connections and serves the user requests quickly as creating a new
connection for each user request will take longer time. Since the connections are pooled, it is possible
that a connection may become invalid or stale, i.e., database server may set the connection state to
closed because of network failure or database might have been restarted. In such scenario, if the application
retrieves a connection from connection pool, failure will occur indicating that the connection is no more valid.


To circumvent stale connections issue, connection pool property "is-connection-validation-required" can be set
to true. This flag indicates the connection pool to validate the connection for every request by the application.
Validating a connection ensures that the connection is in healthy state. If the connection validation fails,
connection will be removed from the pool and the application will get valid connection. [Pool will either return the
next valid connection or create a new connection]. Thus validation ensures that every getConnection() request
by the application  will be served by a valid connection.

To set validation :
eg: DerbyPool

Following command will list resources by name "DerbyPool"

AS_INSTALL_ROOT/bin/asadmin list "\*DerbyPool\*"
domain.resources.jdbc-connection-pool.DerbyPool


To get the list of connection pool attributes and properties,
AS_INSTALL_ROOT/bin/asadmin get domain.resources.jdbc-connection-pool.DerbyPool.\*
...
...
domain.resources.jdbc-connection-pool.DerbyPool.connection-validation-method = auto-commit
...
...
domain.resources.jdbc-connection-pool.DerbyPool.is-connection-validation-required = false
domain.resources.jdbc-connection-pool.DerbyPool.validation-table-name =


 To enable connection validation :
AS_INSTALL_ROOT/bin/asadmin set domain.resources.jdbc-connection-pool.DerbyPool.is-connection-validation-required=true
domain.resources.jdbc-connection-pool.DerbyPool.is-connection-validation-required = true


By default, the validation mode is "auto-commit". Auto-commit property of the connection to be validated
will be set & reset by the validation logic. Most of the jdbc-drivers cache the "auto-commit" value of the
connection and hence actual validation (connecting to database) does not happen.

Instead, setting table-based validation will ensure that validation happens properly. A valid table name need to
be provided, validation logic will issue a query on this table. If the connection is stale, this query will fail and
hence will be removed from the pool.

To set Table-based validation :

AS_INSTALL_ROOT/bin/asadmin set domain.resources.jdbc-connection-pool.DerbyPool.connection-validation-method=table
domain.resources.jdbc-connection-pool.DerbyPool.connection-validation-method = table

bin/asadmin set domain.resources.jdbc-connection-pool.DerbyPool.validation-table-name=sys.systables
domain.resources.jdbc-connection-pool.DerbyPool.validation-table-name = sys.systables


Sometimes, database restart may result in all the connections in the pool to be invalid.
In such a case, first get connection after restart will result in validating a connection, removing it from the pool,
validating the next connection and so on. Eventually all connections will be validated, removed from the pool
and pool will be reinitialized.

Following property "fail-all-connections" will ensure that when one of the connections become valid, pool will be
reinitialized, ie., all connections will be destroyed and initialized again.

AS_INSTALL_ROOT/bin/asadmin set domain.resources.jdbc-connection-pool.DerbyPool.fail-all-connections=true
domain.resources.jdbc-connection-pool.DerbyPool.fail-all-connections = true


Above property need to be set only in cases where there are more possibilities of database restarts/failures, as
all connections are dropped and recreated for a single connection validation failure.

References

Application Server - Reference Manual > create-jdbc-connection-pool


Comments:

Very useful. Tks.

Posted by lux on April 19, 2007 at 10:22 AM IST #

Very Useful, Thanks! :)

Posted by Irfan on April 03, 2009 at 10:38 AM IST #

very useful. Thanks.

Posted by guest on April 09, 2009 at 03:25 PM IST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

jagadish

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today