簡體   English   中英

Spring-Hibernate @Transactional服務方法不會自動提交

[英]Spring-Hibernate @Transactional service methods do not automatically commit

@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對象,修改該對象並再次保存。 從應用程序的角度看,數據似乎已保存,當我單擊“全部列出”按鈕檢索所有數據時,可以看到修改后的數據。 但是,關鍵是數據永遠不會在數據庫中更新。 保存的對象被緩存在休眠會話中的某個位置,但從不進入數據庫。 請幫忙。

通過將日志級別設置為“跟蹤”,我發現事務實際上已經提交。 不知道為什么數據庫表不更新。 這是因為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]

我通過將(value =“ springTransactionManager”)添加到@Transactional批注中解決了該問題。 問題是我配置了一個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無法獲得我想要的正確事務。 謝謝。

您可能使用了錯誤的@Transactional批注。 Spring和JPA有自己的版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM