Skip to content

Commit 464cb13

Browse files
fix: replace deprecated returnResource() and add JedisPool shutdown hook (#1188)
Two resource management issues fixed: 1. returnConnection() used jedisPool.returnResource() which has been deprecated since Jedis 3.0. Replaced with jedis.close() which correctly returns healthy connections to the pool and closes broken ones. 2. JedisPool was never closed, leaving open connections and background threads after process exit. Added closePool() and a JVM shutdown hook to ensure the pool is always properly released on shutdown. Co-authored-by: Claude <claude@anthropic.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent ddd4c73 commit 464cb13

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

platform-core/platform-cache/src/main/scala/org/sunbird/cache/util/RedisConnector.scala

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.sunbird.cache.util
22

33
import org.sunbird.common.Platform
4+
import org.sunbird.telemetry.logger.TelemetryManager
45
import redis.clients.jedis.{Jedis, JedisPool, JedisPoolConfig}
56

67
/**
@@ -14,6 +15,8 @@ trait RedisConnector {
1415
private val INDEX = Platform.getInteger("redis.dbIndex", 0)
1516
private val jedisPool: JedisPool = new JedisPool(getConfig(), HOST, PORT)
1617

18+
registerShutdownHook()
19+
1720
/**
1821
* This Method Returns a connection object from connection pool.
1922
*
@@ -28,21 +31,45 @@ trait RedisConnector {
2831
}
2932

3033
/**
31-
* This Method takes a connection object and put it back to pool.
34+
* This Method takes a connection object and returns it to pool.
35+
* Uses jedis.close() which is the modern, non-deprecated approach
36+
* and correctly returns the connection to the pool (or closes it if broken).
3237
*
3338
* @param jedis
3439
*/
3540
protected def returnConnection(jedis: Jedis): Unit = {
36-
try if (null != jedis) jedisPool.returnResource(jedis)
37-
catch {
38-
case e: Exception => throw e
41+
if (null != jedis) {
42+
try jedis.close()
43+
catch {
44+
case e: Exception => TelemetryManager.error("Error returning Redis connection to pool: " + e.getMessage, e)
45+
}
46+
}
47+
}
48+
49+
/**
50+
* Closes the JedisPool, releasing all connections.
51+
* Called automatically via the JVM shutdown hook.
52+
*/
53+
def closePool(): Unit = {
54+
if (jedisPool != null && !jedisPool.isClosed) {
55+
try jedisPool.close()
56+
catch {
57+
case e: Exception => TelemetryManager.error("Error closing JedisPool: " + e.getMessage, e)
58+
}
3959
}
4060
}
4161

62+
private def registerShutdownHook(): Unit = {
63+
Runtime.getRuntime.addShutdownHook(new Thread(() => {
64+
TelemetryManager.log("Shutting down RedisConnector — closing connection pool")
65+
closePool()
66+
}))
67+
}
68+
4269
private def getConfig(): JedisPoolConfig = {
4370
val config: JedisPoolConfig = new JedisPoolConfig()
4471
config.setMaxTotal(MAX_CONNECTIONS)
45-
config.setBlockWhenExhausted(true);
72+
config.setBlockWhenExhausted(true)
4673
config
4774
}
4875
}

0 commit comments

Comments
 (0)