1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.logging.jdbc;
17
18 import java.lang.reflect.InvocationHandler;
19 import java.lang.reflect.Method;
20 import java.lang.reflect.Proxy;
21 import java.sql.Connection;
22 import java.sql.PreparedStatement;
23 import java.sql.Statement;
24
25 import org.apache.ibatis.logging.Log;
26 import org.apache.ibatis.reflection.ExceptionUtil;
27
28
29
30
31
32
33
34 public final class ConnectionLogger extends BaseJdbcLogger implements InvocationHandler {
35
36 private final Connection connection;
37
38 private ConnectionLogger(Connection conn, Log statementLog, int queryStack) {
39 super(statementLog, queryStack);
40 this.connection = conn;
41 }
42
43 @Override
44 public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
45 try {
46 if (Object.class.equals(method.getDeclaringClass())) {
47 return method.invoke(this, params);
48 }
49 if ("prepareStatement".equals(method.getName()) || "prepareCall".equals(method.getName())) {
50 if (isDebugEnabled()) {
51 debug(" Preparing: " + removeExtraWhitespace((String) params[0]), true);
52 }
53 PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);
54 return PreparedStatementLogger.newInstance(stmt, statementLog, queryStack);
55 }
56 if ("createStatement".equals(method.getName())) {
57 Statement stmt = (Statement) method.invoke(connection, params);
58 return StatementLogger.newInstance(stmt, statementLog, queryStack);
59 }
60 return method.invoke(connection, params);
61 } catch (Throwable t) {
62 throw ExceptionUtil.unwrapThrowable(t);
63 }
64 }
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public static Connection newInstance(Connection conn, Log statementLog, int queryStack) {
79 InvocationHandler handler = new ConnectionLogger(conn, statementLog, queryStack);
80 ClassLoader cl = Connection.class.getClassLoader();
81 return (Connection) Proxy.newProxyInstance(cl, new Class[] { Connection.class }, handler);
82 }
83
84
85
86
87
88
89 public Connection getConnection() {
90 return connection;
91 }
92
93 }