分层架构下的纯JDBC事务控制简单解决方案【转】
时间:2021-07-01 10:21:17
帮助过:3人阅读
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.DataSources;
import com.tjitcast.dao.DaoException;
public class DbUtils {
private static Properties prop = new Properties();
private static DataSource ds = null;
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
static{
try {
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties"));
} catch (IOException e) {
e.printStackTrace();
System.out.println("在classpath下没有找到jdbc.properties文件");
}
try {
Class.forName("com.mysql.jdbc.Driver");
DataSource unpooled = DataSources.unpooledDataSource(
prop.getProperty("url"),
prop.getProperty("user"),
prop.getProperty("password"));
ds = DataSources.pooledDataSource(unpooled);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
private DbUtils(){}
public static synchronized Connection getConnection(){
Connection conn = tl.get();
if(null == conn){
try {
conn = ds.getConnection();
tl.set(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return conn;
}
public static synchronized TransactionManager getTranManager(){
return new TransactionManager(getConnection());
}
protected static void close(Connection conn) throws DaoException{
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
throw new DaoException("关闭连接时出现异常",e);
} finally {
tl.remove();
}
}
}
}
如下事务管理器类:
[java] view plaincopy
- package com.tjitcast.common;
- import java.sql.Connection;
- import java.sql.SQLException;
- import com.tjitcast.dao.DaoException;
- public class TransactionManager {
- private Connection conn;
-
- protected TransactionManager(Connection conn) {
- this.conn = conn;
- }
-
-
- public void beginTransaction() throws DaoException{
- try {
- conn.setAutoCommit(false);
- } catch (SQLException e) {
- throw new DaoException("开户事务时出现异常",e);
- }
- }
-
-
- public void commitAndClose() throws DaoException{
- try {
- conn.commit();
- } catch (SQLException e) {
- throw new DaoException("提交事务时出现异常",e);
- }finally{
- DbUtils.close(conn);
- }
- }
-
-
- public void rollbackAndClose()throws DaoException{
- try {
- conn.rollback();
- } catch (SQLException e) {
- throw new DaoException("回滚事务时出现异常",e);
- }finally{
- DbUtils.close(conn);
- }
- }
- }
如下业务层类:
[java] view plaincopy
- package com.tjitcast.service;
- import java.util.List;
- import com.tjitcast.common.DbUtils;
- import com.tjitcast.common.TransactionManager;
- 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;
- public class ServiceFacade {
- private DeptDao deptDao = DaoFactory.getInstance("deptDao", DeptDao.class);
- private EmployeeDao empDao = DaoFactory.getInstance("empDao", EmployeeDao.class);
-
-
- public void insertDept(Dept dept){
- TransactionManager tx = DbUtils.getTranManager();
- try{
- tx.beginTransaction();
-
- deptDao.insert(dept);
-
- tx.commitAndClose();
- }catch (DaoException e) {
- tx.rollbackAndClose();
- }
- }
-
-
- public void insertEmp(Employee emp){
- TransactionManager tx = DbUtils.getTranManager();
- try{
- tx.beginTransaction();
-
- empDao.insert(emp);
-
- tx.commitAndClose();
- }catch (DaoException e) {
- tx.rollbackAndClose();
- }
- }
-
-
- public List<Dept> getDeptList(){
- List<Dept> list = null;
-
- TransactionManager tx = DbUtils.getTranManager();
- try{
- tx.beginTransaction();
-
- list = deptDao.getDeptList();
-
- tx.commitAndClose();
- }catch (DaoException e) {
- e.printStackTrace();
- tx.rollbackAndClose();
- }
-
- return list;
- }
-
-
- public PageModel<Employee> getEmpListByDeptId(int deptId, int pageNo, int pageSize){
- PageModel<Employee> pm = null;
- TransactionManager tx = DbUtils.getTranManager();
- try{
- tx.beginTransaction();
-
- pm = empDao.getEmpListByDeptId(deptId, pageNo, pageSize);
-
- tx.commitAndClose();
- }catch (DaoException e) {
- tx.rollbackAndClose();
- }
- return pm;
- }
-
-
- public void deleteDept(int id){
-
- TransactionManager tx = DbUtils.getTranManager();
- try{
- tx.beginTransaction();
-
- empDao.deleteByDeptId(id);
- deptDao.delete(id);
-
- tx.commitAndClose();
- }catch (DaoException e) {
- tx.rollbackAndClose();
- }
- }
- }
具体的示例代码结构如下(Eclipse工程):
分层架构下的纯JDBC事务控制简单解决方案【转】
标签: