1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.ibatis.common.jdbc.logging;
17
18 import com.ibatis.common.beans.ClassInfo;
19 import com.ibatis.common.logging.Log;
20 import com.ibatis.common.logging.LogFactory;
21
22 import java.lang.reflect.InvocationHandler;
23 import java.lang.reflect.Method;
24 import java.lang.reflect.Proxy;
25 import java.sql.Connection;
26 import java.sql.PreparedStatement;
27 import java.sql.Statement;
28
29
30
31
32 public class ConnectionLogProxy extends BaseLogProxy implements InvocationHandler {
33
34
35 private static final Log log = LogFactory.getLog(Connection.class);
36
37
38 private Connection connection;
39
40
41
42
43
44
45
46 private ConnectionLogProxy(Connection conn) {
47 super();
48 this.connection = conn;
49 if (log.isDebugEnabled()) {
50 log.debug("{conn-" + id + "} Connection");
51 }
52 }
53
54 @Override
55 public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
56 try {
57 if ("prepareStatement".equals(method.getName())) {
58 if (log.isDebugEnabled()) {
59 log.debug("{conn-" + id + "} Preparing Statement: " + removeBreakingWhitespace((String) params[0]));
60 }
61 PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);
62 return PreparedStatementLogProxy.newInstance(stmt, (String) params[0]);
63 }
64 if ("prepareCall".equals(method.getName())) {
65 if (log.isDebugEnabled()) {
66 log.debug("{conn-" + id + "} Preparing Call: " + removeBreakingWhitespace((String) params[0]));
67 }
68 PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);
69 return PreparedStatementLogProxy.newInstance(stmt, (String) params[0]);
70 }
71 if ("createStatement".equals(method.getName())) {
72 Statement stmt = (Statement) method.invoke(connection, params);
73 return StatementLogProxy.newInstance(stmt);
74 }
75 return method.invoke(connection, params);
76 } catch (Throwable t) {
77 Throwable t1 = ClassInfo.unwrapThrowable(t);
78 log.error("Error calling Connection." + method.getName() + ':', t1);
79 throw t1;
80 }
81
82 }
83
84
85
86
87
88
89
90
91
92 public static Connection newInstance(Connection conn) {
93 InvocationHandler handler = new ConnectionLogProxy(conn);
94 ClassLoader cl = Connection.class.getClassLoader();
95 return (Connection) Proxy.newProxyInstance(cl, new Class[] { Connection.class }, handler);
96 }
97
98
99
100
101
102
103 public Connection getConnection() {
104 return connection;
105 }
106
107 }