Spring-Hibernate @Transactional服务方法不会自动提交

时间:2017-02-06 19:53:40

标签: java spring hibernate

@Component
public interface BenefitRateService {
   void save(EmployeeBenefits benefitRates) throws Exception; 
}

@Service(value="BenefitRateService")
public class BenefitRateServiceImpl implements BenefitRateService {

    @Transactional()
    public void save(EmployeeBenefits benefitRates) throws Exception{
        try{
            benefitRateDao.save(benefitRates);
        }catch(HibernateException e){
            e.printStackTrace();
        }
    }
}

@Component
public interface BenefitRatesDao {
    public void save(EmployeeBenefits benefitRates);
}

@Repository("BenefitRatesDao")
public class BenefitRatesDAOImpl implements BenefitRatesDao {

    @Autowired
    private SessionFactory springSessionCtxFactory;

    public void save(EmployeeBenefits benefitRates) throws Exception{

        Session session = springSessionCtxFactory.getCurrentSession();
        //session.beginTransaction();
        springSessionCtxFactory.getCurrentSession().saveOrUpdate(benefitRates);            
        //session.getTransaction().commit();
    }
}

弹簧-config.xml中

<tx:annotation-driven transaction-manager="springTransactionManager"/> 
<bean id="springTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 <property name="sessionFactory" ref="springSessionCtxFactory" />

<bean id="springSessionCtxFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <list>
            <value>com.benefits</value>
        </list>
    </property>

我正在开发一个具有用户条目EmployeeBenefits表单的Web应用程序。 我试图从数据库中获取EmployeeBenefits对象,修改对象并再次保存。从应用程序的角度来看,似乎数据保存,当我通过单击List All按钮检索所有数据时,我可以看到修改后的数据。但问题是数据永远不会在数据库中更新。 保存的对象在hibernate会话中的某处缓存,但从不进入数据库。 请帮忙。

通过将日志级别设置为Trace,我发现事务实际上已提交。不确定为什么数据库表不会更新。 这是因为hibernate.connection.autocommit属性未设置为true?

2017-02-06 21:30:15,765 [org.springframework.orm.hibernate3.HibernateTransactionManager:365] DEBUG - Creating new transaction with name [com.vdh.budget.service.impl.BenefitRateServiceImpl.save]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''    
2017-02-06 21:30:15,786 [org.springframework.orm.hibernate3.HibernateTransactionManager:493] DEBUG - Opened new Session [org.hibernate.impl.SessionImpl@8f2588] for Hibernate transaction
2017-02-06 21:30:15,788 [org.springframework.orm.hibernate3.HibernateTransactionManager:504] DEBUG - Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@8f2588]
2017-02-06 21:30:15,796 [org.springframework.transaction.support.TransactionSynchronizationManager:183] DEBUG - Bound value [org.springframework.orm.hibernate3.SessionHolder@66eb46] for key [org.hibernate.impl.SessionFactoryImpl@cdc97b] to thread [main]
2017-02-06 21:30:15,797 [org.springframework.transaction.support.TransactionSynchronizationManager:258] DEBUG - Initializing transaction synchronization
2017-02-06 21:30:15,797 [org.springframework.transaction.interceptor.TransactionInterceptor:362] DEBUG - Getting transaction for [com.vdh.budget.service.impl.BenefitRateServiceImpl.save]
2017-02-06 21:30:15,797 [org.springframework.orm.hibernate3.SessionFactoryUtils:316] DEBUG - Opening Hibernate Session
2017-02-06 21:30:15,806 [org.hibernate.SQL:111] DEBUG - update benefits set amount =? where id =?
2017-02-06 21:30:16,055 [org.springframework.transaction.interceptor.TransactionInterceptor:391] DEBUG - Completing transaction for [com.vdh.budget.service.impl.BenefitRateServiceImpl.save]
2017-02-06 21:30:16,055 [org.springframework.orm.hibernate3.HibernateTransactionManager:925] DEBUG - Triggering beforeCommit synchronization
2017-02-06 21:30:16,055 [org.springframework.orm.hibernate3.SessionFactoryUtils:144] DEBUG - Flushing Hibernate Session on transaction synchronization
2017-02-06 21:30:16,170 [org.springframework.orm.hibernate3.HibernateTransactionManager:752] DEBUG - Initiating transaction commit
2017-02-06 21:30:16,170 [org.springframework.orm.hibernate3.HibernateTransactionManager:652] DEBUG - Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@8f2588]

我通过在@Transactional注释中添加(value =“springTransactionManager”)解决了这个问题。 问题是我配置了一个HibernateTransactionManager,它被设置为从线程中检索会话

<property name="hibernate.current_session_context_class">thread</property>

Spring @Transactional没有使用该属性。 所以我配置了一个新的TransactionalMangaer

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

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

<tx:annotation-driven transaction-manager="hibernateTransactionManager"/> 
<tx:annotation-driven transaction-manager="springTransactionManager"/> 
<bean id="springSessionCtxFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>
        </props>
    </property>

在服务类

@Transactional(value = "springTransactionManager"})
public void save(EmployeeBenefits benefitRates);

通过拥有两个事务管理器,没有限定符的@Transactional没有找到我想要的正确的事务管理器。谢谢。

1 个答案:

答案 0 :(得分:0)

您可能使用了错误的@Transactional注释。 Spring和JPA有自己的版本。