AbstractMethodError with jTDS JDBC Driver on Tomcat 8

Marcus Junius Brutus picture Marcus Junius Brutus · Dec 19, 2016 · Viewed 10.5k times · Source

I am deploying a web app (WAR) to a Tomcat 8 web container.

The WAR includes in the '/WEB-INF/lib' directory the following jTDS JDBC driver:

<dependency org="net.sourceforge.jtds" name="jtds" rev="1.3.1" />

(file is: jtds-1.3.1.jar).

This is how the resource is defined in META-INF/context.xml:

<Resource name="jdbc/jtds/sybase/somedb"
          auth="Container"
          type="javax.sql.DataSource"
          driverClassName="net.sourceforge.jtds.jdbc.Driver"
          url="jdbc:jtds:sybase://localhost:2501/somedb"
          username="someuser" password="somepassword"
/>

In my code I obtain the javax.sql.DataSource the normal way:

InitialContext cxt = new InitialContext();
if ( cxt == null ) {
    throw new RuntimeException("Uh oh -- no context!");
}
DataSource ds = (DataSource) cxt.lookup( lookupName );

I further verify (by printing) that the DataSource object ds is of the expected type:

org.apache.tomcat.dbcp.dbcp2.BasicDataSource

… but when I try to get a connection out of it:

Connection conn = ds.getConnection();

… I get the following trace:

java.lang.AbstractMethodError
net.sourceforge.jtds.jdbc.JtdsConnection.isValid(JtdsConnection.java:2833)
org.apache.tomcat.dbcp.dbcp2.DelegatingConnection.isValid(DelegatingConnection.java:924)
org.apache.tomcat.dbcp.dbcp2.PoolableConnection.validate(PoolableConnection.java:282)
org.apache.tomcat.dbcp.dbcp2.PoolableConnectionFactory.validateConnection(PoolableConnectionFactory.java:359)
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.validateConnectionFactory(BasicDataSource.java:2316)
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2299)
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2043)
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1543)

What gives?

Answer

Marcus Junius Brutus picture Marcus Junius Brutus · Dec 19, 2016

Turns out I had to add:

validationQuery="select 1"

in the Resource declaration in context.xml.

This is mentioned here (although mispelled as validateQuery).

Digging into the implementation of JtdsConnection one sees:

/* (non-Javadoc)
 * @see java.sql.Connection#isValid(int)
 */
public boolean isValid(int timeout) throws SQLException {
    // TODO Auto-generated method stub
    throw new AbstractMethodError();
}

This is really weird, I think AbstractMethodError is supposedly thrown by the compiler only, unimplemented methods ought to throw UnsupportedOperationException. At any rate, the following code from PoolableConnection shows why the presence or not of validationQuery in context.xml can change things. Your validationQuery is passed as the value of the sql String parameter in the below method (or null if you don't define a validationQuery):

public void More ...validate(String sql, int timeout) throws SQLException {
    ...
    if (sql == null || sql.length() == 0) {
        ...
        if (!isValid(timeout)) {
            throw new SQLException("isValid() returned false");
        }
        return;
    }
...
}

So basically if no validationQuery is present, then the connection's own implementation of isValid is consulted which in the case of JtdsConnection weirdly throws AbstractMethodError.