标签:
以下只是简要说明几个核心类
数据库连接控制器ConnectionManager
public class ConnectionManager { private static ThreadLocal<Connection> connectionHolder = new ThreadLocal<Connection>(); // 取得当前本地线程局部变量的副本--- 获取连接 public static Connection getConnection() { Connection conn = connectionHolder.get(); if (conn == null) { conn = new DBUtil().getConn(); connectionHolder.set(conn); } return conn; } /** * 关闭数据库连接 */ public static void closeConnection() { Connection conn = connectionHolder.get(); if (conn != null) { try { conn.close(); connectionHolder.remove(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 手工开启事务 * * @param conn */ public static void beginTransaction(Connection conn) { try { if (conn != null) { if (conn.getAutoCommit()) { conn.setAutoCommit(false); } } } catch (SQLException e) { e.printStackTrace(); } } /** * 设置事务的提交 * * @param conn */ public static void commitTransaction(Connection conn) { try { if (conn != null) { if (!conn.getAutoCommit()) { conn.commit(); } } } catch (Exception e) { e.printStackTrace(); } } /** * 设置事务的回滚 * * @param conn */ public static void rollbackTransaction(Connection conn) { try { if (conn != null) { if (!conn.getAutoCommit()) { conn.rollback(); } } } catch (Exception e) { e.printStackTrace(); } } /** * 恢复原始的事务提交 * * @param conn */ public static void recoverTransaction(Connection conn) { try { if (conn != null) { if (conn.getAutoCommit()) { conn.setAutoCommit(false); } else { conn.setAutoCommit(true); } } } catch (Exception e) { e.printStackTrace(); } } }
数据访问层抽象类:AbstractDAO
public abstract class AbstractDAO { protected Connection conn = null; protected PreparedStatement pstmt = null; public AbstractDAO(){ this.conn = ConnectionManager.getConnection(); } }
数据访问层工厂: DAOFactory
public class DAOFactory { public static IMemberDAO getIMemberDAOInstance() throws Exception { String baseName = ResourceUtil.getResourceBundle("Database", "db.name"); String className = ResourceUtil.getResourceBundle(baseName, "member.class"); return (IMemberDAO) Class.forName(className).newInstance(); }
业务层代理类:ServiceProxy
public class ServiceProxy implements InvocationHandler, Serializable { private Object obj; // 真实主题 public Object bind(Object obj) { this.obj = obj; // 实现的MemberServiceImpl return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj .getClass().getInterfaces(), this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object value = null; // 绑定对象的时候进行数据库连接的初始化验证 if (method.getName().contains("login") || method.getName().contains("add") || method.getName().contains("edit") || method.getName().contains("rm")) { // 如果现在是更新操作 Connection conn = ConnectionManager.getConnection(); try { ConnectionManager.beginTransaction(conn); value = method.invoke(this.obj, args); ConnectionManager.commitTransaction(conn); } catch (Exception e) { ConnectionManager.rollbackTransaction(conn); } finally { ConnectionManager.recoverTransaction(conn); ConnectionManager.closeConnection(); } } else { try { value = method.invoke(this.obj, args); }catch (Exception e) { throw e; } finally { ConnectionManager.closeConnection(); } } return value; } }
业务层工厂类:ServiceFactory
public class ServiceFactory { public static IMemberService getIMemberServiceInstance() { return (IMemberService) new ServiceProxy() .bind(new MemberServiceImpl()); } }
标签:
原文地址:http://www.cnblogs.com/denied/p/5496680.html