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;
018:
019: /**
020: * This is the central interface in Spring's transaction infrastructure.
021: * Applications can use this directly, but it is not primarily meant as API:
022: * Typically, applications will work with either TransactionTemplate or
023: * declarative transaction demarcation through AOP.
024: *
025: * <p>For implementors, it is recommended to derive from the provided
026: * {@link org.springframework.transaction.support.AbstractPlatformTransactionManager}
027: * class, which pre-implements the defined propagation behavior and takes care
028: * of transaction synchronization handling. Subclasses have to implement
029: * template methods for specific states of the underlying transaction,
030: * for example: begin, suspend, resume, commit.
031: *
032: * <p>The default implementations of this strategy interface are
033: * {@link org.springframework.transaction.jta.JtaTransactionManager} and
034: * {@link org.springframework.jdbc.datasource.DataSourceTransactionManager},
035: * which can serve as an implementation guide for other transaction strategies.
036: *
037: * @author Rod Johnson
038: * @author Juergen Hoeller
039: * @since 16.05.2003
040: * @see org.springframework.transaction.support.TransactionTemplate
041: * @see org.springframework.transaction.interceptor.TransactionInterceptor
042: * @see org.springframework.transaction.interceptor.TransactionProxyFactoryBean
043: */
044: public interface PlatformTransactionManager {
045:
046: /**
047: * Return a currently active transaction or create a new one, according to
048: * the specified propagation behavior.
049: * <p>Note that parameters like isolation level or timeout will only be applied
050: * to new transactions, and thus be ignored when participating in active ones.
051: * <p>Furthermore, not all transaction definition settings will be supported
052: * by every transaction manager: A proper transaction manager implementation
053: * should thrown an exception when unsupported settings are encountered.
054: * <p>An exception to the above rule is the read-only flag, which should be
055: * ignored if no explicit read-only mode is supported. Essentially, the
056: * read-only flag is just a hint for potential optimization.
057: * @param definition TransactionDefinition instance (can be <code>null</code> for defaults),
058: * describing propagation behavior, isolation level, timeout etc.
059: * @return transaction status object representing the new or current transaction
060: * @throws TransactionException in case of lookup, creation, or system errors
061: * @throws IllegalTransactionStateException if the given transaction definition
062: * cannot be executed (for example, if a currently active transaction is in
063: * conflict with the specified propagation behavior)
064: * @see TransactionDefinition#getPropagationBehavior
065: * @see TransactionDefinition#getIsolationLevel
066: * @see TransactionDefinition#getTimeout
067: * @see TransactionDefinition#isReadOnly
068: */
069: TransactionStatus getTransaction(TransactionDefinition definition)
070: throws TransactionException;
071:
072: /**
073: * Commit the given transaction, with regard to its status. If the transaction
074: * has been marked rollback-only programmatically, perform a rollback.
075: * <p>If the transaction wasn't a new one, omit the commit for proper
076: * participation in the surrounding transaction. If a previous transaction
077: * has been suspended to be able to create a new one, resume the previous
078: * transaction after committing the new one.
079: * <p>Note that when the commit call completes, no matter if normally or
080: * throwing an exception, the transaction must be fully completed and
081: * cleaned up. No rollback call should be expected in such a case.
082: * <p>If this method throws an exception other than a TransactionException,
083: * then some before-commit error caused the commit attempt to fail. For
084: * example, an O/R Mapping tool might have tried to flush changes to the
085: * database right before commit, with the resulting DataAccessException
086: * causing the transaction to fail. The original exception will be
087: * propagated to the caller of this commit method in such a case.
088: * @param status object returned by the <code>getTransaction</code> method
089: * @throws UnexpectedRollbackException in case of an unexpected rollback
090: * that the transaction coordinator initiated
091: * @throws HeuristicCompletionException in case of a transaction failure
092: * caused by a heuristic decision on the side of the transaction coordinator
093: * @throws TransactionSystemException in case of commit or system errors
094: * (typically caused by fundamental resource failures)
095: * @throws IllegalTransactionStateException if the given transaction
096: * is already completed (that is, committed or rolled back)
097: * @see TransactionStatus#setRollbackOnly
098: */
099: void commit(TransactionStatus status) throws TransactionException;
100:
101: /**
102: * Perform a rollback of the given transaction.
103: * <p>If the transaction wasn't a new one, just set it rollback-only for proper
104: * participation in the surrounding transaction. If a previous transaction
105: * has been suspended to be able to create a new one, resume the previous
106: * transaction after rolling back the new one.
107: * <p><b>Do not call rollback on a transaction if commit threw an exception.</b>
108: * The transaction will already have been completed and cleaned up when commit
109: * returns, even in case of a commit exception. Consequently, a rollback call
110: * after commit failure will lead to an IllegalTransactionStateException.
111: * @param status object returned by the <code>getTransaction</code> method
112: * @throws TransactionSystemException in case of rollback or system errors
113: * (typically caused by fundamental resource failures)
114: * @throws IllegalTransactionStateException if the given transaction
115: * is already completed (that is, committed or rolled back)
116: */
117: void rollback(TransactionStatus status) throws TransactionException;
118:
119: }
|