【问题标题】:statement.isClose() is throwing an IllegalAccess Exception because the statement is close?statement.isClose() 因为语句关闭而引发 IllegalAccess 异常?
【发布时间】:2012-09-17 15:26:08
【问题描述】:

我有下面的代码。相当标准的东西。但是,我在 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)

【问题讨论】:

  • 您是否禁用了自动提交模式?只有在禁用自动提交模式时才应使用 commit() 方法。
  • @chaitanya10 答案在代码中。

标签: java database jdbc


【解决方案1】:

我知道这篇文章已经过时了,但是因为我今天遇到了这个问题,我想我可以抽出一些时间。 您收到此错误的原因是您使用的是 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=ok

对比这里:

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;
}

【讨论】:

    【解决方案2】:

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

    不要给百合镀金。

    【讨论】:

    • 嗯,它显然在这一点上关闭了,这就是我得到异常的原因:) 我测试是因为它更安全,例如如果底层连接消失。
    【解决方案3】:

    问题是由于您的数据源提供给您的 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 及更高版本)将此修饰符设为公开,因此您只需更改库版本即可解决此问题。

    【讨论】:

      猜你喜欢
      • 2012-11-26
      • 2016-08-18
      • 2014-01-18
      • 1970-01-01
      • 2018-03-15
      • 2015-07-10
      • 1970-01-01
      • 2014-05-08
      • 2019-11-29
      相关资源
      最近更新 更多