【问题标题】:Call getNextException to see the cause : How to make Hibernate / JPA show the DB server message for an exception调用 getNextException 查看原因:如何让 Hibernate / JPA 显示异常的数据库服务器消息
【发布时间】:2013-03-28 17:17:19
【问题描述】:

我正在使用 Postgresql、Hibernate 和 JPA。每当数据库中出现异常时,我都会得到类似这样的信息,这不是很有帮助,因为它没有显示数据库服务器上真正出了什么问题。

Caused by: java.sql.BatchUpdateException: Batch entry 0 update foo set ALERT_FLAG='3' was aborted.  Call getNextException to see the cause.
    at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2621)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1837)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:407)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2754)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    ... 82 more

我希望数据库中的异常消息出现在应用程序的日志中。

我遇到了this article,它使用 Aspect 填充异常链,否则在 SQLExceptions 的情况下无法正确填充。

有没有办法在不使用 Aspects 或任何自定义代码的情况下解决此问题。理想的解决方案只涉及配置文件更改。

【问题讨论】:

  • 你是如何输出异常的?我使用 log4j 和 slf4j 的经验,我免费得到了由线路引起的......
  • 日志库确实会打印原因,但如果 excption 遵循异常链接约定,它们会这样做。即每个异常都应该通过 getCause() 方法返回其直接根本原因。显然,SQLException 不遵循这个约定。我链接的文章解释了相同的内容。因此作者编写了一个切面,它将 getNextException() 返回的对象设置为父异常中的原因。
  • @Priyank SQLException 在发生多个(不相关的?)异常时使用异常链(可以使用迭代器或使用 getNextException() 进行迭代)。这个概念与原因链正交。在这个具体的例子中,我确实认为它应该被设置为原因。
  • 我正在寻找比编写自定义代码更好的解决方案。我认为这个问题(无法看到 DB 消息)太常见了,没有更优雅的解决方案。
  • ryanp 是对的,但他的回答很冗长:简而言之,由于批量插入,您的日志中应该有一个来自SqlExceptionHelper 的错误行(在您的堆栈跟踪上方),原因是:null value in column "id" violates not-null constraint

标签: java hibernate postgresql jpa jdbc


【解决方案1】:

这对我来说得到了导致问题的异常消息(Hibernate 3.2.5.ga):

catch (JDBCException jdbce) {
    jdbce.getSQLException().getNextException().printStackTrace();
}

【讨论】:

  • 你可以直接去,catch (SQLException e) { e.getNextException().printStackTrace(); }
【解决方案2】:

不需要编写任何自定义代码来实现这一点 - Hibernate 默认会记录异常原因。如果您看不到这一点,则一定不能正确设置 Hibernate 日志记录。这是一个使用 slf4j+log4j 的示例,并使用 Maven 进行依赖管理。

src/main/java/pgextest/PGExceptionTest.java

public class PGExceptionTest {

    public static void main(String[] args) throws Exception {

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(
                "pgextest");
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        // here I attempt to persist an object with an ID that is already in use
        entityManager.persist(new PGExceptionTestBean(1));
        entityManager.getTransaction().commit();
        entityManager.close();
    }
}

src/main/resources/log4j.properties

log4j.rootLogger=ERROR, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

src/main/resources/META-INF/persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
        version="2.0">
    <persistence-unit name="pgextest">
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/pgextest"/>
            <property name="javax.persistence.jdbc.user" value="postgres"/>
            <property name="javax.persistence.jdbc.password" value="postgres"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="hibernate.jdbc.batch_size" value="5"/>
        </properties>
    </persistence-unit>
</persistence>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>pgextest</groupId>
    <artifactId>pgextest</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.6.9.Final</version>
        </dependency>   

        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901.jdbc4</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>

执行 main 方法将记录以下内容:

ERROR [main] - Batch entry 0 insert into PGExceptionTestBean (label, id) values (NULL, '1') was aborted.  Call getNextException to see the cause.
ERROR [main] - ERROR: duplicate key value violates unique constraint "pgexceptiontestbean_pkey"

可能值得一提的是,您可以通过将属性 hibernate.jdbc.batch_size 设置为 0 来禁用包装原始异常的 JDBC 批处理(不用说您可能不想在生产中这样做。)

【讨论】:

  • 是的!我知道异常消息是什么,所以在控制台日志中进行了搜索,我发现了一条带有消息的小行。因此,它确实出现了,但没有完整堆栈跟踪的花里胡哨。这对我来说已经足够了。
  • 在 Play 2.2.x 中可以做这样的事情吗?它使用 logback 进行日志记录
  • 在我的情况下,“java.sql.BatchUpdateException: Batch entry 0 delete from account where id=32 was aborted. Call getNextException to see the cause...” org.slf4j+log4j 是解决方案。必须将日志级别设置为 DEBUG 才能查看根本原因。
