KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > transaction > support > TransactionSynchronizationUtils


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.transaction.support;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 /**
26  * Utility methods for triggering specific {@link TransactionSynchronization}
27  * callback methods on all currently registered synchronizations.
28  *
29  * @author Juergen Hoeller
30  * @since 2.0
31  * @see TransactionSynchronization
32  * @see TransactionSynchronizationManager#getSynchronizations()
33  */

34 public abstract class TransactionSynchronizationUtils {
35
36     private static final Log logger = LogFactory.getLog(TransactionSynchronizationUtils.class);
37
38
39     /**
40      * Trigger <code>beforeCommit</code> callbacks on all currently registered synchronizations.
41      * @param readOnly whether the transaction is defined as read-only transaction
42      * @throws RuntimeException if thrown by a <code>beforeCommit</code> callback
43      * @see TransactionSynchronization#beforeCommit(boolean)
44      */

45     public static void triggerBeforeCommit(boolean readOnly) {
46         for (Iterator JavaDoc it = TransactionSynchronizationManager.getSynchronizations().iterator(); it.hasNext();) {
47             TransactionSynchronization synchronization = (TransactionSynchronization) it.next();
48             synchronization.beforeCommit(readOnly);
49         }
50     }
51
52     /**
53      * Trigger <code>beforeCompletion</code> callbacks on all currently registered synchronizations.
54      * @see TransactionSynchronization#beforeCompletion()
55      */

56     public static void triggerBeforeCompletion() {
57         for (Iterator JavaDoc it = TransactionSynchronizationManager.getSynchronizations().iterator(); it.hasNext();) {
58             TransactionSynchronization synchronization = (TransactionSynchronization) it.next();
59             try {
60                 synchronization.beforeCompletion();
61             }
62             catch (Throwable JavaDoc tsex) {
63                 logger.error("TransactionSynchronization.beforeCompletion threw exception", tsex);
64             }
65         }
66     }
67
68     /**
69      * Trigger <code>afterCommit</code> callbacks on all currently registered synchronizations.
70      * @throws RuntimeException if thrown by a <code>afterCommit</code> callback
71      * @see TransactionSynchronizationManager#getSynchronizations()
72      * @see TransactionSynchronization#afterCommit()
73      */

74     public static void triggerAfterCommit() {
75         List JavaDoc synchronizations = TransactionSynchronizationManager.getSynchronizations();
76         invokeAfterCommit(synchronizations);
77     }
78
79     /**
80      * Actually invoke the <code>afterCommit</code> methods of the
81      * given Spring TransactionSynchronization objects.
82      * @param synchronizations List of TransactionSynchronization objects
83      * @see TransactionSynchronization#afterCommit()
84      */

85     public static void invokeAfterCommit(List JavaDoc synchronizations) {
86         if (synchronizations != null) {
87             for (Iterator JavaDoc it = synchronizations.iterator(); it.hasNext();) {
88                 TransactionSynchronization synchronization = (TransactionSynchronization) it.next();
89                 try {
90                     synchronization.afterCommit();
91                 }
92                 catch (AbstractMethodError JavaDoc tserr) {
93                     if (logger.isDebugEnabled()) {
94                         logger.debug("Spring 2.0's TransactionSynchronization.afterCommit method not implemented in " +
95                                 "synchronization class [" + synchronization.getClass().getName() + "]", tserr);
96                     }
97                 }
98             }
99         }
100     }
101
102     /**
103      * Trigger <code>afterCompletion</code> callbacks on all currently registered synchronizations.
104      * @see TransactionSynchronizationManager#getSynchronizations()
105      * @param completionStatus the completion status according to the
106      * constants in the TransactionSynchronization interface
107      * @see TransactionSynchronization#afterCompletion(int)
108      * @see TransactionSynchronization#STATUS_COMMITTED
109      * @see TransactionSynchronization#STATUS_ROLLED_BACK
110      * @see TransactionSynchronization#STATUS_UNKNOWN
111      */

112     public static void triggerAfterCompletion(int completionStatus) {
113         List JavaDoc synchronizations = TransactionSynchronizationManager.getSynchronizations();
114         invokeAfterCompletion(synchronizations, completionStatus);
115     }
116
117     /**
118      * Actually invoke the <code>afterCompletion</code> methods of the
119      * given Spring TransactionSynchronization objects.
120      * @param synchronizations List of TransactionSynchronization objects
121      * @param completionStatus the completion status according to the
122      * constants in the TransactionSynchronization interface
123      * @see TransactionSynchronization#afterCompletion(int)
124      * @see TransactionSynchronization#STATUS_COMMITTED
125      * @see TransactionSynchronization#STATUS_ROLLED_BACK
126      * @see TransactionSynchronization#STATUS_UNKNOWN
127      */

128     public static void invokeAfterCompletion(List JavaDoc synchronizations, int completionStatus) {
129         if (synchronizations != null) {
130             for (Iterator JavaDoc it = synchronizations.iterator(); it.hasNext();) {
131                 TransactionSynchronization synchronization = (TransactionSynchronization) it.next();
132                 try {
133                     synchronization.afterCompletion(completionStatus);
134                 }
135                 catch (Throwable JavaDoc tsex) {
136                     logger.error("TransactionSynchronization.afterCompletion threw exception", tsex);
137                 }
138             }
139         }
140     }
141
142 }
143
Popular Tags