0su*_*ain 11 java junit spring hibernate transactions
这个问题类似于前一个问题.我正在尝试@Autowire在我的一个Spring-JUnit-Transactional测试中进行Hibernate会话但是我得到了这个异常:
java.lang.IllegalStateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional ...
这是我的JUnit类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml"})
@TransactionConfiguration(transactionManager="transactionManager")
@Transactional
public class MyTest {
@Qualifier("session")
@Autowired
private Session session;
@Test
public void testSomething() {
session.get(User.class, "me@here.com");
}
}
Run Code Online (Sandbox Code Playgroud)
如果我@Autowire是a SessionFactory并且以Session编程方式获取我的每一个工作正常(而不是在Spring XML中定义),就像这样:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml"})
@TransactionConfiguration(transactionManager="transactionManager")
@Transactional
public class MyTest{
@Qualifier("sessionFactory")
@Autowired
private SessionFactory sessionFactory;
@Test
public void testSomething() {
Session session = SessionFactoryUtils.getSession(sessionFactory, false);
session.get(User.class, "me@here.com");
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我Session在我的Spring XML中定义我的原样,我可以得到我的原始示例<aop:scoped-proxy />:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
...
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation"><value>classpath:/hibernate.cfg.xml</value></property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="session" class="org.springframework.orm.hibernate3.SessionFactoryUtils" factory-method="getSession" scope="prototype">
<constructor-arg ref="sessionFactory" />
<constructor-arg value="false" />
<!-- This is seems to be needed to get rid of the 'No Hibernate Session' error' -->
<aop:scoped-proxy />
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么<aop:scoped-proxy />需要在我的单元测试中只有一个线程限制的事务上下文?定义我的bean 的正确方法是什么Hibernate Session?
SessionFactoryUtils.getSession()与获取Session的任何其他方式一样好.HibernateDaoSupport.getSession()会做同样的事情.
你需要scoped-proxy的原因是因为时间问题.如果没有scoped-proxy,它似乎是在测试开始之前注入Session,因此在事务开始之前注入会话,因此你会得到错误.
通过添加scoped-proxy,它代理Session并注入它,因此它不会预先注入实际会话(在事务启动之前),而是仅在测试运行时获取它并对其进行调用,此时它实际需要创建一个打电话给它.
| 归档时间: |
|
| 查看次数: |
22975 次 |
| 最近记录: |