mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
support dynamic change url、user、password
This commit is contained in:
@@ -55,8 +55,11 @@ public final class ConnectionHolder {
|
||||
private int underlyingHoldability;
|
||||
private int underlyingTransactionIsolation;
|
||||
private boolean underlyingAutoCommit;
|
||||
private final int modCount;
|
||||
|
||||
public ConnectionHolder(DruidAbstractDataSource dataSource, Connection conn) throws SQLException{
|
||||
this.modCount = dataSource.getModCount();
|
||||
|
||||
this.dataSource = dataSource;
|
||||
this.conn = conn;
|
||||
this.connecttimeMillis = System.currentTimeMillis();
|
||||
@@ -75,6 +78,10 @@ public final class ConnectionHolder {
|
||||
statementPool = null;
|
||||
}
|
||||
|
||||
public int getModCount() {
|
||||
return modCount;
|
||||
}
|
||||
|
||||
public boolean isUnderlyingReadOnly() {
|
||||
return underlyingReadOnly;
|
||||
}
|
||||
@@ -143,10 +150,10 @@ public final class ConnectionHolder {
|
||||
}
|
||||
|
||||
public boolean isPoolPreparedStatements() {
|
||||
// if (dataSource.isOracle()) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// if (dataSource.isOracle()) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
boolean poolPreparedStatements = dataSource.isPoolPreparedStatements();
|
||||
if (poolPreparedStatements) {
|
||||
boolean transaction = !isUnderlyingAutoCommit();
|
||||
|
||||
@@ -37,6 +37,7 @@ import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.management.JMException;
|
||||
@@ -213,10 +214,18 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
|
||||
protected boolean useOracleImplicitCache = true;
|
||||
|
||||
protected final ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
protected int modCount = 0;
|
||||
|
||||
public boolean isOracle() {
|
||||
return isOracle;
|
||||
}
|
||||
|
||||
protected int getModCount() {
|
||||
return modCount;
|
||||
}
|
||||
|
||||
public void setOracle(boolean isOracle) {
|
||||
if (inited) {
|
||||
throw new IllegalStateException();
|
||||
@@ -832,11 +841,21 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
}
|
||||
|
||||
public void setUsername(String user) {
|
||||
if (inited) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
lock.lock();
|
||||
try {
|
||||
if (this.username == user || (this.username != null && this.username.equals(user))) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.username = user;
|
||||
if (inited) {
|
||||
this.modCount++;
|
||||
clear();
|
||||
}
|
||||
|
||||
this.username = user;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
@@ -844,11 +863,21 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
if (inited) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
lock.lock();
|
||||
try {
|
||||
if (this.password == password || (this.password != null && this.password.equals(password))) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.password = password;
|
||||
if (inited) {
|
||||
this.modCount++;
|
||||
clear();
|
||||
}
|
||||
|
||||
this.password = password;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public Properties getConnectProperties() {
|
||||
@@ -901,12 +930,24 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
}
|
||||
|
||||
public void setUrl(String jdbcUrl) {
|
||||
if (inited) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
lock.lock();
|
||||
try {
|
||||
if (this.jdbcUrl != null && this.jdbcUrl.equals(jdbcUrl)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.jdbcUrl = jdbcUrl;
|
||||
if (inited) {
|
||||
this.modCount++;
|
||||
clear();
|
||||
}
|
||||
|
||||
this.jdbcUrl = jdbcUrl;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void clear();
|
||||
|
||||
public String getDriverClassName() {
|
||||
return driverClass;
|
||||
|
||||
@@ -28,7 +28,6 @@ import java.util.Properties;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.Reference;
|
||||
@@ -67,8 +66,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
private final Condition notEmpty = lock.newCondition();
|
||||
private final Condition empty = lock.newCondition();
|
||||
|
||||
@@ -295,7 +292,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
throw new SQLException("not support oracle driver " + driver.getMajorVersion() + "."
|
||||
+ driver.getMinorVersion());
|
||||
}
|
||||
|
||||
|
||||
if (driver.getMajorVersion() == 10 && isUseOracleImplicitCache()) {
|
||||
this.getConnectProperties().setProperty("oracle.jdbc.FreeMemoryOnEnterImplicitCache", "true");
|
||||
}
|
||||
@@ -605,17 +602,29 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
|
||||
boolean neadDestory = false;
|
||||
lock.lockInterruptibly();
|
||||
try {
|
||||
activeCount--;
|
||||
closeCount++;
|
||||
if (holder.getModCount() == this.modCount) {
|
||||
activeCount--;
|
||||
closeCount++;
|
||||
|
||||
// 第六步,加入队列中(putLast)
|
||||
putLast(holder);
|
||||
recycleCount++;
|
||||
// 第六步,加入队列中(putLast)
|
||||
putLast(holder);
|
||||
recycleCount++;
|
||||
} else {
|
||||
destroyCount++;
|
||||
activeCount--;
|
||||
closeCount++;
|
||||
neadDestory = true;
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
if (neadDestory) {
|
||||
JdbcUtils.close(conn);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
JdbcUtils.close(conn);
|
||||
|
||||
@@ -657,7 +666,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
if (this.closed) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (!this.inited) {
|
||||
return;
|
||||
}
|
||||
@@ -685,9 +694,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
enable = false;
|
||||
notEmpty.signalAll();
|
||||
notEmptySignalCount++;
|
||||
|
||||
|
||||
this.closed = true;
|
||||
|
||||
|
||||
for (Filter filter : filters) {
|
||||
filter.destory();
|
||||
}
|
||||
@@ -1176,6 +1185,28 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
final List<ConnectionHolder> evictList = new ArrayList<ConnectionHolder>();
|
||||
|
||||
lock.lock();
|
||||
try {
|
||||
for (int i = 0; i < poolingCount; ++i) {
|
||||
ConnectionHolder connection = connections[i];
|
||||
evictList.add(connection);
|
||||
}
|
||||
|
||||
poolingCount = 0;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
for (ConnectionHolder item : evictList) {
|
||||
Connection connection = item.getConnection();
|
||||
JdbcUtils.close(connection);
|
||||
destroyCount++;
|
||||
}
|
||||
}
|
||||
|
||||
public int getWaitThreadCount() {
|
||||
lock.lock();
|
||||
try {
|
||||
|
||||
@@ -46,6 +46,17 @@ public abstract class WallProvider {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void clearCache() {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
if (whiteList != null) {
|
||||
whiteList = null;
|
||||
}
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean whiteContains(String sql) {
|
||||
lock.readLock().lock();
|
||||
|
||||
Reference in New Issue
Block a user