-1

I'm using jedis client to interact with the Redis database. I'm using the code like the following as mentioned here in the docs. I'm reusing the jedisPooled repeatedly in various places to add keys and values. I want to know whether the following method is thread-safe or not.

import redis.clients.jedis.JedisPooled

val jedisPooled =  JedisPooled("localhost", 6379);

jedisPooled.set("enabled","true)

It is mentioned here that connection pools are thread-safe. But I encounter exceptions like java.net.SocketException as mentioned here. I would appreciate it very much if someone could tell me what am I doing wrong?

Anandakrishnan
  • 347
  • 1
  • 4
  • 18

1 Answers1

0

JedisPooled is thread safe (as mentioned in the doc you linked).

java.net.SocketException is not due to thread safety issue. Look into the error on its own like the question you linked.

sazzad
  • 5,740
  • 6
  • 25
  • 42
  • so if I create a Jedispooled instance one time and reuse it multiple times throughout the app, it won't fail? – Anandakrishnan Nov 25 '22 at 20:26
  • No. but pool size should be good enough for your entire app – sazzad Nov 26 '22 at 04:56
  • Are you referring to max connections? I'm not following what you mean by pool size. I have changed my code to follow these instructions as [here](https://github.com/redis/jedis/wiki/Getting-started#using-jedis-in-a-multithreaded-environment) and now using jedispool and closing connections after the operation is done. I have noticed that it does mitigate the errors I was facing. – Anandakrishnan Nov 26 '22 at 15:15
  • Also I'm sorry but I didn't provide the complete error in my questions. It is similar to [this](https://stackoverflow.com/questions/64859757/redis-clients-jedis-exceptions-jedisconnectionexception-java-net-socketexceptio) . Except for broken connections mine will be something like software aborted (write failed). – Anandakrishnan Nov 26 '22 at 15:19
  • @Anandakrishnan Yes, by pool size I meant max connections. – sazzad Nov 29 '22 at 08:40
  • hey sorry for the late reply comment. This is just to be clear, I'm using the default `JedisPoolConfig` with 8 connections. Does this mean this is enough for my whole app. – Anandakrishnan Dec 03 '22 at 16:06
  • I don't know the the full load of your whole app. So I can't say. You can start with it and increase the pool size later if necessary. – sazzad Dec 03 '22 at 23:14