我有一个方法可以向表中添加项目并测试异常 我的项目存储在表格中。提出异常,但事务却没有 回滚 我的方法
@Transactional(propagation = Propagation.REQUIRED, readOnly = false,
rollbackFor={java.lang.Exception.class, java.lang.RuntimeException.class})
public MyBean addItem(MyBean bean, Integer iduser) throws Exception {
NalogBean nb = txTemplate.execute(new TransactionCallback<MyBean>() {
@Override
public MyBean doInTransaction(TransactionStatus txStatus) {
MyBean nbl = null;
try {
sessionFactory.getCurrentSession().saveOrUpdate(bean);
throw new RuntimeException("Exception throwed!");
} catch (Exception e) {
txStatus.setRollbackOnly();
}
return nbl;
}
});
return nb;
}
我的servlet-context是标准的
<tx:annotation-driven transaction-manager="transactionManager"/>
<beans:bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="${database.driver}" />
<beans:property name="url" value="${database.url}" />
<beans:property name="username" value="${database.user}" />
<beans:property name="password" value="${database.password}" />
</beans:bean>
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource"></beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
</beans:props>
</beans:property>
<beans:property name="packagesToScan" value="rs.co"></beans:property>
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>rs.co.mfp.klijent.finansije.model.NalogModel</beans:value>
</beans:list>
</beans:property>
</beans:bean>
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory"/>
</beans:bean>
当我调用addItem时,它将bean存储到表中。 我也尝试没有txTemplate 。sessionFactory.getCurrentSession()saveOrUpdate(豆); 抛出新的RuntimeException(&#34; Exception throwed!&#34;); 但结果相同。项目存储在表格中。 有什么想法吗?