mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
修正因为SqlStat OOM防止措施导致的空指针问题
This commit is contained in:
@@ -30,6 +30,8 @@ import javax.management.openmbean.TabularData;
|
||||
import com.alibaba.druid.filter.Filter;
|
||||
import com.alibaba.druid.filter.FilterChain;
|
||||
import com.alibaba.druid.filter.FilterEventAdapter;
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.proxy.jdbc.CallableStatementProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
|
||||
@@ -51,6 +53,8 @@ import com.alibaba.druid.util.TransactionInfo;
|
||||
*/
|
||||
public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(StatFilter.class);
|
||||
|
||||
public final static String ATTR_SQL = "stat.sql";
|
||||
public final static String ATTR_UPDATE_COUNT = "stat.updteCount";
|
||||
public final static String ATTR_TRANSACTION = "stat.tx";
|
||||
@@ -318,8 +322,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
statement.getAttributes().put(ATTR_SQL, sqlStat);
|
||||
}
|
||||
|
||||
sqlStat.addExecuteBatchCount(batchSize);
|
||||
|
||||
if (sqlStat != null) {
|
||||
sqlStat.addExecuteBatchCount(batchSize);
|
||||
}
|
||||
|
||||
internalBeforeStatementExecute(statement, sql);
|
||||
|
||||
// super.statementExecuteBatchBefore(statement);
|
||||
@@ -363,8 +369,11 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
sqlStat.setName(statContext.getName());
|
||||
sqlStat.setFile(statContext.getFile());
|
||||
}
|
||||
sqlStat.setExecuteLastStartTime(System.currentTimeMillis());
|
||||
sqlStat.incrementRunningCount();
|
||||
|
||||
if (sqlStat != null) {
|
||||
sqlStat.setExecuteLastStartTime(System.currentTimeMillis());
|
||||
sqlStat.incrementRunningCount();
|
||||
}
|
||||
|
||||
statement.getAttributes().put(ATTR_UPDATE_COUNT, 0);
|
||||
}
|
||||
@@ -400,13 +409,15 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
// // SQL
|
||||
final JdbcSqlStat sqlStat = getSqlStat(statement);
|
||||
|
||||
sqlStat.incrementExecuteSuccessCount();
|
||||
for (int updateCount : updateCountArray) {
|
||||
sqlStat.addUpdateCount(updateCount);
|
||||
}
|
||||
if (sqlStat != null) {
|
||||
sqlStat.incrementExecuteSuccessCount();
|
||||
for (int updateCount : updateCountArray) {
|
||||
sqlStat.addUpdateCount(updateCount);
|
||||
}
|
||||
|
||||
sqlStat.decrementExecutingCount();
|
||||
sqlStat.addExecuteTime(nanoSpan);
|
||||
sqlStat.decrementExecutingCount();
|
||||
sqlStat.addExecuteTime(nanoSpan);
|
||||
}
|
||||
|
||||
if (updateCountArray.length == 1) {
|
||||
statement.getAttributes().put(ATTR_UPDATE_COUNT, updateCountArray[0]);
|
||||
@@ -432,8 +443,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
// SQL
|
||||
JdbcSqlStat sqlStat = getSqlStat(statement);
|
||||
|
||||
sqlStat.error(error);
|
||||
sqlStat.addExecuteTime(nanoSpan);
|
||||
if (sqlStat != null) {
|
||||
sqlStat.error(error);
|
||||
sqlStat.addExecuteTime(nanoSpan);
|
||||
}
|
||||
|
||||
super.statement_executeErrorAfter(statement, sql, error);
|
||||
}
|
||||
@@ -675,7 +688,7 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
if (dataSourceStat.getSqlStatMap().size() >= maxSqlStatCount) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
JdbcSqlStat newSqlStat = new JdbcSqlStat(sql);
|
||||
if (dataSourceStat.getSqlStatMap().putIfAbsent(sql, newSqlStat) == null) {
|
||||
newSqlStat.setId(JdbcStatManager.getInstance().generateSqlId());
|
||||
@@ -685,6 +698,10 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
sqlStat = dataSourceStat.getSqlStatMap().get(sql);
|
||||
}
|
||||
|
||||
if (sqlStat == null) {
|
||||
LOG.error("stat is null");
|
||||
}
|
||||
|
||||
return sqlStat;
|
||||
}
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@ public class TestPoolPreparedStatement extends TestCase {
|
||||
Connection conn = dataSource.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement("SELECT ?");
|
||||
raw = stmt.unwrap(MockPreparedStatement.class);
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
@@ -57,6 +58,7 @@ public class TestPoolPreparedStatement extends TestCase {
|
||||
Connection conn = dataSource.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement("SELECT ?");
|
||||
Assert.assertTrue(raw == stmt.unwrap(MockPreparedStatement.class));
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.mock.MockPreparedStatement;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.PoolableConnection;
|
||||
import com.alibaba.druid.stat.DruidDataSourceStatManager;
|
||||
|
||||
public class TestPoolPreparedStatement2 extends TestCase {
|
||||
@@ -18,7 +18,7 @@ public class TestPoolPreparedStatement2 extends TestCase {
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
|
||||
|
||||
|
||||
driver = new MockDriver();
|
||||
|
||||
dataSource = new DruidDataSource();
|
||||
@@ -45,13 +45,23 @@ public class TestPoolPreparedStatement2 extends TestCase {
|
||||
|
||||
public void test_stmtCache() throws Exception {
|
||||
|
||||
for (int i = 0; i < 1000 * 1000 * 10; ++i){
|
||||
for (int i = 0; i < 1000 * 100 * 1; ++i) {
|
||||
Connection conn = dataSource.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement("SELECT " + i);
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
PoolableConnection poolableConn = conn.unwrap(PoolableConnection.class);
|
||||
Assert.assertNotNull(poolableConn);
|
||||
|
||||
Assert.assertEquals(dataSource.getMaxPoolPreparedStatementPerConnectionSize(),
|
||||
poolableConn.getConnectionHolder().getStatementPool().getMap().size());
|
||||
|
||||
conn.close();
|
||||
|
||||
Assert.assertEquals(0, dataSource.getActiveCount());
|
||||
Assert.assertEquals(1, dataSource.getPoolingCount());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user