首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何集成spring与hibernate会话和事务管理?

如何集成spring与hibernate会话和事务管理?
EN

Stack Overflow用户
提问于 2010-11-28 02:05:58
回答 2查看 16.1K关注 0票数 16

我是hibernate和spring的初学者。我已经理解了hibernate事务的界限(至少我这么认为)。但在像这样编写了几个方法之后:

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" />

我已经用@transaction标记了我的服务类,但是我总是得到"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在没有活动事务的情况下无效“。

更新:我尝试从外部单元测试(即从实际的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。我使用的是hibernate 3.3.2和spring 2.5.6。

我能有一些关于如何解决这个问题的建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-28 21:11:08

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

<property name="current_session_context_class">thread</property> 
票数 15
EN

Stack Overflow用户

发布于 2013-03-04 19:18:18

@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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4293098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档