github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

timshadel / logging-datastore

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 1
    • 0
  • Source
  • Commits
  • Network (0)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Switch Branches (1)
    • master ✓
  • Switch Tags (0)
  • Branch List
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Java DataStore which lets you log the stack traces of getConnection requests — Read more

  Cancel

http://timshadel.com/2007/02/20/db-connection-problems-try-a-logging-datasource/

  Cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Move from SVN to git. 
Tim Shadel (author)
Fri Mar 28 09:50:58 -0700 2008
commit  0e3b19e41b7f02b644fcb518e431f3e901ba273d
tree    5e9dbabcdb6d1980b74aa15f7b6163d0137a1810
logging-datastore / src / main / java / com / timshadel / util / LoggingDataSource.java src/main/java/com/timshadel/util/LoggingDataSource.java
100644 107 lines (94 sloc) 2.555 kb
edit raw blame history
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();
}
}
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server