mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
DruidDataSource support clone
This commit is contained in:
@@ -210,10 +210,10 @@ public class EncodingConvertFilter extends FilterAdapter {
|
||||
String text = IOUtils.read(reader);
|
||||
return new StringReader(decode(connection, text));
|
||||
}
|
||||
|
||||
return object;
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
|
||||
public Object decodeObject(CallableStatementProxy stmt, Object object) throws SQLException {
|
||||
if (object instanceof String) {
|
||||
return decode(stmt.getConnectionProxy(), (String) object);
|
||||
@@ -224,11 +224,11 @@ public class EncodingConvertFilter extends FilterAdapter {
|
||||
String text = IOUtils.read(reader);
|
||||
return new StringReader(decode(stmt.getConnectionProxy(), text));
|
||||
}
|
||||
|
||||
|
||||
if (object instanceof ResultSet) {
|
||||
long resultSetId = stmt.getConnectionProxy().getDirectDataSource().createResultSetId();
|
||||
ResultSet resultSet = (ResultSet) object;
|
||||
return new ResultSetProxyImpl(stmt, resultSet, dataSource.createResultSetId(),
|
||||
stmt.getLastExecuteSql());
|
||||
return new ResultSetProxyImpl(stmt, resultSet, resultSetId, stmt.getLastExecuteSql());
|
||||
}
|
||||
|
||||
return object;
|
||||
|
||||
@@ -72,8 +72,6 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
|
||||
private boolean connectionStackTraceEnable = false;
|
||||
|
||||
protected DataSourceProxy dataSource;
|
||||
|
||||
protected final AtomicLong resetCount = new AtomicLong();
|
||||
|
||||
// 3 seconds is slow sql
|
||||
@@ -140,7 +138,6 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
|
||||
@Override
|
||||
public synchronized void init(DataSourceProxy dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
|
||||
this.dataSourceStat = dataSource.getDataSourceStat();
|
||||
|
||||
@@ -426,12 +423,12 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
sqlStat.addExecuteTime(statement.getLastExecuteType(), nanoSpan);
|
||||
statement.setLastExecuteTimeNano(nanoSpan);
|
||||
if ((!statement.isFirstResultSet()) && statement.getLastExecuteType() == StatementExecuteType.Execute) {
|
||||
try {
|
||||
int updateCount = statement.getUpdateCount();
|
||||
sqlStat.addUpdateCount(updateCount);
|
||||
} catch (SQLException e) {
|
||||
LOG.error("getUpdateCount error", e);
|
||||
}
|
||||
try {
|
||||
int updateCount = statement.getUpdateCount();
|
||||
sqlStat.addUpdateCount(updateCount);
|
||||
} catch (SQLException e) {
|
||||
LOG.error("getUpdateCount error", e);
|
||||
}
|
||||
}
|
||||
|
||||
long millis = nanoSpan / (1000 * 1000);
|
||||
@@ -569,8 +566,9 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
JdbcConnectionStat.Entry counter = (JdbcConnectionStat.Entry) connection.getAttributes().get(ATTR_NAME_CONNECTION_STAT);
|
||||
|
||||
if (counter == null) {
|
||||
String dataSourceName = connection.getDirectDataSource().getName();
|
||||
connection.getAttributes().put(ATTR_NAME_CONNECTION_STAT,
|
||||
new JdbcConnectionStat.Entry(this.dataSource.getName(), connection.getId()));
|
||||
new JdbcConnectionStat.Entry(dataSourceName, connection.getId()));
|
||||
counter = (JdbcConnectionStat.Entry) connection.getAttributes().get(ATTR_NAME_CONNECTION_STAT);
|
||||
}
|
||||
|
||||
@@ -705,29 +703,33 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
|
||||
@Override
|
||||
public String getConnectionUrl() {
|
||||
if (dataSource == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return dataSource.getUrl();
|
||||
}
|
||||
|
||||
public JdbcSqlStat createSqlStat(StatementProxy statement, String sql) {
|
||||
JdbcStatContext context = JdbcStatManager.getInstance().getStatContext();
|
||||
String contextSql = context != null ? context.getSql() : null;
|
||||
if (contextSql != null && contextSql.length() > 0) {
|
||||
return dataSourceStat.createSqlStat(contextSql);
|
||||
} else {
|
||||
sql = mergeSql(sql);
|
||||
return dataSourceStat.createSqlStat(sql);
|
||||
}
|
||||
String contextSql = context != null ? context.getSql() : null;
|
||||
if (contextSql != null && contextSql.length() > 0) {
|
||||
return dataSourceStat.createSqlStat(contextSql);
|
||||
} else {
|
||||
sql = mergeSql(sql);
|
||||
return dataSourceStat.createSqlStat(sql);
|
||||
}
|
||||
}
|
||||
|
||||
public JdbcSqlStat getSqlCounter(String sql) {
|
||||
|
||||
sql = mergeSql(sql); //mergeSql
|
||||
|
||||
sql = mergeSql(sql); // mergeSql
|
||||
return getSqlStat(sql);
|
||||
}
|
||||
|
||||
public JdbcSqlStat getSqlStat(String sql) {
|
||||
|
||||
sql = mergeSql(sql);
|
||||
|
||||
sql = mergeSql(sql);
|
||||
return dataSourceStat.getSqlStat(sql);
|
||||
}
|
||||
|
||||
|
||||
@@ -68,6 +68,18 @@ public class TestClone extends TestCase {
|
||||
Assert.assertTrue(mockConn_0.isClosed());
|
||||
Assert.assertTrue(mockConn_1.isClosed());
|
||||
Assert.assertTrue(mockConn_2.isClosed());
|
||||
|
||||
// now is new dataSource;
|
||||
dataSource = clone;
|
||||
|
||||
Connection conn_3 = dataSource.getConnection();
|
||||
Connection conn_4 = dataSource.getConnection();
|
||||
Connection conn_5 = dataSource.getConnection();
|
||||
|
||||
conn_3.close();
|
||||
conn_4.close();
|
||||
conn_5.close();
|
||||
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user