Skip to content

Commit 90e3d05

Browse files
nnmehtaQuChen88
authored andcommitted
Create connection pool without relying on Lettuce methods
1 parent 98a5038 commit 90e3d05

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

wrapper/src/main/java/software/amazon/jdbc/plugin/cache/CacheConnection.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
import java.util.Properties;
1717
import java.util.concurrent.locks.ReentrantLock;
1818
import java.util.logging.Logger;
19-
import io.lettuce.core.support.ConnectionPoolSupport;
19+
import org.apache.commons.pool2.BasePooledObjectFactory;
2020
import org.apache.commons.pool2.impl.GenericObjectPool;
2121
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
22+
import org.apache.commons.pool2.impl.DefaultPooledObject;
23+
import org.apache.commons.pool2.PooledObject;
2224
import software.amazon.jdbc.PropertyDefinition;
2325
import software.amazon.jdbc.util.StringUtils;
2426

@@ -117,27 +119,29 @@ private void createConnectionPool(boolean isRead) {
117119
.withSsl(useSSL).withVerifyPeer(false).withLibraryName("aws-jdbc-lettuce").build();
118120

119121
RedisClient client = RedisClient.create(resources, redisUriCluster);
120-
GenericObjectPool<StatefulRedisConnection<byte[], byte[]>> pool =
121-
ConnectionPoolSupport.createGenericObjectPool(
122-
() -> {
123-
StatefulRedisConnection<byte[], byte[]> connection = client.connect(new ByteArrayCodec());
124-
// In cluster mode, we need to send READONLY command to the server for reading from replica.
125-
// Note: we gracefully ignore ERR reply to support non cluster mode.
126-
if (isRead) {
127-
try {
128-
connection.sync().readOnly();
129-
} catch (RedisCommandExecutionException e) {
130-
if (e.getMessage().contains("ERR This instance has cluster support disabled")) {
131-
LOGGER.fine("------ Note: this cache cluster has cluster support disabled ------");
132-
} else {
133-
throw e;
134-
}
122+
GenericObjectPool<StatefulRedisConnection<byte[], byte[]>> pool = new GenericObjectPool<>(
123+
new BasePooledObjectFactory<StatefulRedisConnection<byte[], byte[]>>() {
124+
public StatefulRedisConnection<byte[], byte[]> create() {
125+
StatefulRedisConnection<byte[], byte[]> connection = client.connect(new ByteArrayCodec());
126+
// In cluster mode, we need to send READONLY command to the server for reading from replica.
127+
// Note: we gracefully ignore ERR reply to support non cluster mode.
128+
if (isRead) {
129+
try {
130+
connection.sync().readOnly();
131+
} catch (RedisCommandExecutionException e) {
132+
if (e.getMessage().contains("ERR This instance has cluster support disabled")) {
133+
LOGGER.fine("------ Note: this cache cluster has cluster support disabled ------");
134+
} else {
135+
throw e;
135136
}
136137
}
137-
return connection;
138-
},
139-
poolConfig
140-
);
138+
}
139+
return connection;
140+
}
141+
public PooledObject<StatefulRedisConnection<byte[], byte[]>> wrap(StatefulRedisConnection<byte[], byte[]> connection) {
142+
return new DefaultPooledObject<>(connection);
143+
}
144+
}, poolConfig);
141145

142146
if (isRead) {
143147
readConnectionPool = pool;

0 commit comments

Comments
 (0)