Java Reference
In-Depth Information
Now, if you run your application again, the book stock will not be deducted when the user's balance
is insufficient to purchase the topic.
Although you can manage transactions by explicitly committing and rolling back JDBC connections,
the code required for this purpose is boilerplate code that you have to repeat for different methods.
Moreover, this code is JDBC specific, so once you have chosen another data access technology, it needs
to be changed also. Spring's transaction support offers a set of technology-independent facilities,
including transaction managers (e.g., org.springframework.transaction.PlatformTransactionManager ),
a transaction template (e.g., org.springframework.transaction.support.TransactionTemplate ), and
transaction declaration support to simplify your transaction management tasks.
4-2. Choosing a Transaction Manager Implementation
Problem
Typically, if your application involves only a single data source, you can simply manage transactions by
calling the commit() and rollback() methods on a database connection. However, if your transactions
extend across multiple data sources or you prefer to make use of the transaction management
capabilities provided by your Java EE application server, you may choose the Java Transaction API (JTA).
Besides, you may have to call different proprietary transaction APIs for different object/relational
mapping frameworks such as Hibernate and JPA.
As a result, you have to deal with different transaction APIs for different technologies. It would be
hard for you to switch from one set of APIs to another.
Solution
Spring abstracts a general set of transaction facilities from different transaction management APIs. As an
application developer, you can simply utilize Spring's transaction facilities without having to know
much about the underlying transaction APIs. With these facilities, your transaction management code
will be independent of any specific transaction technology.
Spring's core transaction management abstraction is PlatformTransactionManager . It encapsulates a
set of technology-independent methods for transaction management. Remember that a transaction
manager is needed no matter which transaction management strategy (programmatic or declarative)
you choose in Spring. The PlatformTransactionManager interface provides three methods for working
with transactions:
TransactionStatus getTransaction(TransactionDefinition definition) throws
TransactionException
void commit(TransactionStatus status) throws TransactionException;
void rollback(TransactionStatus status) throws TransactionException;
 
Search WWH ::




Custom Search