简体   繁体   中英

How to determine if java.sql.Connection is valid with PostgreSQL JDBC4

I'm trying write a test for a method that returns a java.sql.Connection to connect to a PostgreSQL.

My test is very straightforward:

@Test
public void connectionFactoryShouldReturnOpenConnection() throws Exception {
    Connection conn = ConnectionFactory.getConnection();

    assertTrue(conn.isValid(2));
}

The isValid call fails, though:

org.postgresql.util.PSQLException: Method org.postgresql.jdbc4.Jdbc4Connection.isValid(int) is not yet implemented.
at org.postgresql.Driver.notImplemented(Driver.java:753)
at org.postgresql.jdbc4.AbstractJdbc4Connection.isValid(AbstractJdbc4Connection.java:109)
at org.postgresql.jdbc4.Jdbc4Connection.isValid(Jdbc4Connection.java:21)

What's going on? Surely there is a PostgreSQL JDBC driver that implements the methods of Java 1.6 such as isValid() ?

Here the details on the driver I'm using:

<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.0-801.jdbc4</version>

Perform a simple query, if it works, it's valid. If not, it's not.

And by simple query I mean something like:

SELECT 1;

Really simple.

From the postresql driver source code the AbstractJdbc4Connection.isValid() throws this exception when called:

public boolean isValid(int timeout) throws SQLException
{
    checkClosed();
    throw org.postgresql.Driver.notImplemented(this.getClass(), "isValid(int)");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM