2010-11-27 66 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

相關問題