mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
DruidDataSource support mbean register & unregister
This commit is contained in:
@@ -279,7 +279,6 @@ public class ConfigFilter extends FilterAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
private InputStream getFileAsStream(String filePath) throws FileNotFoundException {
|
||||
InputStream inStream = null;
|
||||
File file = new File(filePath);
|
||||
|
||||
@@ -139,6 +139,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
private boolean useGloalDataSourceStat = false;
|
||||
|
||||
private boolean mbeanRegistered = false;
|
||||
|
||||
public DruidDataSource(){
|
||||
this(false);
|
||||
}
|
||||
@@ -614,8 +616,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
initedLatch.await();
|
||||
|
||||
initedTime = new Date();
|
||||
ObjectName objectName = DruidDataSourceStatManager.addDataSource(this, this.name);
|
||||
this.setObjectName(objectName);
|
||||
registerMbean();
|
||||
|
||||
if (connectError != null && poolingCount == 0) {
|
||||
throw connectError;
|
||||
@@ -1222,7 +1223,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
poolingCount = 0;
|
||||
DruidDataSourceStatManager.removeDataSource(this);
|
||||
unregisterMbean();
|
||||
|
||||
enable = false;
|
||||
notEmpty.signalAll();
|
||||
@@ -1243,6 +1244,25 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
|
||||
public void registerMbean() {
|
||||
if (!mbeanRegistered) {
|
||||
ObjectName objectName = DruidDataSourceStatManager.addDataSource(this, this.name);
|
||||
this.setObjectName(objectName);
|
||||
this.mbeanRegistered = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void unregisterMbean() {
|
||||
if (mbeanRegistered) {
|
||||
DruidDataSourceStatManager.removeDataSource(this);
|
||||
mbeanRegistered = false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isMbeanRegistered() {
|
||||
return mbeanRegistered;
|
||||
}
|
||||
|
||||
void putLast(DruidConnectionHolder e, long lastActiveTimeMillis) {
|
||||
e.setLastActiveTimeMillis(lastActiveTimeMillis);
|
||||
connections[poolingCount++] = e;
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
|
||||
public class RegisterTest extends TestCase {
|
||||
public void test() throws Exception {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.init();
|
||||
|
||||
Assert.assertTrue(dataSource.isMbeanRegistered());
|
||||
|
||||
dataSource.registerMbean();
|
||||
Assert.assertTrue(dataSource.isMbeanRegistered());
|
||||
|
||||
dataSource.unregisterMbean();
|
||||
Assert.assertFalse(dataSource.isMbeanRegistered());
|
||||
Assert.assertFalse(dataSource.isMbeanRegistered());
|
||||
|
||||
dataSource.close();
|
||||
}
|
||||
}
|
||||
@@ -1,57 +1,56 @@
|
||||
package com.alibaba.druid.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.parser.SQLParserUtils;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Hashtable;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Properties;
|
||||
import java.util.Stack;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
import java.util.Vector;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
public class MemTest {
|
||||
|
||||
HashMap m = new HashMap();
|
||||
HashMap m1 = new HashMap(1);
|
||||
ConcurrentHashMap cm = new ConcurrentHashMap();
|
||||
ConcurrentHashMap cm1 = new ConcurrentHashMap(1, 0.75f, 1);
|
||||
ConcurrentSkipListMap csm = new ConcurrentSkipListMap();
|
||||
Hashtable ht = new Hashtable();
|
||||
Properties p = new Properties();
|
||||
TreeMap t = new TreeMap();
|
||||
LinkedHashMap l = new LinkedHashMap();
|
||||
|
||||
TreeSet ts = new TreeSet();
|
||||
HashSet hs = new HashSet();
|
||||
LinkedHashSet lhs = new LinkedHashSet();
|
||||
|
||||
ArrayList list = new ArrayList();
|
||||
ArrayList list1 = new ArrayList(1);
|
||||
|
||||
CopyOnWriteArrayList cpl = new CopyOnWriteArrayList();
|
||||
CopyOnWriteArraySet cps = new CopyOnWriteArraySet();
|
||||
Vector v = new Vector();
|
||||
LinkedList ll = new LinkedList();
|
||||
Stack stack = new Stack();
|
||||
PriorityQueue pq = new PriorityQueue();
|
||||
ConcurrentLinkedQueue clq = new ConcurrentLinkedQueue();
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// DruidDataSource dataSource = new DruidDataSource();
|
||||
// dataSource.setUrl("jdbc:mock:xxx");
|
||||
// dataSource.setInitialSize(1);
|
||||
// dataSource.setDbType("mysql");
|
||||
// dataSource.setFilters("mergeStat");
|
||||
// dataSource.setPoolPreparedStatements(true);
|
||||
// dataSource.setMaxOpenPreparedStatements(50);
|
||||
|
||||
// for (int i = 1000; i < 2000; ++i) {
|
||||
// String tableName = "t" + i;
|
||||
// String sql = "select * from " + tableName + " where " + tableName + ".id = " + i;
|
||||
// Connection conn = dataSource.getConnection();
|
||||
//
|
||||
// PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
// stmt.execute();
|
||||
// stmt.close();
|
||||
//
|
||||
// conn.close();
|
||||
// }
|
||||
|
||||
// Connection conn = dataSource.getConnection();
|
||||
//
|
||||
String sql = "SELECT UMID, HWID, MAC, GUID, RID , GMT_CREATE, GMT_MODIFIED FROM umid_rid " + //
|
||||
"WHERE RID = ? " + //
|
||||
"LIMIT 10";
|
||||
// PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
// stmt.execute();
|
||||
|
||||
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, "mysql");
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
SQLASTOutputVisitor visitor = ParameterizedOutputVisitorUtils.createParameterizedOutputVisitor(out, "mysql");
|
||||
stmt.accept(visitor);
|
||||
MemTest o = new MemTest();
|
||||
|
||||
Thread.sleep(1000 * 1000);
|
||||
// dataSource.close();
|
||||
|
||||
System.out.println(o);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user