View Javadoc
1   /*
2    * Copyright 2004-2025 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Connection proxy to add logging.
31   */
32  public class ConnectionLogProxy extends BaseLogProxy implements InvocationHandler {
33  
34    /** The Constant log. */
35    private static final Log log = LogFactory.getLog(Connection.class);
36  
37    /** The connection. */
38    private Connection connection;
39  
40    /**
41     * Instantiates a new connection log proxy.
42     *
43     * @param conn
44     *          the conn
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     * Creates a logging version of a connection.
86     *
87     * @param conn
88     *          - the original connection
89     *
90     * @return - the connection with logging
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     * return the wrapped connection.
100    *
101    * @return the connection
102    */
103   public Connection getConnection() {
104     return connection;
105   }
106 
107 }