【解决方案3】:

我认为 Aspect Programming 是解决这类问题的更好方案。

但是,如果您想编写自定义代码来执行此操作,您可以捕获 SqlException 并循环遍历它并记录每个异常。这样的事情应该可以工作。

try {
 // whatever your code is
} catch (SQLException e) {
    while(e!= null) {
      logger.log(e);
      e = e.getNextException();
    }
}

【讨论】:

  • 这看起来根本不对。您为每个异常调用 getNextException 三次!
  • 您可以通过取出变量将其优化为一个...但从技术上讲,它没有任何问题。它会给您同样的异常,因为它是一个 get...
  • @Victor Grazi 第一次是正确的。 getNextException() 每次调用都会得到不同的异常,直到最终返回 null。
【解决方案4】:

对我来说,异常是 PersistenceException,所以我必须这样做:

try {
//...
} catch (javax.persistence.PersistenceException e) {
    log.error(((java.sql.BatchUpdateException) e.getCause().getCause()).getNextException());
}

【讨论】:

    【解决方案5】:
    try {
     // code
    } catch (SQLException e) {      
      for (Throwable throwable : e) {
            log.error("{}", throwable);
      }
    }
    

    【讨论】:

    • 喜欢这个主意。在 log4j 1.2.15 上,占位符不会被我替换,所以我会写自己的消息来跟踪。
    【解决方案6】:

    以防万一您从 JUnit 测试中获得此异常,您可以使用 TestRule 转换 JUnit 异常(这是受 ExpectedExceptionTestRule 的来源的启发)

    public class HibernateBatchUnwindRule implements TestRule {
    
        private boolean handleAssumptionViolatedExceptions = false;
    
        private boolean handleAssertionErrors = false;
    
        private HibernateBatchUnwindRule() {
        }
    
        public static HibernateBatchUnwindRule create(){
            return new HibernateBatchUnwindRule();
        }
    
        public HibernateBatchUnwindRule handleAssertionErrors() {
            handleAssertionErrors = true;
            return this;
        }
    
        public HibernateBatchUnwindRule handleAssumptionViolatedExceptions() {
            handleAssumptionViolatedExceptions = true;
            return this;
        }
    
        public Statement apply(Statement base,
                org.junit.runner.Description description) {
            return new ExpectedExceptionStatement(base);
        }
    
        private class ExpectedExceptionStatement extends Statement {
            private final Statement fNext;
    
            public ExpectedExceptionStatement(Statement base) {
                fNext = base;
            }
    
            @Override
            public void evaluate() throws Throwable {
                try {
                    fNext.evaluate();
                } catch (AssumptionViolatedException e) {
                    optionallyHandleException(e, handleAssumptionViolatedExceptions);
                } catch (AssertionError e) {
                    optionallyHandleException(e, handleAssertionErrors);
                } catch (Throwable e) {
                    handleException(e);
                }
            }
        }
    
        private void optionallyHandleException(Throwable e, boolean handleException)
                throws Throwable {
            if (handleException) {
                handleException(e);
            } else {
                throw e;
            }
        }
    
        private void handleException(Throwable e) throws Throwable {
            Throwable cause = e.getCause();
            while (cause != null) {
                if (cause instanceof BatchUpdateException) {
                    BatchUpdateException batchUpdateException = (BatchUpdateException) cause;
                    throw batchUpdateException.getNextException();
                }
                cause = cause.getCause();
            };
            throw e; 
        }
    }
    

    然后将规则添加到测试用例中

    public class SomeTest {
    
        @Rule
        public HibernateBatchUnwindRule batchUnwindRule = HibernateBatchUnwindRule.create();
    
        @Test
        public void testSomething(){...}
      }
    

    【讨论】:

      【解决方案7】:

      如果您偶然在 Kafka-Connect 中遇到此异常,您可以将 batch.size 属性设置为 0(临时)以显示您的 sink worker 遇到的异常。

      【讨论】:

        【解决方案8】:

        就我而言,当我使用 java 和 postgreSQL 数据库时,我遇到了这个异常。在检查了我尝试inserttable 的记录后,发现该记录具有重复的 id 并且违反了唯一约束。因此,最好检查您要去insert 的记录并尝试使用db 客户端查看insert 以查看提取错误。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-01-02
          • 1970-01-01
          • 2016-05-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-25
          • 2011-04-07
          相关资源
          最近更新 更多