本文介绍了PSQLException没有被抓住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tomcat 6和Postgresql 8.4。我的代码如下:

I am using Tomcat 6 and Postgresql 8.4. My code looks as follows:

try {
  // Prepared statement inserting something...
} catch (final PSQLException e) {
  LOG.log(Level.SEVERE, "Saving failed.", e);
} catch (final SQLException e) {
  LOG.log(Level.SEVERE, "Saving failed (SQL).", e);
}

此插入到数据库中可能会导致PSQLException(例如,唯一键冲突),这会导致我想抓住第一个 catch 。但是,我实际上在日志中找到的是:

This insert into DB may cause a PSQLException (e.g. a unique key violation) which I want to catch with the first catch. However, what I actually find in the log is this:

SEVERE: Saving failed (SQL).
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "UZIVATELIA_U_LOGIN"
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:273)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
    at cz.ecoremedia.realpad.web.backend.Users.saveNewUser(Users.java:119)
    at cz.ecoremedia.realpad.web.backend.Users.saveUser(Users.java:237)
    at org.apache.jsp.User_jsp._jspService(User_jsp.java:159)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)

例如,如果我检查了捕获的 e 的类型,像这样:

If I check the type of e I caught, for example like this:

} catch (final SQLException e) {
  LOG.log(Level.SEVERE, e.getClass().getName());
  LOG.log(Level.SEVERE, "Saving failed (SQL).", e);

它仍然告诉我这是 org.postgresql.util.PSQLException

当我在Eclipse中本地尝试时,它可以正常工作- PSQLException 被捕获正确地在第一个 catch 块中。

When I try this locally in my Eclipse, it works perfectly - PSQLException gets caught correctly in the first catch block.

如果我正确理解了问题,则 org我发现的.postgresql.util.PSQLException 是与实际上抛出的 org.postgresql.util.PSQLException 不同的类。这怎么可能?我在做什么错了?

If I understand the problem correctly, the org.postgresql.util.PSQLException I catch is a different class from the org.postgresql.util.PSQLException that actually gets thrown. How is this possible? What am I doing wrong?

感谢您的想法。

推荐答案

结果是,我的Tomcat的 lib / 文件夹中有另一个JDBC驱动程序,除了 WEB-INF / lib /

Turns out, I had another JDBC driver in my Tomcat's lib/ folder, apart from the one in WEB-INF/lib/. Removing the Tomcat's one fixed my problem.

(是的,这是另一个类加载器问题,感谢@NathanHughes。)

(Yes, it was a different class loader problem, thanks @NathanHughes.)

这篇关于PSQLException没有被抓住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 09:19