Interface Validator
PoolConfiguration.setValidatorClassName(String) is instantiated reflectively and therefore must have a
public no-arg constructor.-
Method Summary
Modifier and TypeMethodDescriptionbooleanvalidate(Connection connection, int validateAction) Validate a connection and return a boolean to indicate if it's valid.
-
Method Details
-
validate
Validate a connection and return a boolean to indicate if it's valid.The pool calls this method with the underlying JDBC connection, outside any interceptor chain, and treats any runtime exception or non-fatal
Errorthrown by an implementation as a validation failure. AStackOverflowErroris swallowed like a non-fatal error; the otherVirtualMachineErrortypes are re-thrown. Implementations that invoke JDBC APIs must catchSQLExceptionand returnfalsethemselves, since this method does not declare a checked exception. Afalseresult (or a thrown runtime exception or error) causes the pool to discard the connection; on a borrow that discard happens within the samegetConnection()call, as the pool immediately re-establishes the session and validates the replacement, reporting it asPooledConnection.VALIDATE_INITwhen new connections are initialized.The pool performs no transaction management as part of validating. The built-in, query-based check ends any transaction open on the session when it completes - its own, or one a previous borrower left behind - but a validator replaces that check and owns the session state it observes: an implementation that executes statements on a session whose auto-commit mode is off must commit or roll back the transaction those statements open, itself.
The pool may skip validation entirely for a given action - it validates when the corresponding
testOnBorrow,testOnReturn,testWhileIdleortestOnConnectflag is set (the idle action additionally requires a running pool sweeper, seePoolConfiguration.setTimeBetweenEvictionRunsMillis(int)), and forPooledConnection.VALIDATE_INITalso when a non-blankinitSQLis configured - and it may short-circuit repeated validations, seePoolConfiguration.setValidationInterval(long). The validator replaces the query-based validation entirely: while it is set, neither thevalidation querynor a configured init SQL is executed. This method is therefore not guaranteed to run for every borrow, and it must not be relied on for per-borrow session state. Implementations are invoked concurrently from any application, management or pool-internal thread - including a sweeper timer thread shared by every pool in the JVM - and must be thread-safe.- Parameters:
connection- the underlyingConnectionto test - not the pool's proxied handle, though the object itself may be a driver or third-party wrappervalidateAction- the action used. One ofPooledConnection.VALIDATE_BORROW,PooledConnection.VALIDATE_IDLE,PooledConnection.VALIDATE_INITorPooledConnection.VALIDATE_RETURN- Returns:
- true if the connection is valid
-