mirror of https://github.com/alibaba/druid.git
userPassword Version
This commit is contained in:
parent
b5169b4264
commit
2e788bde25
|
@ -187,6 +187,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
|||
protected volatile long cachedPreparedStatementCount;
|
||||
protected volatile long cachedPreparedStatementDeleteCount;
|
||||
protected volatile long cachedPreparedStatementMissCount;
|
||||
protected volatile long userPasswordVersion;
|
||||
|
||||
private volatile FilterChainImpl filterChain;
|
||||
|
||||
|
|
|
@ -85,6 +85,7 @@ public final class DruidConnectionHolder {
|
|||
final ReentrantLock lock = new ReentrantLock();
|
||||
protected String initSchema;
|
||||
protected Socket socket;
|
||||
protected final long userPasswordVersion;
|
||||
|
||||
volatile FilterChainImpl filterChain;
|
||||
|
||||
|
@ -116,6 +117,7 @@ public final class DruidConnectionHolder {
|
|||
this.createNanoSpan = connectNanoSpan;
|
||||
this.variables = variables;
|
||||
this.globalVariables = globalVariables;
|
||||
this.userPasswordVersion = dataSource.userPasswordVersion;
|
||||
|
||||
this.connectTimeMillis = System.currentTimeMillis();
|
||||
this.lastActiveTimeMillis = connectTimeMillis;
|
||||
|
@ -468,4 +470,7 @@ public final class DruidConnectionHolder {
|
|||
return buf.toString();
|
||||
}
|
||||
|
||||
public long getUserPasswordVersion() {
|
||||
return userPasswordVersion;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -234,8 +234,8 @@ public class DruidDataSource extends DruidAbstractDataSource
|
|||
lock.lock();
|
||||
try {
|
||||
urlUserPasswordChanged = (url != null && !this.jdbcUrl.equals(url))
|
||||
|| (username != null && !this.username.equals(username))
|
||||
|| (password != null && !this.password.equals(password));
|
||||
|| (username != null && !username.equals(this.username))
|
||||
|| (password != null && !password.equals(this.password));
|
||||
|
||||
String connectUser = username != null ? username : this.username;
|
||||
if (username != null) {
|
||||
|
@ -247,6 +247,9 @@ public class DruidDataSource extends DruidAbstractDataSource
|
|||
connectProperties.put("password", connectPassword);
|
||||
}
|
||||
connectUrl = url != null ? url : this.jdbcUrl;
|
||||
if (urlUserPasswordChanged) {
|
||||
userPasswordVersion++;
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
|
@ -266,17 +269,17 @@ public class DruidDataSource extends DruidAbstractDataSource
|
|||
|
||||
lock.lock();
|
||||
try {
|
||||
if (url != null && !this.jdbcUrl.equals(url)) {
|
||||
if (url != null && !url.equals(this.jdbcUrl)) {
|
||||
this.jdbcUrl = url; // direct set url, ignore init check
|
||||
LOG.info("jdbcUrl changed");
|
||||
}
|
||||
|
||||
if (username != null && !this.username.equals(username)) {
|
||||
if (username != null && !username.equals(this.username)) {
|
||||
this.username = username; // direct set, ignore init check
|
||||
LOG.info("username changed");
|
||||
}
|
||||
|
||||
if (password != null && !this.password.equals(password)) {
|
||||
if (password != null && !password.equals(this.password)) {
|
||||
this.password = password; // direct set, ignore init check
|
||||
LOG.info("password changed");
|
||||
}
|
||||
|
@ -1920,7 +1923,9 @@ public class DruidDataSource extends DruidAbstractDataSource
|
|||
return;
|
||||
}
|
||||
|
||||
if (physicalConnection.isClosed()) {
|
||||
if (holder.userPasswordVersion < userPasswordVersion
|
||||
|| physicalConnection.isClosed()
|
||||
) {
|
||||
lock.lock();
|
||||
try {
|
||||
if (holder.active) {
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* 这个场景测试initialSize > maxActive
|
||||
*
|
||||
* @author wenshao [szujobs@hotmail.com]
|
||||
*/
|
||||
public class UserPasswordVersionTest extends TestCase {
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setMaxActive(1);
|
||||
dataSource.setMaxWait(30);
|
||||
dataSource.setInitialSize(1);
|
||||
dataSource.init();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_maxWait() throws Exception {
|
||||
DruidPooledConnection conn = dataSource.getConnection();
|
||||
assertEquals(0, conn.getConnectionHolder().getUserPasswordVersion());
|
||||
|
||||
Properties properties = new Properties();
|
||||
properties.put("druid.username", "u1");
|
||||
properties.put("druid.password", "p1");
|
||||
|
||||
dataSource.configFromProperties(properties);
|
||||
|
||||
conn.close();
|
||||
|
||||
|
||||
DruidPooledConnection conn1 = dataSource.getConnection();
|
||||
assertEquals(1, conn1.getConnectionHolder().getUserPasswordVersion());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue