Java Code Examples for org.springframework.transaction.support.TransactionSynchronizationAdapter

Following code examples demonstrate how to use org.springframework.transaction.support.TransactionSynchronizationAdapterfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.transaction.support.TransactionSynchronizationAdapterand various code implementation of this class.

	public void test() {
		try {
			System.out.println("service内部事务,等待5秒");
			Thread.sleep(5000L);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
			@Override
			public void afterCommit() {
				Thread thread = Thread.currentThread();
				System.out.println("transfer after transaction commit...");
			}
		});
	} 


    public static void afterCommit(TransactionSynchronizationAdapter adapter) {
        if (TransactionSynchronizationManager.isActualTransactionActive()) {
            TransactionSynchronizationManager.registerSynchronization(adapter);
        } else {
            adapter.afterCommit();
        }
    } 

    }

    public void registerSynchronization(
        final Synchronization synchronization) {
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                public void afterCompletion(int state) {
                    System.out.println(
                        " ---------------------------------------- ");
                    System.out.println(synchronization);
                    System.out.println(
                        " ---------------------------------------- ");
                    synchronization.afterCompletion(state);
                }

                public void beforeCompletion() {
                    synchronization.beforeCompletion();
                }
            });
    } 

    public void onSuccess(Runnable runnable) {
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                public void afterCompletion(int status) {
                    if (status == TransactionSynchronization.STATUS_COMMITTED) {
                        runnable.run();
                    }
                }

            });
        }
    } 

Advertisement
Javadoc
Simple TransactionSynchronization adapter containing empty method implementations, for easier overri

ding of single methods.

Also implements the Ordered interface to enable the execution order of synchronizations to be controlled declaratively. The default #getOrder() order is Ordered#LOWEST_PRECEDENCE, indicating late execution; return a lower value for earlier execution. @author Juergen Hoeller @since 22.01.2004

Read More
Advertisement