gpt4 book ai didi

java - JUnit-org.hibernate.SessionException : Session is closed

转载 作者:行者123 更新时间:2023-12-01 11:31:29 29 4
gpt4 key购买 nike

我在 Hibernate session 中遇到一个常见错误:javax.persistence.TransactionRequiredException:执行更新/删除 查询。

我找到了一个解决方案,使我的测试类扩展 AbstractTransactionalJUnit4SpringContextTests 但我不明白为什么有必要。

我尝试在我的项目上设置junit(4.11版本)测试。

  • 我的测试类:

_

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:config/ioc/application-context-spring-test.xml" })
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })
@TransactionConfiguration(transactionManager = "myTxManager")
@Transactional
public class UserPreferenceDAOTest extends AbstractTransactionalJUnit4SpringContextTests {

@Autowired
private UserPreferenceDAO dao;

@Test
@Transactional
public void test() {

try {
SQLQuery query = dao.createSQLQuery("SELECT 1 from dual");
query.uniqueResult();
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
}

UserPreferenceDAO 已实例化。 query.uniqueResult();抛出 javax.persistence.TransactionRequiredException 异常。

  • application-context-spring.xml:

包含数据库访问配置:

    <!-- To precise the persistence configuration name file -->
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence-web.xml</value>
</list>
</property>
<property name="defaultDataSource" ref="dataSource" />
</bean>

<!-- EntityManagerFactory definition : JPA one -->
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
<property name="persistenceUnitName" value="myPersistenceUnit" />
</bean>

<!-- Transaction Manager definition : JPA one-->
<bean id="myTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf" />
</bean>

<!-- Enable the annotation usage (bean injection for instance) -->
<context:annotation-config />

<!-- Enable annotation usage for transaction -->
<tx:annotation-driven transaction-manager="myTxManager" proxy-target-class="false"/>

<!-- Enterprise layer's dependencies -->
<!-- UserPreferenceDAO bean statement here -->
<import resource="classpath:ioc/0-model-enterpriselayer-dependencies.xml"/>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@myurl:1522:TD11P15A" />
<property name="username" value="user" />
<property name="password" value="pwd" />
</bean>
  • persistence.xml:

_

<persistence>

<persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL">

<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- with Annotations -->
<class>com.airfrance.padme.server.entity.UserPreference</class>
<!-- etc... (all my entities) -->
<properties>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
</properties>
</persistence-unit>
</persistence>

我发现的大多数教程都没有使用这个 AbstractTransactionalJUnit4SpringContextTests 类。示例如下:http://www.concretepage.com/spring-4/spring-4-junit-4-annotation-example

我哪里错了?

谢谢

最佳答案

根据文档AbstractTransactionalJUnit4SpringContextTests包括以下测试执行监听器:

  • ServletTestExecutionListener
  • DependencyInjectionTestExecutionListener
  • DirtiesContextTestExecutionListener
  • 事务测试执行监听器
  • SqlScriptsTestExecutionListener

如果您不想从 AbstractTransactionalJUnit4SpringContextTests 扩展,但仍希望事务行为,只需将 TransactionalTestExecutionListener 添加到您的监听器:

@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class})

TransactionalTestExecutionListener 的 javadoc:

TestExecutionListener that provides support for executing tests within test-managed transactions by honoring Spring's @Transactional annotation.

关于java - JUnit-org.hibernate.SessionException : Session is closed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30349265/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com