1

我有下面的代码。相当标准的东西。但是,我在 if(!stmt.isClose()) 行上遇到非法访问异常......该错误表明这是因为该语句已关闭。这不是该方法调用的重点吗?有人对此有什么想法吗?

Connection conn = dataSource.getConnection();
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
try {
    stmt.executeUpdate(updateQuery1);
    stmt.executeUpdate(updateQuery2);
    stmt.executeUpdate(updateQuery3);
    conn.commit();
    try {
        if (!stmt.isClosed()) {
            stmt.close();
        }
        if (!conn.isClosed()) {
            conn.close();
        }
    } catch (Throwable ignored) {
} catch(Throwable ignored) {
}

499285 java.lang.IllegalAccessError: org.apache.tomcat.dbcp.dbcp.DelegatingStatement.isClosed()Z 499286 at com.millennialmedia.enterprise.services.impl.CampaignUtilServiceImpl.resetExceptionCampaign(CampaignUtilServiceImpl.java:80) 499287 at com.millennialmedia.rest.services.CampaignUtilRESTService$1.process(CampaignUtilRESTService.java:55) 499288 at com.millennialmedia.rest.services.CampaignUtilRESTService$1.process(CampaignUtilRESTService.java:49) 499289 at com.millennialmedia.services.util.Processor.doProcess(Processor.java:98) 499290 at com.millennialmedia.rest.services.CampaignUtilRESTService.resetExceptionCampaign(CampaignUtilRESTService.java:49) 499291 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 499292 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 499293 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 499294 at java.lang.reflect.Method.invoke(Method.java:597) 499295 at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60) 499296 at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchPro 499297 at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) 499298 at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288) 499299 at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) 499300 at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) 499301 at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) 499302 at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) 499303 at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469) 499304 at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400) 499305 at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349) 499306 at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339) 499307 at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) 499308 at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537) 499309 at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699) 499310 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 499311 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 499312 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 499313 at com.millennialmedia.instrumentation.MethodInvocationFilter._doFilter(MethodInvocationFilter.java:20) 499314 at com.millennialmedia.instrumentation.MethodInvocationFilter.doFilter(MethodInvocationFilter.java:31) 499315 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 499316 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 499317 at com.millennialmedia.session.BaseAuthenticatedUserFilter.doFilter(BaseAuthenticatedUserFilter.java:249) 499318 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 499319 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 499320 at com.millennialmedia.session.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:57) 499321 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 499322 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 499323 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 499324 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 499325 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433) 499326 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) 499327 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 499328 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 499329 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286) 499330 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845) 499331 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)

4

3 回答 3

5

我知道这篇文章已经过时了,但是由于我今天遇到了这个问题,我想我可以抽出一些时间。您收到此错误的原因是您使用的是 Java 6 之前(和 JDBC-4 之前)的 Tomcat 版本,其中

java.sql.Statement.isClosed()

不是接口一部分:

http://docs.oracle.com/javase/1.5.0/docs/api/java/sql/Statement.html

但在 Java 6 中,它是:

http://docs.oracle.com/javase/6/docs/api/java/sql/Statement.html#isClosed%28%29

在此处查看 isClosed() 的方法可见性

http://www.jarvana.com/jarvana/view/org/apache/tomcat/dbcp/6.0.20/dbcp-6.0.20.jar!/org/apache/tomcat/dbcp/dbcp/DelegatingStatement.class?classDetails =好的

与这里:

http://www.jarvana.com/jarvana/view/org/apache/tomcat/tomcat-dbcp/7.0.23/tomcat-dbcp-7.0.23.jar!/org/apache/tomcat/dbcp/dbcp/DelegatingStatement .class?classDetails=ok

当前 DelegatingStatement 的 isClose() 的评论说:

/*
* Note was protected prior to JDBC 4
* TODO Consider adding build flags to make this protected unless we are
*      using JDBC 4.
*/
public boolean isClosed() throws SQLException {
    return _closed;
}
于 2013-01-23T10:04:33.920 回答
0

我不明白你为什么还要测试它。该语句此时不可能关闭,所以不要测试它,只需关闭它。同上连接。事实上,如果已经关闭,这两种关闭方法都被指定为不执行任何操作。

不要给百合镀金。

于 2012-09-17T22:50:58.043 回答
0

问题是由于您的数据源为您提供的 PreparedStatement 的实现。如果您查看您提供的跟踪,您可以看到:

499285 java.lang.IllegalAccessError:org.apache.tomcat.dbcp.dbcp.DelegatingStatement.isClosed()Z

这意味着您使用的 PreparedStatement 的实现是类 org.apache.tomcat.dbcp.dbcp.DelegatingStatement 的实例。如果您查看1.2.2版本,方法isClosed()受到保护,因此任何访问此方法的尝试都会导致 java.lang.IllegalAccessError。该库的其他版本(1.3 及更高版本)将此修饰符公开,因此您只需更改库版本即可解决此问题。

于 2019-03-14T17:48:37.217 回答