diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/StickyDataSourceHolder.java b/src/main/java/com/alibaba/druid/pool/ha/selector/StickyDataSourceHolder.java new file mode 100644 index 0000000000..e69659e85d --- /dev/null +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/StickyDataSourceHolder.java @@ -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; + } +} diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/StickyRandomDataSourceSelector.java b/src/main/java/com/alibaba/druid/pool/ha/selector/StickyRandomDataSourceSelector.java new file mode 100644 index 0000000000..bd871a9a3b --- /dev/null +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/StickyRandomDataSourceSelector.java @@ -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 holders = new ThreadLocal(); + + 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; + } +}