001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.transaction.support;
018:
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: /**
026: * Utility methods for triggering specific {@link TransactionSynchronization}
027: * callback methods on all currently registered synchronizations.
028: *
029: * @author Juergen Hoeller
030: * @since 2.0
031: * @see TransactionSynchronization
032: * @see TransactionSynchronizationManager#getSynchronizations()
033: */
034: public abstract class TransactionSynchronizationUtils {
035:
036: private static final Log logger = LogFactory
037: .getLog(TransactionSynchronizationUtils.class);
038:
039: /**
040: * Trigger <code>beforeCommit</code> callbacks on all currently registered synchronizations.
041: * @param readOnly whether the transaction is defined as read-only transaction
042: * @throws RuntimeException if thrown by a <code>beforeCommit</code> callback
043: * @see TransactionSynchronization#beforeCommit(boolean)
044: */
045: public static void triggerBeforeCommit(boolean readOnly) {
046: for (Iterator it = TransactionSynchronizationManager
047: .getSynchronizations().iterator(); it.hasNext();) {
048: TransactionSynchronization synchronization = (TransactionSynchronization) it
049: .next();
050: synchronization.beforeCommit(readOnly);
051: }
052: }
053:
054: /**
055: * Trigger <code>beforeCompletion</code> callbacks on all currently registered synchronizations.
056: * @see TransactionSynchronization#beforeCompletion()
057: */
058: public static void triggerBeforeCompletion() {
059: for (Iterator it = TransactionSynchronizationManager
060: .getSynchronizations().iterator(); it.hasNext();) {
061: TransactionSynchronization synchronization = (TransactionSynchronization) it
062: .next();
063: try {
064: synchronization.beforeCompletion();
065: } catch (Throwable tsex) {
066: logger
067: .error(
068: "TransactionSynchronization.beforeCompletion threw exception",
069: tsex);
070: }
071: }
072: }
073:
074: /**
075: * Trigger <code>afterCommit</code> callbacks on all currently registered synchronizations.
076: * @throws RuntimeException if thrown by a <code>afterCommit</code> callback
077: * @see TransactionSynchronizationManager#getSynchronizations()
078: * @see TransactionSynchronization#afterCommit()
079: */
080: public static void triggerAfterCommit() {
081: List synchronizations = TransactionSynchronizationManager
082: .getSynchronizations();
083: invokeAfterCommit(synchronizations);
084: }
085:
086: /**
087: * Actually invoke the <code>afterCommit</code> methods of the
088: * given Spring TransactionSynchronization objects.
089: * @param synchronizations List of TransactionSynchronization objects
090: * @see TransactionSynchronization#afterCommit()
091: */
092: public static void invokeAfterCommit(List synchronizations) {
093: if (synchronizations != null) {
094: for (Iterator it = synchronizations.iterator(); it
095: .hasNext();) {
096: TransactionSynchronization synchronization = (TransactionSynchronization) it
097: .next();
098: try {
099: synchronization.afterCommit();
100: } catch (AbstractMethodError tserr) {
101: if (logger.isDebugEnabled()) {
102: logger
103: .debug(
104: "Spring 2.0's TransactionSynchronization.afterCommit method not implemented in "
105: + "synchronization class ["
106: + synchronization
107: .getClass()
108: .getName()
109: + "]", tserr);
110: }
111: }
112: }
113: }
114: }
115:
116: /**
117: * Trigger <code>afterCompletion</code> callbacks on all currently registered synchronizations.
118: * @see TransactionSynchronizationManager#getSynchronizations()
119: * @param completionStatus the completion status according to the
120: * constants in the TransactionSynchronization interface
121: * @see TransactionSynchronization#afterCompletion(int)
122: * @see TransactionSynchronization#STATUS_COMMITTED
123: * @see TransactionSynchronization#STATUS_ROLLED_BACK
124: * @see TransactionSynchronization#STATUS_UNKNOWN
125: */
126: public static void triggerAfterCompletion(int completionStatus) {
127: List synchronizations = TransactionSynchronizationManager
128: .getSynchronizations();
129: invokeAfterCompletion(synchronizations, completionStatus);
130: }
131:
132: /**
133: * Actually invoke the <code>afterCompletion</code> methods of the
134: * given Spring TransactionSynchronization objects.
135: * @param synchronizations List of TransactionSynchronization objects
136: * @param completionStatus the completion status according to the
137: * constants in the TransactionSynchronization interface
138: * @see TransactionSynchronization#afterCompletion(int)
139: * @see TransactionSynchronization#STATUS_COMMITTED
140: * @see TransactionSynchronization#STATUS_ROLLED_BACK
141: * @see TransactionSynchronization#STATUS_UNKNOWN
142: */
143: public static void invokeAfterCompletion(List synchronizations,
144: int completionStatus) {
145: if (synchronizations != null) {
146: for (Iterator it = synchronizations.iterator(); it
147: .hasNext();) {
148: TransactionSynchronization synchronization = (TransactionSynchronization) it
149: .next();
150: try {
151: synchronization.afterCompletion(completionStatus);
152: } catch (Throwable tsex) {
153: logger
154: .error(
155: "TransactionSynchronization.afterCompletion threw exception",
156: tsex);
157: }
158: }
159: }
160: }
161:
162: }
|