spring回滚事务

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
">

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"/>
    </bean>  
    
<tx:advice id="dataAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="select*" propagation="SUPPORTS" />
            <tx:method name="query*" propagation="SUPPORTS" />
            <tx:method name="get*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="dataPointcut" expression="execution(* com.sf.sgs.user.manager.*.impl.*.*(..))" />
<aop:advisor pointcut-ref="dataPointcut" advice-ref="dataAdvice" />
</aop:config>

</beans>

Spring框架提供了一种灵活的方式来管理数据库事务,包括手动控制事务回滚。如果你需要在特定条件下手动触发事务回滚,可以按照以下步骤操作: 1. **声明式事务**:默认情况下,Spring会基于@Transactional注解进行声明式事务管理,如果在运行时遇到异常,它会自动回滚事务。如果你想手动干预,可以在catch块中使用`PlatformTransactionManager`的`rollback()`方法。 ```java @Autowired private PlatformTransactionManager transactionManager; try { // 执行业务操作 YourService yourService = new YourServiceImpl(); yourService.doSomething(); } catch (Exception e) { if (shouldRollback) { // 自定义条件判断 transactionManager.rollback(); } throw e; // 或者记录日志后再次抛出异常 } ``` 2. **编程式事务**:如果你更倾向于在代码层面控制事务,可以创建`JdbcTemplate`, `HibernateTemplate`等具体的模板,并显式开启和管理事务。 ```java TransactionDefinition def = new DefaultTransactionDefinition(); TransactionStatus status = transactionManager.getTransaction(def); try { // 开始事务 jdbcTemplate.update("your sql statement"); // 如果满足某个条件,如检查错误码 if (errorCondition) { transactionManager.rollback(status); // 回滚事务 } else { transactionManager.commit(status); // 提交事务 } } finally { transactionManager.rollbackIfNecessary(status); // 确保事务结束 } ``` 记得在finally块中处理事务的最终状态,确保资源得到正确的清理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值