首页 > 其他 > 详细

TransactionSynchronizationManager TransactionSynchronizationAdapter

时间:2014-07-14 22:07:55      阅读:965      评论:0      收藏:0      [点我收藏+]
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;
    }
}

 

TransactionSynchronizationManager TransactionSynchronizationAdapter,布布扣,bubuko.com

TransactionSynchronizationManager TransactionSynchronizationAdapter

原文:http://www.cnblogs.com/zhonghan/p/3842499.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!