2010-11-27 132 views
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" /> 

,我已经标有@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:保存无效无活动事务”。

UPDATE:我试着从外部单元测试(即从实际的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。

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

+0

显示你的`hibernate.cfg.xml`。 – axtavt 2010-11-27 18:20:21

回答

15

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

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

这是行之有效的,感谢您的帮助。 – Hery 2010-11-30 01:24:50

1

@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

相关问题