gpt4 book ai didi

java - PSQLException 未被捕获

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:13:20 28 4
gpt4 key购买 nike

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

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

插入 DB 可能会导致 PSQLException(例如唯一键违规),我想用第一个 catch 捕获它。然而,我在日志中实际找到的是这样的:

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 的类型,例如:

} 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 block 中被正确捕获。

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

感谢您的想法。

最佳答案

事实证明,我的 Tomcat 的 lib/ 文件夹中有另一个 JDBC 驱动程序,而不是 WEB-INF/lib/ 中的驱动程序。删除 Tomcat 解决了我的问题。

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

关于java - PSQLException 未被捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10742577/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com