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