We got nominated! Help us out and vote for GitHub as Best Bootstrapped Startup of 2008. (You can vote once a day.) [ hide ]

public
Description: Java DataStore which lets you log the stack traces of getConnection requests
Homepage: http://timshadel.com/2007/02/20/db-connection-problems-try-a-logging-datasource/
Clone URL: git://github.com/timshadel/logging-datastore.git
Tim Shadel (author)
Fri Mar 28 09:50:58 -0700 2008
logging-datastore / src / main / java / com / timshadel / util / LoggingDataSource.java
100644 107 lines (94 sloc) 2.555 kb
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.timshadel.util;
 
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
 
import org.apache.log4j.Logger;
import org.springframework.jdbc.datasource.DelegatingDataSource;
 
public class LoggingDataSource extends DelegatingDataSource {
 
  private Logger log = Logger.getLogger(LoggingDataSource.class);
  private String filterPattern;
  private String indent;
 
  public LoggingDataSource() {
    setIndentSize(8);
  }
  
  @Override
  public Connection getConnection() throws SQLException {
    Connection connection;
    try {
      connection = super.getConnection();
      logConnectionRequest();
    } catch (RuntimeException e) {
      logConnectionRequest(e);
      throw e;
    } catch (SQLException e) {
      logConnectionRequest(e);
      throw e;
    }
    return connection;
  }
 
  @Override
  public Connection getConnection(String username, String password) throws SQLException {
    Connection connection;
    try {
      connection = super.getConnection(username, password);
      logConnectionRequest();
    } catch (RuntimeException e) {
      logConnectionRequest(e);
      throw e;
    } catch (SQLException e) {
      logConnectionRequest(e);
      throw e;
    }
    return connection;
  }
 
  private void logConnectionRequest() {
    logConnectionRequest(null);
  }
 
  private void logConnectionRequest(Throwable exceptionThrown) {
    if (filterPattern == null) {
      log.debug("No filter set. Not pulling stacktrace.");
      return;
    }
    StackTraceUtil util = new StackTraceUtil();
    List<String> trace = util.getTrace(filterPattern);
    if (trace.size() == 0) {
      log.debug("No trace entries.");
      return;
    }
    StringBuilder sb = new StringBuilder();
    sb.append("Connection request stack trace, filtered by '" + filterPattern + "'\n");
    if (exceptionThrown != null) {
      sb.append(indent);
      sb.append("CAUGHT EXCEPTION (");
      sb.append(exceptionThrown.getClass().getName());
      sb.append("): ");
      sb.append(exceptionThrown.getMessage());
      sb.append("\n");
    }
    for(String s : trace) {
      if (!s.matches("^com.timshadel.util.LoggingDataSource[^a-zA-Z].*")) {
        sb.append(indent);
        sb.append(s);
        sb.append("\n");
      }
    }
    if (exceptionThrown == null) {
      log.info(sb.toString());
    } else {
      log.error(sb.toString());
    }
  }
 
  public void setFilterPattern(String pattern) {
    this.filterPattern = pattern;
  }
 
  public void setIndent(String indent) {
    this.indent = indent;
  }
 
  public void setIndentSize(int size) {
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < size; i++) {
      sb.append(" ");
    }
    this.indent = sb.toString();
  }
}