我正在使用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

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

我遇到了this article个,它使用一个方面来填充异常链,在SQLExceptions的情况下,异常链没有正确填充.

有没有一种方法可以在不使用方面或任何自定义代码的情况下修复此问题.理想的解决方案只涉及配置文件的更改.

推荐答案

无需编写任何自定义代码即可实现这一点——默认情况下,Hibernate将记录异常原因.如果看不到这一点,则必须正确设置Hibernate日志(log).下面是一个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批处理(不用说,您可能不想在生产中这样做)

Postgresql相关问答推荐

时间戳的postgreSQL-to_char如果为零,则不显示微秒

在Postgres中如何连接具有相同名称列的表并更改列名称?

无法将 json 范围读取为 pgtype.Int4range

JPA将没有时区的Postgres时间戳调整为服务器时区

在 Postgresql 中实现自定义运算符时出错

Postgres 查询指向国外数据工作者的分区表比直接查询 fdw 慢很多倍

计算每行的多列中的非 NULL 元素

消除 PostgreSQL SELECT 语句中的重复行

在 CentOS 上安装 psycopg2 时遇到问题

statement_date列是日期类型,但表达式是整数类型

整数除法返回 0

如何在可选参数上查询 postgres?

是否有为 SQL Server 生成完整数据库 DDL 的工具? Postgres 和 MySQL 呢?

在不存在的行上有select for update块

Postgres NOT IN (null) 没有结果

将数据推送到 Heroku 时出错:time zone displacement out of range

postgres regexp_replace 只想允许 a-z 和 A-Z

错误:索引表达式中的函数必须在 Postgres 中标记为 IMMUTABLE

如何为 adminpack 解决 PostgreSQL pgAdmin 错误Server instrumentation not installed?

错误:permission denied for language c