JDBC解耦

摘要

JDBC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//与具体的数据库解耦
public class JdbcUtil {

private static String driverClass;
private static String url;
private static String user;
private static String password;

static{
try {
InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("dbcfg.properties");
Properties props = new Properties();
props.load(in);

driverClass = props.getProperty("driverClass");
url = props.getProperty("url");
user = props.getProperty("user");
password = props.getProperty("password");
Class.forName(driverClass);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}

}

public static Connection getConnection() {
try {
Connection conn = DriverManager.getConnection(url,user,password);
return conn;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

public static void release(ResultSet rs, Statement stmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}

TransactionManager

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//事务管理器 解耦
public class TransationManager {
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
public static Connection getConnection(){
Connection conn = tl.get();//从当前线程上获得链接
if(conn==null){
conn = DBCPUtil.getConnection();
tl.set(conn);//把链接绑定到当前线程上
}
return conn;
}
public static void startTransaction(){
Connection conn = getConnection();
try {
conn.setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void commit(){
Connection conn = getConnection();
try {
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void rollback(){
Connection conn = getConnection();
try {
conn.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void release(){
Connection conn = getConnection();
try {
conn.close();
tl.remove();//与线程池有关,解除关系
} catch (SQLException e) {
e.printStackTrace();
}
}
}

对象代理工厂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class BeanFactory {
/**
* 产生BusinessService的实例
* @param isProxy ture,返回代理类。false,原来的类
* @return
*/
public static BusinessService getBusinessService(boolean isProxy){
final BusinessService s = new BusinessServiceImpl();
if(isProxy){
//返回实现的代理类
BusinessService proxyS = (BusinessService)Proxy.newProxyInstance(s.getClass().getClassLoader(),
s.getClass().getInterfaces(), new InvocationHandler() {

@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object rtValue = null;
try{
long time = System.currentTimeMillis();
TransationManager.startTransaction();
rtValue = method.invoke(s, args);
TransationManager.commit();
System.out.println(method.getName()+" cost time "+(System.currentTimeMillis()-time) +" millis second");
} catch (Exception e) {
TransationManager.rollback();
throw new RuntimeException(e);
}finally{
TransationManager.release();
}
return rtValue;
}
});
return proxyS;
}else{
return s;
}
}
}

达到的目的

  • 默认就开启了事务支持
  • 高度解耦
    1
    2
    3
    4
    5
    6
    7
    public class Test{
    public void t1(){
    BusinessService s = BeanFactory.getBusinessService(true);
    System.out.println(s.getClass().getName());
    s.transfer("aaa", "bbb", 100);
    }
    }