I have a Java Hibernate project configuration which worked with SQL Server 2008 R2, now with a new OS 8.1 (from 7) and SQL Server 2012 (express), I\'m unable to connect to S
Your problem is jTDS does not support the way DBCP2 validates a connection by default (I'm assuming you use DBCP2 from
). See the solution below.
Usually the error stacktrace is as shown:
Caused by: java.lang.AbstractMethodError
at net.sourceforge.jtds.jdbc.JtdsConnection.isValid(JtdsConnection.java:2833)
at org.apache.tomcat.dbcp.dbcp2.DelegatingConnection.isValid(DelegatingConnection.java:913)
The problem, though, is not related to the SQL Server version, but to the DBCP (Tomcat) version used (or the Tomcat server version the project is deployed to).
Once I was using jTDS 1.3.1 and the project worked fine (and connected to SQLServer 2012 as well) under Tomcat7. When I changed to Tomcat 8, that error appeared.
The reason, as hinted in jTDS forums, is:
java.sql.Connection.isValid(int)
to validate the connection.isValid()
, so jTDS driver won't work with DBCP 2, unless...validationQuery
parameter, which will make DBCP not call .isValid()
to test the validity of the connection.So, the workaround is to set the validationQuery
parameter, which will make DBCP2 not call .isValid()
to test the validity of the connection. Here's how:
Add validationQuery="select 1"
to your Tomcat
tag for connection pool, which is usually in META-INF/context.xml
of your app or conf/server.xml
:
When using DBCP2 through Spring, the solution is something around:
...
dataSource.setValidationQuery("select 1");