我的项目正在使用Spring MVC4,Hibernate 5.我已经使用Advice Interceptor配置了hibernate事务,但它并没有按照我的意愿回滚。请帮帮我,我的配置有什么问题?
我的所有代码如下: 的 1。 Hibernate配置:
@Configuration
@EnableTransactionManagement
public class DataSourceConfiguration {
@Autowired
private Environment env;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(
new String[] {env.getProperty("spring.hibernate.packagesToScan")});
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));
return dataSource;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
Spring Advice Interceptor:
@Aspect @组态 公共类TxAdviceInterceptor {
private static final String TX_METHOD_NAME = "*";
@Value(value = "${tx-advice.timeout:-1}")
private Integer txMethodTimeout = -1;
private static final String AOP_POINTCUT_EXPRESSION =
"execution(* com.ptg.service..*.*(..))";
@Autowired
private PlatformTransactionManager transactionManager;
@Bean
public TransactionInterceptor txAdvice() {
MatchAlwaysTransactionAttributeSource source = new MatchAlwaysTransactionAttributeSource();
RuleBasedTransactionAttribute transactionAttribute = new RuleBasedTransactionAttribute();
transactionAttribute.setName(TX_METHOD_NAME);
transactionAttribute.setRollbackRules(
Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
transactionAttribute.setTimeout(txMethodTimeout);
source.setTransactionAttribute(transactionAttribute);
return new TransactionInterceptor(transactionManager, source);
}
@Bean
public Advisor txAdviceAdvisor() {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
return new DefaultPointcutAdvisor(pointcut, txAdvice());
}
}
DAO:
@Repository 公共抽象类GenericDaoImpl {
@Autowired
private SessionFactory sessionFactory;
@Override
public void S save(S entity) {
sessionFactory.save(entity);
}
}
DaoImpl:
@Repository
public class TagDaoImpl extends GenericDaoImpl{
}
@Repository
public class PostDaoImpl extends GenericDaoImpl{
}
服务:
@服务 public class PostServiceImpl {
@Autowired
private PostDao postDao;
@Autowired
private TagDao tagDao;
public void merge(Post post){
tagDao.save();
postDao.save();
}
}
如上面的代码,我想如果postDao.save错误,tagDao也会回滚。
答案 0 :(得分:1)
我发现了这个问题。我的配置没错。 问题是" 默认情况下,仅回滚未经检查的异常(即java.lang.RuntimeException的子类)。对于这种情况,抛出已检查的异常,将提交事务"。 我用NullPointerException错误测试了我的代码,因此不回滚Transaction。
参考:https://www.catalysts.cc/wissenswertes/spring-transactional-rollback-on-checked-exceptions/
答案 1 :(得分:0)
你试过@Transactional注释吗?