spring 监听方法事务提交后再执行某些动作

本文介绍了一个名为`TransactionAfterCommitExecutor`的类,它继承自`ThreadPoolExecutor`,用于在Spring事务成功提交后执行特定任务。同时提及了`TransactionSupport`类,可能与事务管理相关。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class TransactionAfterCommitExecutor extends ThreadPoolExecutor {

public TransactionAfterCommitExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
    super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}

public TransactionAfterCommitExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
    super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
}

public TransactionAfterCommitExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
    super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
}

public TransactionAfterCommitExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, Blo
### Spring 框架中事务事件的使用与配置 #### 配置事务管理器 为了使应用程序能够利用Spring框架提供的事务支持,首先需要定义一个`PlatformTransactionManager`类型的Bean。这可以通过XML配置文件或基于注解的方式完成。 当采用Java Config方式时,通常会创建如下所示的一个类: ```java @Configuration @EnableTransactionManagement public class TransactionConfig { @Autowired private DataSource dataSource; @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource); } } ``` 此段代码展示了如何声明一个名为`transactionManager()`的方法返回一个新的`DataSourceTransactionManager`实例[^2]。 #### 启用事务管理功能 要让Spring自动管理事务边界,在配置类上加上`@EnableTransactionManagement`注解即可启用全局事务管理特性。这样做的好处是可以方便地在服务层方法上添加`@Transactional`注解来指定哪些操作应该参与同一个事务上下文中执行。 #### 定义事务传播行为和隔离级别 除了基本的开启/关闭外,还可以自定义更多细节选项比如传播行为(propagation behavior)以及隔离等级(isolation level),这些都可以作为参数传递给`@Transactional`注解: ```java @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false, timeout = 30, rollbackFor = Exception.class ) public void performServiceOperation(){ // Service logic here... } ``` 上述例子设置了几个重要的属性:传播模式设为REQUIRED意味着如果有现有事务则加入其中;如果没有,则启动新的事务。设置只读标志位false表示允许修改数据,默认超时时长设定为30秒,并指定了任何异常发生都将触发回滚动作[^1]。 #### 处理特定条件下才发生的回滚情况 有时可能希望仅针对某些特定类型的未检查异常(unchecked exceptions)引发回滚而不是全部都这样做。这时可以在`@Transactional`标记里明确指出那些应当引起回滚的具体异常类别列表,例如: ```java @Transactional(rollbackFor={IllegalArgumentException.class}) public void someBusinessMethod() throws IllegalArgumentException{ ... } ``` 这里表明只有抛出了`IllegalArgumentException`才会导致当前事务被撤销[^5]。 #### 监听事务生命周期内的变化 对于想要跟踪记录每次成功提交或者失败后的状态改变的应用来说,可以考虑注册监听者去捕获来自容器发出的通知消息。具体做法是在项目里面引入相应的依赖项之后编写实现接口`ApplicationListener<TransactionalEvent>`的对象并将其交给Spring IOC容器托管起来等待接收广播过来的消息体。 下面给出了一种简单的实现方案: ```java @Component public class MyTransactionEventListener implements ApplicationListener<AfterCommitEvent> { @Override public void onApplicationEvent(AfterCommitEvent event){ System.out.println("Received after commit notification."); } } // 或者也可以选择继承AbstractTransactionLifecycleEventListener抽象基底类重载相应钩子函数 @Component public static final class CustomTxnLifeCycleListener extends AbstractTransactionLifecycleEventListener { protected void doAfterCommit(TransactionPhase phase, DefaultTransactionStatus status){ super.doAfterCommit(phase,status ); log.info("{} has been committed.",status.getTransaction().getName()); } protected void doRollback(DefaultTransactionStatus status){ super.doRollback(status); log.warn("{} was rolled back due to an error condition.",status.getTransaction().getName()); } } ``` 这段程序片段说明了两种不同的途径用来订阅感兴趣的主题——一种是直接响应具体的事件类型(`AfterCommitEvent`),另一种则是通过覆盖父级成员函数间接达到相同目的[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值