Tomcat 8 上 jTDS JDBC 驱动程序的 AbstractMethodError [英] AbstractMethodError with jTDS JDBC Driver on Tomcat 8
问题描述
我正在将 Web 应用程序 (WAR) 部署到 Tomcat 8 Web 容器.
I am deploying a web app (WAR) to a Tomcat 8 web container.
WAR 在/WEB-INF/lib"目录中包含以下 jTDS JDBC 驱动程序:
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" />
(文件为:jtds-1.3.1.jar
).
这是在META-INF/context.xml
中定义资源的方式:
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"
/>
在我的代码中,我获得了 javax.sql.DataSource 正常方式:
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 );
我进一步验证(通过打印)DataSource 对象 ds
是预期的类型:
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)
是什么?
推荐答案
原来我必须添加:
validationQuery="select 1"
在 context.xml
中的资源声明中.
in the Resource declaration in context.xml
.
在此处提到了这一点(尽管拼写错误为validateQuery
).
This is mentioned here (although mispelled as validateQuery
).
深入研究 JtdsConnection
的实现可以看到:
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();
}
这真的很奇怪,我认为 AbstractMethodError 应该只由编译器抛出,未实现的方法应该抛出 不支持的操作异常.无论如何,以下代码来自 PoolableConnection 显示了为什么 context.xml
中存在或不存在 validationQuery
可以改变事情.您的 validationQuery
在下面的方法中作为 sql
String
参数的值传递(或者 null
如果你不这样做'不定义 validationQuery
):
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;
}
...
}
所以基本上如果没有 validationQuery
存在,那么连接自己的 isValid
实现被咨询,在 JtdsConnection
的情况下,它奇怪地抛出 AbstractMethodError
.
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
.
这篇关于Tomcat 8 上 jTDS JDBC 驱动程序的 AbstractMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!