java.lang.IllegalStateException:EntityManager已使用HSQL关闭
出于教育目的,我正在构建一个简单的SpringWeb应用程序,但持久性级别有一个小问题—java.lang.IllegalStateException:EntityManager is closed,当我试图从数据库中获取某些内容时抛出。对象是持久化的,但当我尝试进行查询时,会抛出异常。因此,我将Hibernate与HSQL inmemory db一起使用,下面是我的persistence.xml:java.lang.IllegalStateException:EntityManager已使用HSQL关闭,java,spring,hsqldb,Java,Spring,Hsqldb,出于教育目的,我正在构建一个简单的SpringWeb应用程序,但持久性级别有一个小问题—java.lang.IllegalStateException:EntityManager is closed,当我试图从数据库中获取某些内容时抛出。对象是持久化的,但当我尝试进行查询时,会抛出异常。因此,我将Hibernate与HSQL inmemory db一起使用,下面是我的persistence.xml: <?xml version="1.0" encoding="UTF-8"?> <
<?xml version="1.0" encoding="UTF-8"?>
<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="pp_persistence_unit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testDB"/>
<property name="sqltool.charset" value="UTF-8" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="current_session_context_class" value="thread"/>
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
这里是getConnectionsCount方法
public int getConnectionsCount() {
Query query = entityManager.createQuery("select count(c.id) from Connection c");
return (Integer)query.getSingleResult();
}
我真的不知道是什么问题。我假设它可能来自数据库的编码,但我尝试将其设置为UTF-8,但错误仍然是一样的
很抱歉忘记了,这是我的application-context.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<context:component-scan base-package="web.task.business">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
<context:component-scan base-package="web.task.dao">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>
<context:component-scan base-package="web.task.transformation">
<context:include-filter type="annotation" expression="web.task.annotations.Transformation" />
</context:component-scan>
<bean id="ppEntityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="pp_persistence_unit" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="ppEntityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
请共享您的spring上下文xml抱歉,我真的忘记了-帖子已上载。您可以发布web.xml吗?您还可以发布您的web.task.dao.ConnectionDao.getconnectionscout方法吗?我发布它没有问题,但每当我尝试从数据库获取内容时,都会引发该异常
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<context:component-scan base-package="web.task.business">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
<context:component-scan base-package="web.task.dao">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>
<context:component-scan base-package="web.task.transformation">
<context:include-filter type="annotation" expression="web.task.annotations.Transformation" />
</context:component-scan>
<bean id="ppEntityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="pp_persistence_unit" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="ppEntityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />