@transactional不工作春天4休眠4

时间:2014-09-02 12:33:41

标签: spring hibernate transactions

我想使用spring的transactionManager。 我的弹簧配置在这里:

<?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>

这是我的经理课程之一:

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()方法从spring 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 个答案:

答案 0 :(得分:0)

在你的情况下,Spring默认会使用JDK代理。这将要求您在接口级别拥有@Transactional

要在类级别启用@Transactional,您可以将Spring配置为使用cglib代理,如下所示

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

更多详情here

相关问题