mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
smart select lock fair or unfair
This commit is contained in:
@@ -38,6 +38,7 @@ import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -220,13 +221,18 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
|
||||
protected boolean useOracleImplicitCache = true;
|
||||
|
||||
protected final ReentrantLock lock;
|
||||
protected ReentrantLock lock;
|
||||
protected Condition notEmpty;
|
||||
protected Condition empty;
|
||||
|
||||
protected AtomicLong createCount = new AtomicLong();
|
||||
protected AtomicLong destroyCount = new AtomicLong();
|
||||
|
||||
public DruidAbstractDataSource(boolean lockFair){
|
||||
lock = new ReentrantLock(lockFair);
|
||||
|
||||
notEmpty = lock.newCondition();
|
||||
empty = lock.newCondition();
|
||||
}
|
||||
|
||||
public boolean isOracle() {
|
||||
@@ -788,6 +794,20 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
return;
|
||||
}
|
||||
|
||||
if (maxWaitMillis > 0 && !this.inited) {
|
||||
final ReentrantLock lock = this.lock;
|
||||
lock.lock();
|
||||
try {
|
||||
if ((!this.inited) && (!lock.isFair())) {
|
||||
this.lock = new ReentrantLock(true);
|
||||
this.notEmpty = this.lock.newCondition();
|
||||
this.empty = this.lock.newCondition();
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
if (inited) {
|
||||
LOG.error("maxWait changed : " + this.maxWait + " -> " + maxWaitMillis);
|
||||
}
|
||||
|
||||
@@ -32,7 +32,6 @@ import java.util.ServiceLoader;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
import javax.management.JMException;
|
||||
@@ -83,9 +82,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final Condition notEmpty = lock.newCondition();
|
||||
private final Condition empty = lock.newCondition();
|
||||
|
||||
// stats
|
||||
private long connectCount = 0L;
|
||||
private long closeCount = 0L;
|
||||
@@ -127,7 +123,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
private JdbcDataSourceStat dataSourceStat;
|
||||
|
||||
public DruidDataSource(){
|
||||
this(true);
|
||||
this(false);
|
||||
}
|
||||
|
||||
public DruidDataSource(boolean fairLock){
|
||||
|
||||
Reference in New Issue
Block a user