码迷,mamicode.com
首页 > 其他好文 > 详细

TransactionSynchronizationManager TransactionSynchronizationAdapter

时间:2014-07-14 22:07:34      阅读:567      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   cti   div   

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

标签:style   blog   color   io   cti   div   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!