Uploaded image for project: 'Spring Data Redis'
  1. Spring Data Redis
  2. DATAREDIS-1044

RedisTemplate happened thread-unsafe case,maybe my code worng but i'dnot know where and why

    Details

    • Type: Bug
    • Status: Open
    • Priority: Critical
    • Resolution: Unresolved
    • Affects Version/s: 1.8.23 (Ingalls SR23)
    • Fix Version/s: None
    • Component/s: Core
    • Labels:
      None
    • Environment:
      linux + jdk8

      Description

      i try use RedisTemplate to implement a distributed lock, then i got some exception it's seem like thread-unsafe case.

      java.lang.ClassCastException: java.lang.Long cannot be cast to [B
              at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:181) ~[jedis-2.6.2.jar:?]
              at redis.clients.jedis.Jedis.set(Jedis.java:79) ~[jedis-2.6.2.jar:?]
              at com.xxx.locks.DistributedLock$1.doInRedis(DistributedLock.java:45) ~[fop-pdbds-core-baseinfo-common-1.0-SNAPSHOT.jar:?]
              at com.xxx.baseinfo.locks.DistributedLock$1.doInRedis(DistributedLock.java:39) ~[fop-pdbds-core-baseinfo-common-1.0-SNAPSHOT.jar:?]
              at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:190) ~[spring-data-redis-1.4.2.RELEASE.jar:1.4.2.RELEASE]
              at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:152) ~[spring-data-redis-1.4.2.RELEASE.jar:1.4.2.RELEASE]
              at com.xxx.locks.DistributedLock.lock(DistributedLock.java:39) [fop-pdbds-core-baseinfo-common-1.0-SNAPSHOT.jar:?]
              at com.xxx.kafka.consumer.EmployeeDataConsumerListener.onMessage(EmployeeDataConsumerListener.java:70) [fop-pdbds-core-baseinfo-service-1.0-SNAPSHOT.jar:?]
              at com.sf.kafka.api.consume.KafkaConsumer$ConsumeTask.run(KafkaConsumer.java:247) [sf-kafka-api-core-1.15.1.jar:?]
              at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_172-ea]
              at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_172-ea]

      code:

      public boolean lock(String key, String requestId) {
              String lockResult = "";
              try {
                  lockResult = (String) iRedisCache.getRedisTemplate().execute(new RedisCallback<String>() {
                      @Override
                      public String doInRedis(RedisConnection redisConnection) {
                          Object nativeConnection = redisConnection.getNativeConnection();
                          String result = "";
                          if (nativeConnection instanceof JedisCommands)

      {                         result = ((JedisCommands) nativeConnection).set(key, requestId, "NX", "EX",                                 DISTRIBUTEDLOCK_TIME);                     }

                          return result;
                      }
                  }, true);
              } catch (Exception e)

      {                    }

              return "OK".equals(lockResult);
          }
          public void releaseLock(String key, String requestId) {
              try {
                  iRedisCache.getRedisTemplate().execute(new RedisCallback<String>() {
                      @Override
                      public String doInRedis(RedisConnection redisConnection)

      {                     Jedis jedis = (Jedis) redisConnection.getNativeConnection();                     final String script = "if redis.call(\"get\",\"" + key + "\") == \"" + requestId + "\"then  " +                             "return redis.call(\"del\",\"" + key + "\") else return 0 end ";                     Long effectRows = (Long) jedis.eval(script);                     return "OK";                 }

                  }, true);
              } catch (Exception e)

      {                     }

          }

      thks

        Attachments

          Activity

            People

            • Assignee:
              cstrobl Christoph Strobl
              Reporter:
              spring4me Jianhui
              Last updater:
              Jianhui
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: