mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
mbean支持shrink
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@541 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -743,9 +743,11 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
int removeCount = evictList.size();
|
||||
System.arraycopy(connections, removeCount, connections, 0, count - removeCount);
|
||||
Arrays.fill(connections, count - removeCount, count, null);
|
||||
count -= removeCount;
|
||||
if (removeCount > 0) {
|
||||
System.arraycopy(connections, removeCount, connections, 0, count - removeCount);
|
||||
Arrays.fill(connections, count - removeCount, count, null);
|
||||
count -= removeCount;
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
@@ -1128,6 +1130,32 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
@Override
|
||||
public void shrink() {
|
||||
throw new UnsupportedOperationException();
|
||||
final List<ConnectionHolder> evictList = new ArrayList<ConnectionHolder>();
|
||||
lock.lock();
|
||||
try {
|
||||
for (int i = 0; i < count; ++i) {
|
||||
ConnectionHolder connection = connections[i];
|
||||
|
||||
if (count - evictList.size() <= minIdle) {
|
||||
break;
|
||||
}
|
||||
|
||||
evictList.add(connection);
|
||||
}
|
||||
int removeCount = evictList.size();
|
||||
if (removeCount > 0) {
|
||||
System.arraycopy(connections, removeCount, connections, 0, count - removeCount);
|
||||
Arrays.fill(connections, count - removeCount, count, null);
|
||||
count -= removeCount;
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
for (ConnectionHolder item : evictList) {
|
||||
Connection connection = item.getConnection();
|
||||
JdbcUtils.close(connection);
|
||||
destroyCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.awt.EventQueue;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
@@ -17,6 +18,10 @@ import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.management.InstanceNotFoundException;
|
||||
import javax.management.MBeanRegistrationException;
|
||||
import javax.management.MalformedObjectNameException;
|
||||
import javax.management.ObjectName;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
@@ -446,6 +451,12 @@ public class DruidDataSourceUIManager extends JFrame {
|
||||
|
||||
public void closeDataSource_actionPerformed(ActionEvent e) {
|
||||
dataSource.close();
|
||||
|
||||
try {
|
||||
ManagementFactory.getPlatformMBeanServer().unregisterMBean(new ObjectName("com.alibaba.druid:type=DruidDataSource"));
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
txtUrl.setEnabled(true);
|
||||
txtDriverClass.setEnabled(true);
|
||||
@@ -487,6 +498,8 @@ public class DruidDataSourceUIManager extends JFrame {
|
||||
|
||||
dataSource.setTimeBetweenEvictionRunsMillis(60000);
|
||||
dataSource.setNumTestsPerEvictionRun(20);
|
||||
|
||||
ManagementFactory.getPlatformMBeanServer().registerMBean(dataSource, new ObjectName("com.alibaba.druid:type=DruidDataSource"));
|
||||
|
||||
try {
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Reference in New Issue
Block a user