2014-09-02 95 views
2

我想使用spring的transactionManager。 我的春天配置是在这里:@transactional不工作春天4冬眠4

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


<bean id="dataSource" 
     class="org.apache.commons.dbcp.BasicDataSource" 
     depends-on="propertyPlaceholderConfigurer" 
     p:driverClassName="org.postgresql.Driver" 
     p:url="${db.url}" 
     p:username="${db.username}" 
     p:password="${db.password}" 
     destroy-method="close" /> 




<context:spring-configured /> 
<context:annotation-config /> 

<bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" 
     depends-on="flywayAutomaticMigrationBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="packagesToScan" value="com.example" /> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
      <prop key="hibernate.show_sql">false</prop> 
      <prop key="hibernate.default_schema">restProj</prop> 
     </props> 
    </property> 

    <property name="mappingResources"> 
     <list> 
      <value>com/example/db/hbm/user/AuthUser.hbm.xml</value> 
      <value>com/example/db/hbm/user/User.hbm.xml</value> 
      <value>com/example/db/hbm/user/Role.hbm.xml</value> 
      <value>com/example/db/hbm/user/Feature.hbm.xml</value> 

     </list> 
    </property> 
</bean> 

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

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

<context:component-scan base-package="com.example.model" > 
    <context:include-filter type="regex" expression=".*\.dao\..*DAO"/> 
    <context:include-filter type="regex" expression=".*\.logic\..*Mgr"/> 
</context:component-scan> 
<context:component-scan base-package="com.example.commons" > 
    <context:include-filter type="regex" expression=".*\.dao\..*DAO"/> 
    <context:include-filter type="regex" expression=".*\.logic\..*Mgr"/> 
</context:component-scan> 


</beans> 

这是我Manager类中的一种:

import org.springframework.transaction.annotation.Transactional; 

public class UserMgr extends BaseUserMgr { 

    @Transactional (rollbackFor = Exception.class) 
    public void checkTransaction(){ 
     Feature feature = new Feature(); 
     feature.setExtuid(UUID.randomUUID().toString()); 
     feature.setName("boogh"); 
     feature.setDescription("salam"); 
     FeatureDAO.getInstance().save(feature); 
    } 
} 

当我运行UserMgr.checkTransaction()数据不保存。 任何人都可以向我解释什么是错的?

__UPDATE___

这是我FeatureDao保存方法:

 public java.lang.Long save(com.example.model.user.Feature feature) 
     throws org.hibernate.HibernateException { 
     return (java.lang.Long) super.save(feature); 
    } 

,这是超保存方法:

protected Serializable save(final Object obj) { 
    return save(obj, getSession()); 
} 

和getSession()方法得到春天SessionFactory的会议。

这是我FeatureDao getInstance()方法:

public static com.model.user.dao.FeatureDAO getInstance() { 
    return com.example.core.spring.ApplicationContextUtil.getApplicationContext().getBean(com.example.model.user.dao.FeatureDAO.class); 
} 
+1

我觉得你FeatureDAO不是一个Spring bean =>它不是由Spring包裹AOP =>它独立管理会话和事务,而无需处理Spring事务。 **请添加FeatureDAO源代码,这个问题要回答**。 – 2014-09-02 12:38:44

+0

FeatureDao加载到 2014-09-02 12:43:23

+0

我更新了帖子。 :) – 2014-09-02 12:57:26

回答

0

在你的情况春天会默认使用JDK代理。这将要求您在界面级别拥有@Transactional

要启用类级别使用@Transactional你可以春天配置为使用CGLIB代理如下

<tx:annotation-driven transaction-manager="transactionManager" 
proxy-target-class="true" /> 

更多细节here