Simple Solution for pure JDBC transaction control in Layered Architecture (continued)

Source: Internet
Author: User

In the previous article, simple solution for pure JDBC transaction control under layered architecture, the transaction control when pure JDBC is used in layer-4 architecture applications is discussed, and a simple solution is provided, I received replies from many netizens. Some netizens should also see that when writing methods at the business layer, the code framework for transaction processing is the same, but the operations at the persistent layer are combined. In this case, I used the [template method] mode to encapsulate it again. Add the following interfaces and classes:

 

1. Transaction callback interface (the callback method has a return value): transactioncallback. Java

Package com. tjitcast. Common;

Import com. tjitcast. Dao. daoexception;

/**
* Transaction callback Interface
* @ Author qiujy
*/
Public interface transactioncallback <t> {
/**
* Method for callback execution in the transaction
* @ Return refers to the data of the specified type.
* @ Throws daoexception Data Access exception
*/
T dointransaction () throws daoexception;
}

 

2. Transaction callback interface (the callback method does not return values): transactioncallbackwithoutresult. Java

Package com. tjitcast. Common;

Import com. tjitcast. Dao. daoexception;

/**
* Transaction callback interface without return values
* @ Author qiujy
*/
Public interface transactioncallbackwithoutresult {
/**
* Method for callback execution in the transaction
* @ Throws daoexception Data Access exception
*/
Public void dointransaction () throws daoexception;
}

 

3. The template class for executing the specified callback interface method in the transaction: transactiontemplate. Java

Package com. tjitcast. Common;

Import com. tjitcast. Dao. daoexception;

/**
* Execution of the template class of the specified callback interface method in a transaction
* @ Author qiujy
*/
Public class transactiontemplate {

/**
* Methods that return values in the implementation class of the callback interface executed in transactions
* @ Param <t> Return Value Type
* @ Param callback Interface
* @ Return refers to the return value of the specified type.
* @ Throws daoexception Data Access exception
*/
Public static <t> T execute (transactioncallback <t> callback) throws daoexception {
T result = NULL;
Transactionmanager Tx = connectionfactory. gettranmanager ();
Try {
TX. begintransaction ();

Result = callback. dointransaction ();

TX. commitandclose ();
} Catch (daoexception e ){
TX. rollbackandclose ();
Throw E;
}
Return result;
}
 
/**
* The callback interface implementation class in a transaction does not return values.
* @ Param callback Interface
* @ Throws daoexception Data Access exception
*/
Public static void execute (transactioncallbackwithoutresult callback) throws daoexception {
Transactionmanager Tx = connectionfactory. gettranmanager ();
Try {
TX. begintransaction ();

Callback. dointransaction ();

TX. commitandclose ();
} Catch (daoexception e ){
TX. rollbackandclose ();
Throw E;
}
}
}

 

 

After the preceding interfaces and classes are added, the servicefacade class code of the business logic layer can be modified to the following format:

 

Package com. tjitcast. Service;

Import java. util. List;

Import com. tjitcast. Common. transactioncallback;
Import com. tjitcast. Common. transactioncallbackwithoutresult;
Import com. tjitcast. Common. transactiontemplate;
Import com. tjitcast. Dao. daoexception;
Import com. tjitcast. Dao. daofactory;
Import com. tjitcast. Dao. deptdao;
Import com. tjitcast. Dao. employeedao;
Import com. tjitcast. entity. Dept;
Import com. tjitcast. entity. employee;
Import com. tjitcast. entity. pagemodel;

/**
* Business layer facade --> Add transaction control
* JDBC transactions are automatically committed by default,
* @ Author qiujy
*/
Public class servicefacade {
Private deptdao = daofactory. getinstance ("deptdao", deptdao. Class );
Private employeedao empdao = daofactory. getinstance ("empdao", employeedao. Class );
 
/**
* Add a department
* @ Param Dept
*/
Public void insertdept (final dept Dept ){
Transactiontemplate.exe cute (New transactioncallbackwithoutresult (){
Public void dointransaction () throws daoexception {
Deptdao. insert (Dept );
}
});
}
 
Public void insertemp (final employee EMP ){
Transactiontemplate.exe cute (New transactioncallbackwithoutresult (){
Public void dointransaction () throws daoexception {
Empdao. insert (EMP );
}
});
}
 
/**
* Obtain the list of all departments
* @ Return
*/
Public list <dept> getdeptlist (){
Return transactiontemplate.exe cute (New transactioncallback <list <dept> (){
Public list <dept> dointransaction () throws daoexception {
Return deptdao. getdeptlist ();
}
});
}
 
/**
* Retrieve the list of employees in a specified department
* @ Param deptid
* @ Param pageno
* @ Param pagesize
* @ Return
*/
Public pagemodel <employee> getemplistbydeptid (final int deptid, final int pageno, final int pagesize ){
Return transactiontemplate.exe cute (New transactioncallback <pagemodel <employee> (){
Public pagemodel <employee> dointransaction () throws daoexception {
Return empdao. getemplistbydeptid (deptid, pageno, pagesize );
}
});
}
 
/**
* Delete a department with the specified ID
* @ Param ID
*/
Public void deletedept (final int ID ){
Transactiontemplate.exe cute (New transactioncallbackwithoutresult (){
Public void dointransaction () throws daoexception {
Empdao. deletebydeptid (ID );
Deptdao. Delete (ID );
}
});
}
}

 

In this way, the transaction processing at the business layer is also transparent, and you only need to pay attention to the combination of business logic, is it easier to write ..

 

Don't understand ?? You need to first learn the template method of the gof design pattern; anonymous internal class of Java core technology.

 

Finally, today's gray web page makes the function of inserting code when I write an article unusable, causing all the text I wrote to be lost, so I had to repeat it again. Alas, I don't want to write it today. I'm sorry.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.