源码文件:DruidDataSource源码
1571行 private DruidPooledConnection getConnectionInternal(long maxWait) throws SQLException {}
在这个getConnectionInternal方法 for循环中如果 createDirect为true则直接创建物理连接。
而这个createDirect一开始为false,当满足如下条件的时候才会被置为true
if (createScheduler != null
&& poolingCount == 0
&& activeCount < maxActive
&& creatingCountUpdater.get(this) == 0
&& createScheduler instanceof ScheduledThreadPoolExecutor) {
ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) createScheduler;
if (executor.getQueue().size() > 0) {
createDirect = true;
continue;
}
}
这段代码表达的意思没理解,求解释。
主要是 为什么要判断 executor.getQueue().size>0, 线程池的任务队列中有任务才会直接创建?为啥有任务的时候才需要直接创建? 有任务的时候往队列中添加创建任务不可以吗?
问题2: 在for 中如果createDirect为true 且cas成功,会创建物理连接,如下
PhysicalConnectionInfo pyConnInfo = DruidDataSource.this.createPhysicalConnection();
holder = new DruidConnectionHolder(this, pyConnInfo);
然后在下面的代码又会执行
if (maxWait > 0) {
holder = pollLast(nanos);
} else {
holder = takeLast();
}
这不会导致 先创阿金的DruidConnectionHolder被丢弃了吗?