@Override public void close() throws SQLException { ds.closeConnection(this); }
@Override public int clearSlowSqls() { synchronized (slowSqlLock){ slowSqlIndex = 0; return clearSqls(slowSqls); } }
@Override protected void endExecuteStatement(StatementProxy stmt) { ds.onStatementEndExecute(this, stmt); }
protected void onStatementEndExecute(MConnectionProxy conn, StatementProxy stmt) { if(!conn.printThreadDumpEnable()){ return; if(config.getVerySlowSqlThreshold() > 0 && stmt.getLastExecutingDurationMs() >= config.getVerySlowSqlThreshold()) { ss = new SlowSql(stmt.getLastExecutingSql(), stmt.getLastExecutingDurationMs(), conn.getStackTraceOnOpen()); if(config.isLogVerySlowSql()) { log.warn("Found very slow sql ->\n time : {}ms\n sql : {}\n trace : [ \n{}]", ss.getDurationMs(), ss.getSql(), new StackTraceStringBuilder(ss.getStackTraceElements()).toString()); }else if(config.getSlowSqlThreshold() > 0 && stmt.getLastExecutingDurationMs() >= config.getSlowSqlThreshold()) { ss = new SlowSql(stmt.getLastExecutingSql(), stmt.getLastExecutingDurationMs(), conn.getStackTraceOnOpen()); if(config.isLogSlowSql()) { log.warn("Found slow sql ->\n time : {}ms\n sql : {}\n trace : [ \n{}]", ss.getDurationMs(), ss.getSql(), new StackTraceStringBuilder(ss.getStackTraceElements()).toString());
@Managed public ActiveConnectionsModel getActiveConnections() { MConnection[] activeConnections = MDataSourceProxy.this.getActiveConnections(); ConnectionModel[] activeConnectionModels = new ConnectionModel[activeConnections.length]; for(int i=0;i<activeConnectionModels.length;i++) { activeConnectionModels[i] = new ConnectionModel(activeConnections[i]); } return new ActiveConnectionsModel(activeConnectionModels); }
@Override public Connection getConnection() throws SQLException { return openConnection(super.getConnection()); }
protected Connection openConnection(Connection connection) { if(null == connection) { return null; } MConnectionProxy proxy = new MConnectionProxy(this, connection); activeConnections.add(proxy); return proxy; }
protected void closeConnection(MConnectionProxy proxy) throws SQLException { Connection conn = proxy.wrapped(); try{ activeConnections.remove(proxy); }finally{ conn.close(); } }
public String getOpenTime() { return Dates.format(new Date(connection.getOpenTime()), DateFormats.TIMESTAMP_PATTERN); }
@Override public final StackTraceElement[] getStackTraceOnOpen() { return hasStackTraceOnOpen() ? super.getStackTraceOnOpen() : ((ConnectionProxy)conn).getStackTraceOnOpen(); }
public String getStackTrace() { return new StackTraceStringBuilder(connection.getStackTraceOnOpen()).toString(); } }
protected void exportDataSourceMBean(String name, DataSource ds) { if(!(ds instanceof MDataSource)) { return; } mbeanExporter.export(objectName(name), ((MDataSource)ds).getMBean()); }
protected void notifyDataSourceCreated(String name, DataSource ds, boolean exportMBean) { if(ds instanceof MDataSourceProxy) { ((MDataSourceProxy) ds).setName(name); } if(exportMBean) { exportDataSourceMBean(name, ds); } for(DataSourceListener l : listeners){ l.onDataSourceCreated(name, ds); } }
public DataSource tryCreateDataSource(DataSourceProps conf) throws SQLException { DataSource ds = null; String name = conf.getDataSourceType(); if(!Strings.isEmpty(name)) { DataSourceFactory dsf = dataSourceFactories.get(name); if(null == dsf) { throw new IllegalStateException("DataSource Type '" + name + "' not found"); } ds = dsf.tryCreateDataSource(conf); }else if(null != defaultDataSourceFactory){ ds = defaultDataSourceFactory.tryCreateDataSource(conf); } if(null == ds && config.isDebug()){ ds = unpooledDataSourceFactory.tryCreateDataSource(conf); } if(null != ds) { validateDataSource(ds); } if(monitoring && null != ds && ! (ds instanceof MDataSourceProxy)) { ds = new MDataSourceProxy(ds, this); } return ds; }
@Override public int clearVerySlowSqls() { synchronized (verySlowSqlLock){ verySlowSqlIndex = 0; return clearSqls(verySlowSqls); } }
@Override public Connection getConnection(String username, String password) throws SQLException { return openConnection(super.getConnection(username, password)); }