Question
The org.postgresql JDBC driver for Postgres has a new release for JDBC 4.2 (JEP 170). The old package with classes for dynamically determining the state of a Connection’s transaction has disappeared. What to use now?
Background
Strange but true, JDBC has no way to identify the state of a transaction. So for example, when getting a Connection from a pool, you cannot verify if there is a pending transaction left hanging from previous usage.
As a workaround, you can call on Postgres-specific methods in the JDBC driver to determine if the state is idle (no txn), open (pending txn), or failed (error in txn).
In the previous version, 9.4-1201-jdbc41
(note the 41
vs 42
, meaning JDBC 4.2), you could write code like this.
if ( conn instanceof org.postgresql.jdbc2.AbstractJdbc2Connection ) {
// Cast from a generalized JDBC Connection to one specific to our expected Postgres JDBC driver.
org.postgresql.jdbc2.AbstractJdbc2Connection aj2c = ( org.postgresql.jdbc2.AbstractJdbc2Connection ) conn; // Cast to our Postgres-specific Connection.
// This `getTransactionState` method is specific to the Postgres JDBC driver, not general JDBC.
int txnState = aj2c.getTransactionState();
// We compare that state’s `int` value by comparing to constants defined in this source code:
// https://github.com/pgjdbc/pgjdbc/blob/master/org/postgresql/core/ProtocolConnection.java#L27
switch ( txnState ) {
case org.postgresql.core.ProtocolConnection.TRANSACTION_IDLE:
stateEnum = DatabaseHelper.TransactionState.IDLE;
break;
case org.postgresql.core.ProtocolConnection.TRANSACTION_OPEN:
stateEnum = DatabaseHelper.TransactionState.OPEN;
break;
case org.postgresql.core.ProtocolConnection.TRANSACTION_FAILED:
stateEnum = DatabaseHelper.TransactionState.FAILED;
break;
default:
// No code needed.
// Go with return value having defaulted to null.
break;
}
} else {
logger.error( "The 'transactionStateOfConnection' method was passed Connection that was not an instance of org.postgresql.jdbc2.AbstractJdbc2Connection. Perhaps some unexpected JDBC driver is in use. Message # 47a076b1-ba44-49c7-b677-d30d76367d7c." );
return null;
}
The current driver is JDBC42 Postgresql Driver, Version 9.4-1203
. In this new driver the org.postgresql.jdbc2
package seems to have gone away. What is the replacement?