zoukankan      html  css  js  c++  java
  • TransactionSynchronizationManager TransactionSynchronizationAdapter

    public class TransactionSynchronizer {
        private final static Logger logger = LoggerFactory.getLogger(TransactionSynchronizer.class);
    
        public static void afterCommit(final Runnable runnable) {
            if (TransactionSynchronizationManager.isActualTransactionActive()) {
                TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                    @Override
                    public void afterCommit() {
                        runnable.run();
                    }
                });
            } else {
                //directly call
                runnable.run();
            }
        }
    
        @SuppressWarnings("unchecked")
        public static <T> Future<T> afterCommit(final Callable<T> callable) {
            final TransactionFuture<T> future = new TransactionFuture<>();
            if (TransactionSynchronizationManager.isActualTransactionActive()) {
                TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                    @Override
                    public void afterCommit() {
                        try {
                            future.put(callable.call());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            } else {
                //directly call
                try {
                    future.put(callable.call());
                } catch (Exception e) {
                    future.cancel(true);
                }
            }
            return future;
        }
    
        /**
         * after commit or rollback
         */
        public static void afterCompletion(final Runnable runnable) {
            if (TransactionSynchronizationManager.isActualTransactionActive()) {
                TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                    @Override
                    public void afterCompletion(int status) {
                        runnable.run();
                    }
                });
            } else {
                //directly call
                runnable.run();
            }
        }
    
        @SuppressWarnings("unchecked")
        public static <T> Future<T> afterCompletion(final Callable<T> callable) {
            final TransactionFuture<T> future = new TransactionFuture<>();
            if (TransactionSynchronizationManager.isActualTransactionActive()) {
                TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                    @Override
                    public void afterCompletion(int status) {
                        try {
                            future.put(callable.call());
                        } catch (Exception e) {
                            logger.error("error calling callable.", e);
                            future.cancel(true);
                        }
                    }
                });
            } else {
                //directly call
                try {
                    future.put(callable.call());
                } catch (Exception e) {
                    logger.error("error calling callable.", e);
                    future.cancel(true);
                }
            }
            return future;
        }
    }
  • 相关阅读:
    Eclipse 中生成帮助文档 (javadoc) 迎客
    网管利器:七大免费网络工具 迎客
    oracle 11g 学习笔记 10_27
    oracle 11g 学习笔记 10_29
    oracle 11g学习笔记 2012_10_22
    oracle 11g 学习笔记 2012_10_25(2)
    oracle 11g 学习笔记 10_26
    oracle 11g 学习笔记 2012_10_24(1)
    oracle 11g 学习笔记2012_10_23(2)
    oracle 11g 学习笔记 2012_10_25(a)
  • 原文地址:https://www.cnblogs.com/zhonghan/p/3842499.html
Copyright © 2011-2022 走看看