Transaction processing is one of the most important problems to be solved in enterprise application. Java EE provides complete transaction management capabilities through JTA, including the ability to manage multiple transactional resources. But most applications run on a single transactional resource (a database) that does not require a global transaction service. Local transaction services are already sufficient (for example, JDBC Transaction management).
The main purpose of this article is to discuss how to design transactional services more elegantly. Take JDBC transactions only as an example. Refer to the Dao,factory,proxy,decorator and other model concepts, please read the relevant information.
Perhaps you have heard that transaction processing should be done at the service level, and maybe you are doing it, but do you know why? Why not put a transaction in the DAO layer. The obvious reason is that every method of the business layer interface is sometimes a business use case (User cases), and it needs to invoke a different DAO object to complete a business method. For example, simply to purchase Books Online bookstore For example, the first business method is to call the Bookdao object (usually produced through the DAO Factory), Bookdao judge whether there is still inventory margin, to obtain the price information of the book, Then call Customerdao to deduct the corresponding fee from the account and record the information, then other services (notify the Administrator, etc.). Simplifying business processes is probably the case:
Note that our example ignores the handling of the connection, as long as it is guaranteed that the same connection is taken within the same thread (available threadlocal implementation):
The first is the business interface, which is intended for the interface, not for class programming:
public interface BookStoreManager{
public boolean buyBook(String bookId,int quantity)throws SystemException;
....其他业务方法
}
Next is the implementation class for the business interface?? Business object:
public class Bookstoremanagerimpl implements bookstoremanager{
Public boolean buybook (String bookid) thro WS systemexception{
Connection conn=connectionmanager.getconnection ();//Get Database connection
Boolean b=false;
try{
Conn.setautocommit (FALSE);//Cancel autocommit
Bookdao Bookdao=daofactory.getbookdao ();
Customerdao Customerdao=daofactory.getcustomerdao ();
//Attempt to fetch the book from inventory
if (Bookdao.reduceinventory (conn,bookid,quantity)) {
BigDecimal pri Ce=bookdao.getprice (BookID); Take the price
//deduct the price*quantity fee from the customer account
b=
Customerdao.reduceaccount (conn,price. Multiply (new BigDecimal (quantity));
.....
Other business methods, such as notifying the administrator, generating orders, and so on.
...
Conn.commit (); Commit TRANSACTION
Conn.setautocommit (true);
}catch (SQLException e) {
Conn.rollback (); Exception occurred, ROLLBACK TRANSACTION
Con.setautocommit (TRUE);
E.printstacktrace ();
throws New SystemException (e);
}
return B;
}
}
Then the business Representative factory:
public final class ManagerFactory {
public static BookStoreManager getBookStoreManager() {
return new BookStoreManagerImpl();
}
}