|
I have created a small demo application, where after creating an EntityManagerFactory and an EntityManager, I simply do a select query and then close the EntityManager first and then the EntityManagerFactory. But when closing the Factory, I get the following Exception:
java.sql.SQLException: Cannot close a connection while a transaction is still active.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.newSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.checkForTransactionInProgress(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.close(Unknown Source)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.stop(DriverManagerConnectionProviderImpl.java:275)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.stopService(AbstractServiceRegistryImpl.java:360)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.destroy(AbstractServiceRegistryImpl.java:344)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.deRegisterChild(AbstractServiceRegistryImpl.java:393)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.destroy(AbstractServiceRegistryImpl.java:351)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$ServiceRegistryCloser.sessionFactoryClosed(EntityManagerFactoryBuilderImpl.java:842)
at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryClosed(SessionFactoryObserverChain.java:48)
at org.hibernate.internal.SessionFactoryImpl.close(SessionFactoryImpl.java:1066)
at org.hibernate.jpa.internal.EntityManagerFactoryImpl.close(EntityManagerFactoryImpl.java:339)
at com.stackoverflow.jpa.emf.close.example.Run.main(Run.java:24)
Caused by: ERROR 25001: Cannot close a connection while a transaction is still active.
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory.wrapArgsForTransportAcrossDRDA(Unknown Source)
... 17 more
Since I do not use or need any transaction for a simply select query, I think that there might be an error in Hibernate. The Exception will not be raised, when I start a transaction before doing the select and close it afterwards. But it seems a bit strange, that I have to do a transaction for every select!
See the attachement for the demo application.
|