mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Add an enhanced StickyRandomDataSourceSelector
This selector can stick a DataSource to a Thread in a short time.
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package com.alibaba.druid.pool.ha.selector;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* A class holding DataSource reference and retrieving time.
|
||||
*
|
||||
* @author DigitalSonic
|
||||
*/
|
||||
public class StickyDataSourceHolder {
|
||||
private long retrievingTime = System.currentTimeMillis();
|
||||
private DataSource dataSource;
|
||||
|
||||
public StickyDataSourceHolder() {
|
||||
}
|
||||
|
||||
public StickyDataSourceHolder(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return retrievingTime > 0 && dataSource != null;
|
||||
}
|
||||
|
||||
public long getRetrievingTime() {
|
||||
return retrievingTime;
|
||||
}
|
||||
|
||||
public void setRetrievingTime(long retrievingTime) {
|
||||
this.retrievingTime = retrievingTime;
|
||||
}
|
||||
|
||||
public DataSource getDataSource() {
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.alibaba.druid.pool.ha.selector;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ha.HighAvailableDataSource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* An extend selector based on RandomDataSourceSelector which can stick a DataSource to a Thread in a while.
|
||||
*
|
||||
* @author DigitalSonic
|
||||
* @see RandomDataSourceSelector
|
||||
* @see StickyDataSourceHolder
|
||||
*/
|
||||
public class StickyRandomDataSourceSelector extends RandomDataSourceSelector {
|
||||
private ThreadLocal<StickyDataSourceHolder> holders = new ThreadLocal<StickyDataSourceHolder>();
|
||||
|
||||
private int expireSeconds = 5;
|
||||
|
||||
public StickyRandomDataSourceSelector(HighAvailableDataSource highAvailableDataSource) {
|
||||
super(highAvailableDataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource get() {
|
||||
StickyDataSourceHolder holder = holders.get();
|
||||
if (holder != null && isValid(holder) && !isExpired(holder)) {
|
||||
return holder.getDataSource();
|
||||
}
|
||||
DataSource dataSource = super.get();
|
||||
holder = new StickyDataSourceHolder(dataSource);
|
||||
holders.remove();
|
||||
holders.set(holder);
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
private boolean isValid(StickyDataSourceHolder holder) {
|
||||
boolean flag = holder.isValid() && !getBlacklist().contains(holder.getDataSource());
|
||||
if (!(holder.getDataSource() instanceof DruidDataSource) || !flag) {
|
||||
return flag;
|
||||
}
|
||||
DruidDataSource dataSource = (DruidDataSource) holder.getDataSource();
|
||||
return flag && dataSource.getActiveCount() < dataSource.getMaxActive();
|
||||
}
|
||||
|
||||
private boolean isExpired(StickyDataSourceHolder holder) {
|
||||
return System.currentTimeMillis() - holder.getRetrievingTime() > expireSeconds * 1000;
|
||||
}
|
||||
|
||||
public int getExpireSeconds() {
|
||||
return expireSeconds;
|
||||
}
|
||||
|
||||
public void setExpireSeconds(int expireSeconds) {
|
||||
this.expireSeconds = expireSeconds;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user