日期:2014-05-16 浏览次数:20535 次
package ro.kifs.diagnostic; import java.io.PrintStream; import java.sql.CallableStatement; import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.SQLWarning; import java.sql.Savepoint; import java.sql.Statement; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.UUID; public class Connection implements java.sql.Connection { private static HashMap _openedConnections = new HashMap(); java.sql.Connection _conn; private String _connid; private static SimpleDateFormat sdf = new SimpleDateFormat("MM/DD HH:mm:ss"); public static String getGUID() { String ret = UUID.randomUUID().toString(); return ret; } public void registerOpenedConnection() { _openedConnections.put(getConnectionId(), whoCalledMe()); } public static String getStillOpenedConnsStackTraces() { StringBuffer sb = new StringBuffer(5000); try { sb.append("<br>\n Who calls the unclosed connections: <BR>\n"); Iterator it = _openedConnections.entrySet().iterator(); Map.Entry me = null; while (it.hasNext()) { me = (Map.Entry)it.next(); sb.append("<BR>\n"); sb.append("" + me.getValue()); } } catch (Exception ex) { sb.append("ex when iterating the connection stacktraces: " + ex.getMessage()); } return sb.toString(); } private void $init$() { this._connid = null; } public String getConnectionId() { return this._connid; } public String whoCalledMe() { StringBuffer ret = new StringBuffer(300); StackTraceElement[] trace = new Throwable().getStackTrace(); for (int i = 0; i < trace.length - 1; ++i) { ret.append(trace[i].getClassName()); ret.append("."); ret.append(trace[i].getMethodName()); ret.append("() line:"); ret.append("() line:"); ret.append(trace[i].getLineNumber()); ret.append("<BR>\n"); } ret.append(sdf.format(new Date())); return ret.toString(); } public Connection(java.sql.Connection wrappedConnection) { $init$(); this._conn = wrappedConnection; this._connid = getGUID(); registerOpenedConnection(); } public Statement createStatement() throws SQLException { return this._conn.createStatement(); } public PreparedStatement prepareStatement(String sql) throws SQLException { return this._conn.prepareStatement(sql); } public CallableStatement prepareCall(String sql) throws SQLException { return this._conn.prepareCall(sql); } public String nativeSQL(String sql) throws SQLException { return this._conn.nativeSQL(sql); } public void setAutoCommit(boolean autoCommit) throws SQLException { this._conn.setAutoCommit(autoCommit); } public boolean getAutoCommit() throws SQLException { return this._conn.getAutoCommit(); } public void commit() throws SQLException { this._conn.commit(); } public void rollback() throws SQLException { this._conn.rollback(); } public void close() throws SQLException { try { this._conn.close(); this._conn = null; _openedConnections.remove(getConnectionId()); } catch (Exception x) { System.out.println(">>>>>>>>>>EX la wrapper.close(): " + x.getMessage()); x.printStackTrace(); } } public boolean isClosed() throws SQLException { return