java - 找出哪一行代码正在生成 SQL 异常

标签 java playframework

我有一个 Actor(Play Framework,Java)用于定期导入一些数据库数据。这个参与者调用各种其他类来执行导入、持久化等操作。我当前的问题是我无法确定生成 SQL 异常的确切行号和文件。例如,我收到这样的错误:

[info] application - javax.persistence.PersistenceException: ERROR executing DML bindLog[] error[ERROR: null value in column "email" violates not-null constraint\n   Detail: Failing row contains (266, null, null, null).]
[info] application - Starting persisting of customer id 29917837
[error] o.j.StatementLogger - insert into emails (email, domain, user_id) values (null,null,null);
throws exception: org.postgresql.util.PSQLException: ERROR: null value in column "email" violates not-null constraint
  Detail: Failing row contains (268, null, null, null).
org.postgresql.util.PSQLException: ERROR: null value in column "email" violates not-null constraint
  Detail: Failing row contains (268, null, null, null).
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2182)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1911)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:173)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:645)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:495)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:441)
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61)
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java)
    at sun.reflect.GeneratedMethodAccessor16.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

当然,我可以查看错误消息,发现某处有东西试图保留我的电子邮件模型而不填充它。现在代码库真的很小(约 200-300 行),我可以非常准确地猜到这是从哪里来的。但是框架不应该也报告行号吗?或许这就是使用 actor 时发生的情况,因为它们在某种程度上处于系统之外?

最佳答案

您正在使用一个使用反射执行其查询的框架,这使得调试非常困难。我不知道你使用的具体框架,但很可能它有可以设置的配置,使调试这类事情更容易。

如果不是,或者是您自己的代码在使用反射,您可以捕获异常,包装它,然后从“外部”代码(执行反射操作的代码)中重新抛出它。这将为您提供一个额外的堆栈跟踪,其中包含它在您的代码中发生的确切位置。

例如,您可以使用包装器代码执行此操作:

try {
     this.custPersister.persist(customer);
} catch (Exception e) {
     throw new Exception(e);
}

只需确保在您的外部代码中的适当位置捕获此异常,并记录堆栈跟踪。

关于java - 找出哪一行代码正在生成 SQL 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41128745/

相关文章:

java - "exception in thread main/by zero"错误

java - java.sql.Timestamp 时区特定吗?

Java:从具有缓冲输入的随机访问文件中读取字符串

scala - WSClient Play Framework SSL 设置

java - 制作[1] : Entering directory error message

java - Spring Web 服务响应中没有端点适配器

java - 无法在 Play 2 中使用多个 ebean 数据库

java - 与锡耶纳玩耍的多重关系

java - Jenkins + 玩 1.2.4 : problems with cobertura lock files/report

java - 具有枚举字段类型的 Ebean 模型意外生成的演化脚本