java - Call getNextException to see the cause : How to make Hibernate / JPA show the DB server message for an exception -
i using postgresql, hibernate , jpa. whenever there exception in database, not helpful not show went wrong on db server.
caused by: java.sql.batchupdateexception: batch entry 0 update foo set alert_flag='3' aborted. call getnextexception see cause. @ org.postgresql.jdbc2.abstractjdbc2statement$batchresulthandler.handleerror(abstractjdbc2statement.java:2621) @ org.postgresql.core.v3.queryexecutorimpl.processresults(queryexecutorimpl.java:1837) @ org.postgresql.core.v3.queryexecutorimpl.execute(queryexecutorimpl.java:407) @ org.postgresql.jdbc2.abstractjdbc2statement.executebatch(abstractjdbc2statement.java:2754) @ com.mchange.v2.c3p0.impl.newproxypreparedstatement.executebatch(newproxypreparedstatement.java:1723) @ org.hibernate.jdbc.batchingbatcher.doexecutebatch(batchingbatcher.java:70) @ org.hibernate.jdbc.abstractbatcher.executebatch(abstractbatcher.java:268) ... 82 more
i want exception message database appear in application's log.
i came across this article uses aspect populate exception chain otherwise not populated in case of sqlexceptions.
is there way fix without using aspects or custom code. ideal solution involve config file changes.
there no need write custom code achieve - hibernate log exception cause default. if can't see this, hibernate logging must not set correctly. here's example slf4j+log4j, , using maven dependency management.
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 attempt persist object id 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>
executing main method log following:
error [main] - batch entry 0 insert pgexceptiontestbean (label, id) values (null, '1') aborted. call getnextexception see cause. error [main] - error: duplicate key value violates unique constraint "pgexceptiontestbean_pkey"
it's worth mentioning can disable jdbc batching wraps original exception setting property hibernate.jdbc.batch_size
0
(needless don't want in production.)
Comments
Post a Comment