2013-10-23 31 views
0

我在我的應用程序中使用了spring + hibernate + jersey。我想使用事務,所以我在我的服務層中使用了spring的@Transactional註釋。這是我的hibernate.cfg.xml:如果使用事務註釋時沒有活動事務錯誤,則保存無效

<hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db</property> 
    <property name="hibernate.connection.username">user</property> 
    </session-factory> 
</hibernate-configuration> 

我沒有用session_context這裏,所以春季可以管理它。在我的applicationContext.xml中,我已經定義transactionManager的:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 
    <property name="driverClass" value="com.mysql.jdbc.Driver"/> 
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db"/> 
    <property name="user" value="username"/> 
</bean> 

<context:annotation-config/> 
<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="packagesToScan"> 
     <list> 
      <value>com.hibernate.pojos</value> 
     </list> 
    </property> 
</bean> 

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 

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

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

所有URL匹配/ API/V1/*地圖使用一個servlet命名的球衣和servlet類是com.sun.jersey.spi.spring.container.servlet.SpringServlet,而我已經通過com.services爲包掃描。在這個包我有一個類:

@Path("/app") 
@Component 
public class testApi() { 
    @Autowired 
    private DAOImpl daoimpl; 

    @POST 
    @Path("/create") 
    @Consumes(MediaType.APPLICATION_JSON) 
    @Produces(MediaType.APPLICATION_JSON) 
    @Transactional(rollbackFor = {Exception.class}) 
    public Map<String,Object> testHello(user u) { 
     Map response = daoimpl.save(u); 
     return response; 
    } 
} 

daoimpl有SessionFactory的自動裝配,並使用sessionFactory.getCurrentSession()方法獲取會話。方法daoimpl.save(obj)只是將其保存在數據庫中。由於我將testHello標記爲事務性的,所以我期望一個由Spring管理的事務開始,然後控制權應該發送到實際保存發生的地方。但是,如果沒有活動事務,我得到的保存無效。我已經看到很多帖子,其中session_context在hibernate配置中被提及,因此spring無法處理事務。但就我而言,我確保我不提供任何session_context。我錯過了什麼?我甚至嘗試在DAO中添加@transactional,因爲在我的示例應用程序中,我只是發出一個數據庫調用服務。但是這也沒有效果。

回答

-1

可能是因爲你正在通過hibernate.cfg.xml指定會話工廠,然後在Spring中再次傳遞給事務管理器。

不知道說實話,但我從來沒有使用hibenate.cfg.xml,然後下面的作品爲我(與您指定添加事務配置)。這也爲您提供了將連接參數指定爲屬性的優點,並允許您通過彈簧配置文件或其他機制輕鬆交換db配置。

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 

     <bean id="sessionFactory" 
       class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
       <property name="dataSource" ref="dataSource" /> 
       <property name="packagesToScan"> 
         <list> 
           <value>uk.co.xyz.domain</value> 
         </list> 
       </property> 
       <property name="hibernateProperties"> 
         <props> 
           <prop key="hibernate.show_sql">${hibernate.showsql}</prop> 
           <prop key="hibernate.format_sql">true</prop> 
           <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
           <prop key="hibernate.hbm2ddl.auto">${hibernate.ddlauto}</prop> 
           <prop key="hibernate.cache.use_second_level_cache">${hibernate.enablecache}</prop> 
           <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> 
         </props> 
       </property> 
     </bean> 

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

</beans> 

在此還看到這實際上表明是不應該事:

applicationContext.xml with datasource or hibernate.cfg.xml. Difference?

+0

我看不出有什麼差別。我仍然遇到同樣的錯誤。 – coder

相關問題