Java 调用getNextException查看原因:如何使Hibernate/JPA显示异常的DB server消息
我正在使用Postgresql、Hibernate和JPA。每当数据库中出现异常时,我都会得到类似这样的结果,这不是很有帮助,因为它没有显示数据库服务器上到底出了什么问题Java 调用getNextException查看原因:如何使Hibernate/JPA显示异常的DB server消息,java,hibernate,postgresql,jpa,jdbc,Java,Hibernate,Postgresql,Jpa,Jdbc,我正在使用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.jd
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
我希望数据库中的异常消息出现在应用程序的日志中
我遇到了一个使用方面来填充异常链的方法,在SQLExceptions的情况下,该方法无法正确填充异常链
有没有一种方法可以在不使用方面或任何自定义代码的情况下修复此问题。理想的解决方案只涉及配置文件更改。我认为方面编程是解决此类问题的更好的解决方案 但是,如果您想编写一个自定义代码来实现这一点,您可以捕获SqlException并循环遍历它并记录每个异常。这样的办法应该行得通
try {
// whatever your code is
} catch (SQLException e) {
while(e!= null) {
logger.log(e);
e = e.getNextException();
}
}
无需编写任何自定义代码即可实现此目的-默认情况下,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>
<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>
可能值得一提的是,您可以通过将属性
hibernate.JDBC.batch_size
设置为0
(不用说,您可能不想在生产中这样做)来禁用包装原始异常的JDBC批处理万一您从JUnit测试内部获得此异常,您可以使用TestRule
转换JUnit异常(这是受ExpectedException
TestRule
的源代码启发的)
然后将规则添加到测试用例中
public class SomeTest {
@Rule
public HibernateBatchUnwindRule batchUnwindRule = HibernateBatchUnwindRule.create();
@Test
public void testSomething(){...}
}
这对我来说起到了作用,得到了导致问题的异常消息(Hibernate 3.2.5.ga):
对我来说,异常是PersistenceException,所以我必须这样做:
try {
//...
} catch (javax.persistence.PersistenceException e) {
log.error(((java.sql.BatchUpdateException) e.getCause().getCause()).getNextException());
}
如果您碰巧在Kafka Connect中遇到此异常,可以将batch.size属性设置为0(临时)以显示接收器工作程序遇到的异常。如何输出异常?根据我对log4j和slf4j的经验,我可以免费获得由行引起的结果……日志库确实会打印原因,但如果exoption遵循异常链接约定,它们就会打印。i、 e.每个异常都应该通过getCause()方法返回其直接根本原因。显然,SQLException不遵循此约定。我链接的那篇文章也解释了同样的问题。因此,作者编写了一个方面,它接受getNextException()返回的对象,并将其设置为父异常的原因。@Priyank
SQLException
在发生多个(不相关的?)异常时使用异常链(可以使用迭代器或使用getNextException()
对其进行迭代)。这个概念与原因链是正交的。在这个具体的例子中,我确实认为应该将其设置为原因。我正在寻找一个比编写自定义代码更好的解决方案。我认为,这个问题(无法看到DB消息)太常见了,没有更优雅的解决方案是不可能的。ryanp是对的,但他的答案很详细:简而言之,由于批量插入,您应该在日志中(stacktrace上方)有一个错误行,该行来自SqlExceptionHelper
,带有原因,例如:列“id”中的null值违反非空约束
是!我知道异常消息应该是什么,所以在控制台日志中搜索,我发现了一行很小的消息。因此,它确实出现了,但没有完整堆栈跟踪的钟声和哨声。这对我来说已经足够好了。在游戏2.2.x中可以做类似的事情吗?它使用logback进行日志记录在我的案例中,“java.sql.BatchUpdateException:批处理条目0从id=32的帐户中删除。调用getNextException查看原因…”org.slf4j+log4j是解决方案。日志级别必须设置为调试才能查看根本原因。我喜欢这个想法。在log4j1.2.15上,占位符不会被我替换,所以我将编写自己的消息来跟踪跟踪。这看起来一点也不正确。您正在为每个异常调用getNextException三次!!您可以通过去掉变量将其优化为1。。。但从技术上讲,这并没有什么错。。它会给你同样的例外,因为它是一个get…@Victor Grazi第一次是正确的getNextException()
每次调用它都会获取不同的异常,直到它最终返回null。您可以直接使用,catch(SQLException e){e.getNextException().printStackTrace();}
public class SomeTest {
@Rule
public HibernateBatchUnwindRule batchUnwindRule = HibernateBatchUnwindRule.create();
@Test
public void testSomething(){...}
}
try {
// code
} catch (SQLException e) {
for (Throwable throwable : e) {
log.error("{}", throwable);
}
}
catch (JDBCException jdbce) {
jdbce.getSQLException().getNextException().printStackTrace();
}
try {
//...
} catch (javax.persistence.PersistenceException e) {
log.error(((java.sql.BatchUpdateException) e.getCause().getCause()).getNextException());
}