上篇文章《分层架构下的纯JDBC事务控制简单解决方案》中对四层架构应用中使用纯JDBC时的事务控制进行了论述,并提供了一个简单解决方案,得到了很多网友的回复。有些网友也应该看到:在编写业务层中的方法时,事务处理的代码框架都一样,只是把持久层的操作组合起来,针对这种情况,我使用了【模板方法】模式对它进行了再度封装。添加以下几个接口和类:
1. 事务回调接口(回调方法有返回值):TransactionCallback.java
package com.tjitcast.common;
import com.tjitcast.dao.DaoException;
/**
* 事务回调接口
* @author qiujy
*/
public interface TransactionCallback<T> {
/**
* 要在事务中回调执行的方法
* @return 所指定类型的数据
* @throws DaoException 数据访问异常
*/
T doInTransaction() throws DaoException;
}
2. 事务回调接口(回调方法没有返回值):TransactionCallbackWithoutResult.java
package com.tjitcast.common;
import com.tjitcast.dao.DaoException;
/**
* 无返回值 的事务回调接口
* @author qiujy
*/
public interface TransactionCallbackWithoutResult {
/**
* 要在事务中回调执行的方法
* @throws DaoException 数据访问异常
*/
public void doInTransaction() throws DaoException;
}
3. 在事务中执行指定回调接口方法的模板类:TransactionTemplate.java
package com.tjitcast.common;
import com.tjitcast.dao.DaoException;
/**
* 在事务中执行指定回调接口方法的模板类
* @author qiujy
*/
public class TransactionTemplate {
/**
* 在事务里执行回调接口实现类中有返回值的方法
* @param <T> 返回值类型
* @param callback 回调接口
* @return 指定类型的返回值
* @throws DaoException 数据访问异常
*/
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;
}
/**
* 在事务里执行回调接口实现类中没有返回值的方法
* @param callback 回调接口
* @throws DaoException 数据访问异常
*/
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;
}
}
}
添加以上几个接口和类后,业务逻辑层ServiceFacade类的代码可以修改成如下格式:
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;
/**
* 业务层门面 --> 添加事务控制
* JDBC的事务默认自动提交方式,
* @author qiujy
*/
public class ServiceFacade {
private DeptDao deptDao = DaoFactory.getInstance("deptDao", DeptDao.class);
private EmployeeDao empDao = DaoFactory.getInstance("empDao", EmployeeDao.class);
/**
* 新增部门
* @param dept
*/
public void insertDept(final Dept dept){
TransactionTemplate.execute(new TransactionCallbackWithoutResult() {
public void doInTransaction() throws DaoException {
deptDao.insert(dept);
}
});
}
public void insertEmp(final Employee emp){
TransactionTemplate.execute(new TransactionCallbackWithoutResult() {
public void doInTransaction() throws DaoException {
empDao.insert(emp);
}
});
}
/**
* 获取所有部门的列表
* @return
*/
public List<Dept> getDeptList(){
return TransactionTemplate.execute(new TransactionCallback<List<Dept>>() {
public List<Dept> doInTransaction() throws DaoException {
return deptDao.getDeptList();
}
});
}
/**
* 获取指定部门下的员工分页列表
* @param deptId
* @param pageNo
* @param pageSize
* @return
*/
public PageModel<Employee> getEmpListByDeptId(final int deptId, final int pageNo, final int pageSize){
return TransactionTemplate.execute(new TransactionCallback<PageModel<Employee>>() {
public PageModel<Employee> doInTransaction() throws DaoException {
return empDao.getEmpListByDeptId(deptId, pageNo, pageSize);
}
});
}
/**
* 删除指定ID的部门
* @param id
*/
public void deleteDept(final int id){
TransactionTemplate.execute(new TransactionCallbackWithoutResult() {
public void doInTransaction() throws DaoException {
empDao.deleteByDeptId(id);
deptDao.delete(id);
}
});
}
}
这样,业务层的事务处理也被透明化了,你又只需要关注业务逻辑的组合,是不是写起来更爽啦。。
不理解??那就得先去学看看GOF设计模式之模板方法;Java核心技术之匿名内部类。
最后扯一句:今天的灰色网页使得写文章时插入代码的功能不能用,害我写了一大段的文字全丢了,只好重来一次。唉,今天不写了,默哀去。