【问题标题】:How to integrate spring with hibernate session and transaction management?如何将spring与hibernate会话和事务管理集成?
【发布时间】:2010-11-27 18:05:57
【问题描述】:

我是hibernate和spring的初学者。我已经了解了休眠事务分界(至少我是这么认为的)。但是在编写了一些这样的方法之后:

sessionFactory.getCurrentSession().beginTransaction();
//do something here
sessionFactory.getCurrentSession().endTransaction();

我开始想避免它,并希望在我的方法之外自动完成它,这样我就只写“//在这里做点什么”部分。我已经阅读了有关 TransactionProxyFactoryBean 的信息,并认为 xml 配置很长,并且必须为我想要进行事务处理的每个类重复,所以如果可能的话,我想避免使用它。

我尝试使用@Transactional,但它根本不起作用。我的 applicationContext.xml 中有这些行

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="dataSource" ref="dataSource" />
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

我已经用@Transactional 标记了我的服务类,但我总是得到“xxx 在没有活动事务的情况下无效”。 这是一个给我一个错误的示例代码(顺便说一句,在单元测试中运行):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations =
{
    "classpath:applicationContext.xml"
})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class UserServiceTest
{
    @Resource
    private UserService userService;

    @Test
    public void testAddUser()
    {
        User us = new User();
        us.setName("any name");
        userService.addUser(us);
    }
}

在这种情况下,确切的错误消息是:“org.hibernate.HibernateException: save is not valid without active transaction”。

更新:我尝试从外部单元测试(即从实际的 Web 应用程序)调用 userService.addUser() 方法,我也得到了同样的错误。

这是我的 hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>
        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>
        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <!-- all my mapping resources here -->
    </session-factory>
</hibernate-configuration>

userService 类用@Transactional 标记。我正在使用休眠 3.3.2 和 spring 2.5.6。

我可以就如何解决这个问题提供一些建议吗?

【问题讨论】:

  • 显示您的hibernate.cfg.xml

标签: hibernate spring transactions


【解决方案1】:

删除以下行,它会干扰 Spring 管理的事务:

<property name="current_session_context_class">thread</property> 

【讨论】:

  • 我有类似的问题。但是当我删除此行时出现以下错误。 HibernateException:未配置 CurrentSessionContext! org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:685)
  • 您好,为什么删除此行有帮助?我不明白,我有类似的问题,尝试替换该值也无济于事。如果删除此行,那么默认值是多少?有人知道吗?
  • @hephestos:如果你不指定这个属性,Spring 会为其提供自己的值,以便将其事务管理抽象与 Hibernate 集成,否则 Spring 不会设置它。
  • @axavt,这就是我所理解的,但默认值是什么?因为如果我用三个值覆盖它,似乎没有一个与默认值匹配。而且从谷歌也可以指出,由于配置不同,我无法匹配。所以这就是为什么在这里问
【解决方案2】:

@hephestos:参数current_session_context_class 的值决定了会话(Hibernate 会话)必须绑定到的上下文。

默认情况下它与当前运行的线程绑定。但是当“jta”用于管理事务时,将此参数的值更改为“jta”会将会话绑定到当前的JTA事务上下文。

基本上它定义了会话的上下文。更多信息:http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/architecture.html#architecture-current-session

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 2016-12-02
    • 2013-03-10
    • 2012-10-16
    相关资源
    最近更新 更多