当前位置: 首页 > 面试题库 >

在Spring Transaction JUnit测试中自动连接休眠会话的正确方法

龚寂弦
2023-03-14
问题内容

这个问题类似于上一个问题。我正在尝试通过@AutowireSpring-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");
    }
}

如果我@Autowirea 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");
    }
}

但是,如果我像这样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>

我的问题是:为什么<aop:scoped-proxy/>在我的单元测试中应该只有一个线程绑定的事务上下文,这是为什么?定义我的bean 的正确方法 什么Hibernate Session


问题答案:

SessionFactoryUtils.getSession()与获取Session的任何其他方式一样好。它所做的与HibernateDaoSupport.getSession()相同。

需要使用scoped-proxy的原因是由于时间问题。如果没有scoped-
proxy,似乎它是在测试开始之前,因此在事务开始之前注入会话,因此您会得到错误。

通过添加scoped-
proxy,它可以代理Session并进行注入,因此它不会预先注入实际的会话(在事务开始之前),而仅在测试运行时(实际上需要进行测试时)获取并进行调用。反对它。



 类似资料:
  • 问题内容: 一个只有一个数据库的hibernate会话将保持多少个连接? 问题答案: 在给定的时间,给定的会话将仅保留一个连接,您可以使用connect()方法进行访问。 使用reconnect()方法可以更改所使用的连接。

  • 问题内容: 我想从hibernate会话中获取jdbc连接。hibernate会话中有方法,即session.connection();。但已被弃用。我知道这仍然有效,但是我不想使用已弃用的方法,因为我确定他们必须为此提供一些替代方法?在http://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/Session.html上,连接方法api表示

  • 问题内容: 我有以下方法,每隔几秒钟插入一大批记录。运行一段时间后,出现如下错误: 错误:通讯链接失败 从服务器成功接收到的最后一个数据包是523毫秒前。成功发送到服务器的最后一个数据包是8毫秒前。 2013年5月16日,上午9:48:30 com.mchange.v2.c3p0.stmt.GooGooStatementCache checkinStatement INFO:签入语句出现问题,被丢

  • 问题内容: 我在hibernate和延迟加载方面遇到问题。 背景:我有一个Spring MVC Web应用程序,我将Hibernate用于持久层。我正在使用OpenSessionInViewFilter使我能够在视图层中延迟加载实体。我正在扩展HibernateDaoSupport类,并使用HibernateTemplate保存/加载对象。一切都进行得很好。直至现在。 问题:我有一个可以通过Web

  • 问题内容: 能否请您告诉我,我如何才能很好地为Hibernate实体启用Spring自动装配? 假设我有一个实体,并希望在那里有邮件发送者: 有没有比做的更好的方法 在我的DAO中? 谢谢! 问题答案: 有可能的!(这是Spring Roo中的默认样式!) 您所需要做的就是将@Configurable批注添加到您的实体。在配置中并使用AspectJ编织激活注释。 Spring参考中有一章:7.8.

  • 问题内容: Hibernate可以选择自动检测。如何获取该自动检测的值?我找不到与此有关的任何信息。 问题答案: 您可以从SessionFactory检索它,但首先需要将其转换为SessionFactoryImplementor: 上面的代码将检索会话工厂 当前正在使用 的方言实例,如果未通过属性明确指定,则该实例为自动检测到的实例。