001: /*
002: * Copyright 2002-2007 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: /**
020: * Interface for transaction synchronization callbacks.
021: * Supported by AbstractPlatformTransactionManager.
022: *
023: * <p>TransactionSynchronization implementations can implement the Ordered interface
024: * to influence their execution order. A synchronization that does not implement the
025: * Ordered interface is appended to the end of the synchronization chain.
026: *
027: * <p>System synchronizations performed by Spring itself use specific order values,
028: * allowing for fine-grained interaction with their execution order (if necessary).
029: *
030: * @author Juergen Hoeller
031: * @since 02.06.2003
032: * @see TransactionSynchronizationManager
033: * @see AbstractPlatformTransactionManager
034: * @see org.springframework.jdbc.datasource.DataSourceUtils#CONNECTION_SYNCHRONIZATION_ORDER
035: * @see org.springframework.orm.hibernate3.SessionFactoryUtils#SESSION_SYNCHRONIZATION_ORDER
036: */
037: public interface TransactionSynchronization {
038:
039: /** Completion status in case of proper commit */
040: int STATUS_COMMITTED = 0;
041:
042: /** Completion status in case of proper rollback */
043: int STATUS_ROLLED_BACK = 1;
044:
045: /** Completion status in case of heuristic mixed completion or system errors */
046: int STATUS_UNKNOWN = 2;
047:
048: /**
049: * Suspend this synchronization.
050: * Supposed to unbind resources from TransactionSynchronizationManager if managing any.
051: * @see TransactionSynchronizationManager#unbindResource
052: */
053: void suspend();
054:
055: /**
056: * Resume this synchronization.
057: * Supposed to rebind resources to TransactionSynchronizationManager if managing any.
058: * @see TransactionSynchronizationManager#bindResource
059: */
060: void resume();
061:
062: /**
063: * Invoked before transaction commit (before "beforeCompletion").
064: * Can e.g. flush transactional O/R Mapping sessions to the database.
065: * <p>This callback does <i>not</i> mean that the transaction will actually be committed.
066: * A rollback decision can still occur after this method has been called. This callback
067: * is rather meant to perform work that's only relevant if a commit still has a chance
068: * to happen, such as flushing SQL statements to the database.
069: * <p>Note that exceptions will get propagated to the commit caller and cause a
070: * rollback of the transaction.
071: * @param readOnly whether the transaction is defined as read-only transaction
072: * @throws RuntimeException in case of errors; will be <b>propagated to the caller</b>
073: * (note: do not throw TransactionException subclasses here!)
074: * @see #beforeCompletion
075: */
076: void beforeCommit(boolean readOnly);
077:
078: /**
079: * Invoked before transaction commit/rollback.
080: * Can perform resource cleanup <i>before</i> transaction completion.
081: * <p>This method will be invoked after <code>beforeCommit</code>, even when
082: * <code>beforeCommit</code> threw an exception. This callback allows for
083: * closing resources before transaction completion, for any outcome.
084: * @throws RuntimeException in case of errors; will be <b>logged but not propagated</b>
085: * (note: do not throw TransactionException subclasses here!)
086: * @see #beforeCommit
087: * @see #afterCompletion
088: */
089: void beforeCompletion();
090:
091: /**
092: * Invoked after transaction commit. Can perform further operations right
093: * <i>after</i> the main transaction has <i>successfully</i> committed.
094: * <p>Can e.g. commit further operations that are supposed to follow on a successful
095: * commit of the main transaction, like confirmation messages or emails.
096: * <p><b>NOTE:</b> The transaction will have been committed already, but the
097: * transactional resources might still be active and accessible. As a consequence,
098: * any data access code triggered at this point will still "participate" in the
099: * original transaction, allowing to perform some cleanup (with no commit following
100: * anymore!), unless it explicitly declares that it needs to run in a separate
101: * transaction. Hence: <b>Use <code>PROPAGATION_REQUIRES_NEW</code> for any
102: * transactional operation that is called from here.</b>
103: * @throws RuntimeException in case of errors; will be <b>propagated to the caller</b>
104: * (note: do not throw TransactionException subclasses here!)
105: */
106: void afterCommit();
107:
108: /**
109: * Invoked after transaction commit/rollback.
110: * Can perform resource cleanup <i>after</i> transaction completion.
111: * <p><b>NOTE:</b> The transaction will have been committed or rolled back already,
112: * but the transactional resources might still be active and accessible. As a
113: * consequence, any data access code triggered at this point will still "participate"
114: * in the original transaction, allowing to perform some cleanup (with no commit
115: * following anymore!), unless it explicitly declares that it needs to run in a
116: * separate transaction. Hence: <b>Use <code>PROPAGATION_REQUIRES_NEW</code>
117: * for any transactional operation that is called from here.</b>
118: * @param status completion status according to the <code>STATUS_*</code> constants
119: * @throws RuntimeException in case of errors; will be <b>logged but not propagated</b>
120: * (note: do not throw TransactionException subclasses here!)
121: * @see #STATUS_COMMITTED
122: * @see #STATUS_ROLLED_BACK
123: * @see #STATUS_UNKNOWN
124: * @see #beforeCompletion
125: */
126: void afterCompletion(int status);
127:
128: }
|