手工调用Transaction

除了用 @Transactional 外还可以手工调用

PlatformTransactionManager transactionManager = (PlatformTransactionManager)SpringBeanUtil.getBean(PlatformTransactionManager.class);
        DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
        transactionDefinition.setPropagationBehavior(Propagation.REQUIRED.value());
        TransactionStatus transaction = null;

        Object result;
        try {
            transaction = transactionManager.getTransaction(transactionDefinition);
           // 业务代码
            transactionManager.commit(transaction);
            
        } catch (Exception ex) {
            if (null != transaction && !transaction.isCompleted()) {
                transactionManager.rollback(transaction);
            }

            throw ex;
        } finally {
            if (null != transaction && !transaction.isCompleted()) {
                transactionManager.rollback(transaction);
            }

        }

 

posted @ 2023-12-26 14:04  zslm___  阅读(11)  评论(0编辑  收藏  举报