Skip to content

Instantly share code, notes, and snippets.

@jabaraster
Last active December 17, 2015 06:48
Show Gist options
  • Save jabaraster/5568075 to your computer and use it in GitHub Desktop.
Save jabaraster/5568075 to your computer and use it in GitHub Desktop.
hibernate-entitymanagerにて分かりにくい例外に遭遇。 EntityManagerFactoryをcloseしたのにEntityTransactionでトランザクションを確定しようとすると、直感にかなり反する例外がスローされる。 最少のサンプルを残す。
Exception in thread "main" javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:92)
at sandbox.Sample.main(Sample.java:17)
Caused by: javax.persistence.PersistenceException: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.event.service.spi.EventListenerRegistry]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1365)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1293)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:80)
... 1 more
Caused by: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.event.service.spi.EventListenerRegistry]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:126)
at org.hibernate.internal.SessionImpl.eventListenerGroup(SessionImpl.java:705)
at org.hibernate.internal.SessionImpl.listeners(SessionImpl.java:701)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1180)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:379)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:75)
... 1 more
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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">
<persistence-unit name="HibernateException" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:target/db/db" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
</properties>
</persistence-unit>
</persistence>
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sandbox</groupId>
<artifactId>HibernateException</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>HibernateException Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.171</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>sandbox.Sample</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
package sandbox;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class Sample {
@SuppressWarnings({ "nls" })
public static void main(final String[] pArgs) throws NamingException {
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("HibernateException");
final EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
emf.close();
em.getTransaction().commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment