In the previous article in this series, we talked about the use of Transactionmanger and Connectionholder to complete thread-safe transaction management, in this article we will introduce template mode for transaction management.
Template mode everyone should be familiar with, such as spring provides a lot of template, like JdbcTemplate and jmstemplate and so on. The basic idea of template mode is to declare the method of core function as abstract method in superclass, leave it to subclass, and accomplish some common operations in superclass, such as the establishment of JMS session and the preparation of database transaction.
In this article, we use a template class to help manage transactions, and define the Transactiontemplate class as follows:
Package Davenkin.step4_transaction_template;import Davenkin.step3_connection_holder. Transactionmanager;import Javax.sql.datasource;public abstract class transactiontemplate{ private TransactionManager TransactionManager; Protected transactiontemplate (DataSource DataSource) { TransactionManager = new TransactionManager ( DataSource); } public void Dojobintransaction () { try { transactionmanager.start (); Dojob (); Transactionmanager.commit (); } catch (Exception e) { transactionmanager.rollback (); } finally { Transactionmanager.close (); } } protected abstract void Dojob () throws Exception;}
Define a Dojobintransaction method in the Transactiontemplate class that begins the transaction first using TransactionManager, and then calls the Dojob method to complete the business function, the Dojob method is the abstract method, Subclasses that complete the business function should implement the method, and finally, whether the commit transaction or the rollback transaction is successfully determined based on the Dojob method.
Then define the Transactiontemplatebankservice that uses transactiontemplate:
Package Davenkin.step4_transaction_template;import Davenkin. Bankservice;import Davenkin.step3_connection_holder. Connectionholderbankdao;import Davenkin.step3_connection_holder. Connectionholderinsurancedao;import Javax.sql.datasource;public class Transactiontemplatebankservice implements bankservice{private DataSource DataSource; Private Connectionholderbankdao Connectionholderbankdao; Private Connectionholderinsurancedao Connectionholderinsurancedao; Public Transactiontemplatebankservice (DataSource DataSource) {this.datasource = DataSource; Connectionholderbankdao = new Connectionholderbankdao (DataSource); Connectionholderinsurancedao = new Connectionholderinsurancedao (DataSource); } public void Transfer (final int fromid, final int toid, final int amount) {New Transactiontemplate (datasour CE) {@Override protected void Dojob () throws Exception {Connectio Nholderbankdao.withdraw (Fromid, amount); Connectionholderinsurancedao.deposit (toid, amount); }}.dojobintransaction (); }}
In Transactiontemplatebankservice's transfer method, we created an anonymous Ttransactiontemplate class and implemented the Dojob method, In the Dojob method, call two DAO to complete the business operation, and then call the Dojobintransaction method that calls Transactiontemplatebankservice.
Since transactiontemplate is just a layer of transaction processing in the previous article, TransactionManager and the two DAO classes remain the same as in the previous article, At this point they all use Singlethreadconnectionholder to obtain connection, so the transaction processing succeeds.
In the next article, we'll talk about using dynamic proxies for Java to complete transactions, which is a typical approach to spring management transactions.
Java Transaction Full Resolution (V)--template mode