[英]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.