Skip to content

Commit e8167a8

Browse files
committed
Add support for all connection pool config options. Fixes #27.
1 parent 4df1e1e commit e8167a8

File tree

2 files changed

+159
-3
lines changed

2 files changed

+159
-3
lines changed

README.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ Copy the following files into the `TOMCAT_BASE/lib` directory:
7272

7373
Reboot the server, and sessions should now be stored in Redis.
7474

75+
Connection Pool Configuration
76+
-----------------------------
77+
78+
All of the configuration options from both `org.apache.commons.pool2.impl.GenericObjectPoolConfig` and `org.apache.commons.pool2.impl.BaseObjectPoolConfig` are also configurable for the Redis connection pool used by the session manager. To configure any of these attributes (e.g., `maxIdle` and `testOnBorrow`) just use the config attribute name prefixed with `connectionPool` (e.g., `connectionPoolMaxIdle` and `connectionPoolTestOnBorrow`) and set the desired value in the `<Manager>` declaration in your Tomcat context.xml.
79+
7580
Session Change Tracking
7681
-----------------------
7782

src/main/java/com/radiadesign/catalina/session/RedisSessionManager.java

Lines changed: 154 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import org.apache.catalina.Session;
1111
import org.apache.catalina.session.ManagerBase;
1212

13+
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
14+
import org.apache.commons.pool2.impl.BaseObjectPoolConfig;
15+
1316
import redis.clients.util.Pool;
1417
import redis.clients.jedis.JedisPool;
1518
import redis.clients.jedis.JedisSentinelPool;
@@ -44,6 +47,7 @@ public class RedisSessionManager extends ManagerBase implements Lifecycle {
4447
Set<String> sentinelSet = null;
4548

4649
protected Pool<Jedis> connectionPool;
50+
protected JedisPoolConfig connectionPoolConfig = new JedisPoolConfig();
4751

4852
protected RedisSessionHandlerValve handlerValve;
4953
protected ThreadLocal<RedisSession> currentSession = new ThreadLocal<>();
@@ -579,16 +583,15 @@ public void processExpires() {
579583

580584
private void initializeDatabaseConnection() throws LifecycleException {
581585
try {
582-
// TODO: Allow configuration of pool (such as size...)
583586
if (getSentinelMaster() != null) {
584587
Set<String> sentinelSet = getSentinelSet();
585588
if (sentinelSet != null && sentinelSet.size() > 0) {
586-
connectionPool = new JedisSentinelPool(getSentinelMaster(), sentinelSet, new JedisPoolConfig(), getTimeout(), getPassword());
589+
connectionPool = new JedisSentinelPool(getSentinelMaster(), sentinelSet, this.connectionPoolConfig, getTimeout(), getPassword());
587590
} else {
588591
throw new LifecycleException("Error configuring Redis Sentinel connection pool: expected both `sentinelMaster` and `sentiels` to be configured");
589592
}
590593
} else {
591-
connectionPool = new JedisPool(new JedisPoolConfig(), getHost(), getPort(), getTimeout(), getPassword());
594+
connectionPool = new JedisPool(this.connectionPoolConfig, getHost(), getPort(), getTimeout(), getPassword());
592595
}
593596
} catch (Exception e) {
594597
e.printStackTrace();
@@ -613,4 +616,152 @@ private void initializeSerializer() throws ClassNotFoundException, IllegalAccess
613616
}
614617
serializer.setClassLoader(classLoader);
615618
}
619+
620+
621+
// Connection Pool Config Accessors
622+
623+
// - from org.apache.commons.pool2.impl.GenericObjectPoolConfig
624+
625+
public int getConnectionPoolMaxTotal() {
626+
return this.connectionPoolConfig.getMaxTotal();
627+
}
628+
629+
public void setConnectionPoolMaxTotal(int connectionPoolMaxTotal) {
630+
this.connectionPoolConfig.setMaxTotal(connectionPoolMaxTotal);
631+
}
632+
633+
public int getConnectionPoolMaxIdle() {
634+
return this.connectionPoolConfig.getMaxIdle();
635+
}
636+
637+
public void setConnectionPoolMaxIdle(int connectionPoolMaxIdle) {
638+
this.connectionPoolConfig.setMaxIdle(connectionPoolMaxIdle);
639+
}
640+
641+
public int getConnectionPoolMinIdle() {
642+
return this.connectionPoolConfig.getMinIdle();
643+
}
644+
645+
public void setConnectionPoolMinIdle(int connectionPoolMinIdle) {
646+
this.connectionPoolConfig.setMinIdle(connectionPoolMinIdle);
647+
}
648+
649+
650+
// - from org.apache.commons.pool2.impl.BaseObjectPoolConfig
651+
652+
public boolean getLifo() {
653+
return this.connectionPoolConfig.getLifo();
654+
}
655+
public void setLifo(boolean lifo) {
656+
this.connectionPoolConfig.setLifo(lifo);
657+
}
658+
public long getMaxWaitMillis() {
659+
return this.connectionPoolConfig.getMaxWaitMillis();
660+
}
661+
662+
public void setMaxWaitMillis(long maxWaitMillis) {
663+
this.connectionPoolConfig.setMaxWaitMillis(maxWaitMillis);
664+
}
665+
666+
public long getMinEvictableIdleTimeMillis() {
667+
return this.connectionPoolConfig.getMinEvictableIdleTimeMillis();
668+
}
669+
670+
public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
671+
this.connectionPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
672+
}
673+
674+
public long getSoftMinEvictableIdleTimeMillis() {
675+
return this.connectionPoolConfig.getSoftMinEvictableIdleTimeMillis();
676+
}
677+
678+
public void setSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis) {
679+
this.connectionPoolConfig.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
680+
}
681+
682+
public int getNumTestsPerEvictionRun() {
683+
return this.connectionPoolConfig.getNumTestsPerEvictionRun();
684+
}
685+
686+
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
687+
this.connectionPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
688+
}
689+
690+
public boolean getTestOnCreate() {
691+
return this.connectionPoolConfig.getTestOnCreate();
692+
}
693+
694+
public void setTestOnCreate(boolean testOnCreate) {
695+
this.connectionPoolConfig.setTestOnCreate(testOnCreate);
696+
}
697+
698+
public boolean getTestOnBorrow() {
699+
return this.connectionPoolConfig.getTestOnBorrow();
700+
}
701+
702+
public void setTestOnBorrow(boolean testOnBorrow) {
703+
this.connectionPoolConfig.setTestOnBorrow(testOnBorrow);
704+
}
705+
706+
public boolean getTestOnReturn() {
707+
return this.connectionPoolConfig.getTestOnReturn();
708+
}
709+
710+
public void setTestOnReturn(boolean testOnReturn) {
711+
this.connectionPoolConfig.setTestOnReturn(testOnReturn);
712+
}
713+
714+
public boolean getTestWhileIdle() {
715+
return this.connectionPoolConfig.getTestWhileIdle();
716+
}
717+
718+
public void setTestWhileIdle(boolean testWhileIdle) {
719+
this.connectionPoolConfig.setTestWhileIdle(testWhileIdle);
720+
}
721+
722+
public long getTimeBetweenEvictionRunsMillis() {
723+
return this.connectionPoolConfig.getTimeBetweenEvictionRunsMillis();
724+
}
725+
726+
public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
727+
this.connectionPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
728+
}
729+
730+
public String getEvictionPolicyClassName() {
731+
return this.connectionPoolConfig.getEvictionPolicyClassName();
732+
}
733+
734+
public void setEvictionPolicyClassName(String evictionPolicyClassName) {
735+
this.connectionPoolConfig.setEvictionPolicyClassName(evictionPolicyClassName);
736+
}
737+
738+
public boolean getBlockWhenExhausted() {
739+
return this.connectionPoolConfig.getBlockWhenExhausted();
740+
}
741+
742+
public void setBlockWhenExhausted(boolean blockWhenExhausted) {
743+
this.connectionPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
744+
}
745+
746+
public boolean getJmxEnabled() {
747+
return this.connectionPoolConfig.getJmxEnabled();
748+
}
749+
750+
public void setJmxEnabled(boolean jmxEnabled) {
751+
this.connectionPoolConfig.setJmxEnabled(jmxEnabled);
752+
}
753+
public String getJmxNameBase() {
754+
return this.connectionPoolConfig.getJmxNameBase();
755+
}
756+
public void setJmxNameBase(String jmxNameBase) {
757+
this.connectionPoolConfig.setJmxNameBase(jmxNameBase);
758+
}
759+
760+
public String getJmxNamePrefix() {
761+
return this.connectionPoolConfig.getJmxNamePrefix();
762+
}
763+
764+
public void setJmxNamePrefix(String jmxNamePrefix) {
765+
this.connectionPoolConfig.setJmxNamePrefix(jmxNamePrefix);
766+
}
616767
}

0 commit comments

Comments
 (0)