mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed pooledStatementStat
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package com.alibaba.druid.bvt.filter;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.stat.JdbcSqlStat;
|
||||
import com.alibaba.druid.stat.JdbcStatManager;
|
||||
import com.alibaba.druid.util.Histogram;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class StatFilterAfterResetTest extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setFilters("stat");
|
||||
|
||||
dataSource.init();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
|
||||
public void test_stat() throws Exception {
|
||||
|
||||
final String sql = "SELECT 1";
|
||||
Assert.assertTrue(dataSource.isInited());
|
||||
|
||||
JdbcSqlStat sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
|
||||
|
||||
Assert.assertNull(sqlStat);
|
||||
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
rs.next();
|
||||
rs.close();
|
||||
|
||||
sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
|
||||
Assert.assertNotNull(sqlStat);
|
||||
|
||||
Histogram histogram = sqlStat.getExecuteAndResultHoldTimeHistogram();
|
||||
Assert.assertEquals("first failed", 1,
|
||||
histogram.getValue(0) + histogram.getValue(1) + histogram.getValue(2));
|
||||
|
||||
rs.close();
|
||||
|
||||
Assert.assertEquals("second failed", 1,
|
||||
histogram.getValue(0) + histogram.getValue(1) + histogram.getValue(2));
|
||||
|
||||
stmt.close();
|
||||
|
||||
conn.close();
|
||||
|
||||
Assert.assertEquals(1, histogram.getValue(0) + histogram.getValue(1) + histogram.getValue(2));
|
||||
}
|
||||
|
||||
JdbcStatManager.getInstance().reset();
|
||||
|
||||
Assert.assertFalse(sqlStat.isRemoved());
|
||||
|
||||
JdbcStatManager.getInstance().reset();
|
||||
Assert.assertTrue(sqlStat.isRemoved());
|
||||
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
rs.next();
|
||||
rs.close();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
Assert.assertNotSame(sqlStat, dataSource.getDataSourceStat().getSqlStat(sql));
|
||||
}
|
||||
}
|
||||
@@ -41,48 +41,27 @@ public class StatFilterResultSetMultiCloseTest extends TestCase {
|
||||
|
||||
Assert.assertNull(sqlStat);
|
||||
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
rs.next();
|
||||
rs.close();
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
|
||||
Assert.assertNotNull(sqlStat);
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
rs.next();
|
||||
rs.close();
|
||||
|
||||
Histogram histogram = sqlStat.getExecuteAndResultHoldTimeHistogram();
|
||||
Assert.assertEquals("first failed", 1,
|
||||
histogram.getValue(0) + histogram.getValue(1) + histogram.getValue(2));
|
||||
sqlStat = dataSource.getDataSourceStat().getSqlStat(sql);
|
||||
Assert.assertNotNull(sqlStat);
|
||||
|
||||
rs.close();
|
||||
Histogram histogram = sqlStat.getExecuteAndResultHoldTimeHistogram();
|
||||
Assert.assertEquals("first failed", 1, histogram.getValue(0) + histogram.getValue(1) + histogram.getValue(2));
|
||||
|
||||
Assert.assertEquals("second failed", 1,
|
||||
histogram.getValue(0) + histogram.getValue(1) + histogram.getValue(2));
|
||||
rs.close();
|
||||
|
||||
stmt.close();
|
||||
Assert.assertEquals("second failed", 1, histogram.getValue(0) + histogram.getValue(1) + histogram.getValue(2));
|
||||
|
||||
conn.close();
|
||||
stmt.close();
|
||||
|
||||
Assert.assertEquals(1, histogram.getValue(1));
|
||||
}
|
||||
conn.close();
|
||||
|
||||
JdbcStatManager.getInstance().reset();
|
||||
|
||||
Assert.assertFalse(sqlStat.isRemoved());
|
||||
|
||||
JdbcStatManager.getInstance().reset();
|
||||
Assert.assertTrue(sqlStat.isRemoved());
|
||||
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
rs.next();
|
||||
rs.close();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
Assert.assertNotSame(sqlStat, dataSource.getDataSourceStat().getSqlStat(sql));
|
||||
Assert.assertEquals(1, histogram.getValue(0) + histogram.getValue(1) + histogram.getValue(2));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user