13

所有 Spring 配置均已正确编写。非 Multi-Exec Redis 操作完美运行。

@Autowired
@Qualifier("stringRedisTemplate")
StringRedisTemplate template;

void test(){
  template.multi();
  template.boundValueOps("somevkey").increment(1);
  template.boundZSetOps("somezkey").add("zvalue", timestamp);
  template.exec();
}

通过 Junit Test 运行上述代码后,抛出异常。

    org.springframework.data.redis.RedisSystemException: Unknown exception; nested exception is org.springframework.data.redis.RedisSystemException: Unknown jedis exception; nested exception is java.lang.NullPointerException
        at org.springframework.data.redis.connection.jedis.JedisUtils.convertJedisAccessException(JedisUtils.java:93)
        at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.translateExceptionIfPossible(JedisConnectionFactory.java:155)
        at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
        at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
        at $Proxy66.appendUserStream(Unknown Source)
        at com.uniu.test.repository.StreamCacheRepositoryTest.testAppendUserStream(StreamCacheRepositoryTest.java:23)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Caused by: org.springframework.data.redis.RedisSystemException: Unknown jedis exception; nested exception is java.lang.NullPointerException
        at org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:119)
        at org.springframework.data.redis.connection.jedis.JedisConnection.exec(JedisConnection.java:523)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.data.redis.core.CloseSuppressingInvocationHandler.invoke(CloseSuppressingInvocationHandler.java:58)
        at $Proxy70.exec(Unknown Source)
        at org.springframework.data.redis.core.RedisTemplate$1.doInRedis(RedisTemplate.java:416)
        at org.springframework.data.redis.core.RedisTemplate$1.doInRedis(RedisTemplate.java:412)
        at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:162)
        at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:133)
        at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:121)
        at org.springframework.data.redis.core.RedisTemplate.exec(RedisTemplate.java:412)
        at com.uniu.repository.impl.StreamCacheRepositoryRedisImpl.appendUserStream(StreamCacheRepositoryRedisImpl.java:37)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
        ... 33 more
    Caused by: java.lang.NullPointerException
        at redis.clients.jedis.BinaryTransaction.exec(BinaryTransaction.java:31)
        at org.springframework.data.redis.connection.jedis.JedisConnection.exec(JedisConnection.java:521)
        ... 54 more

我查看了redis服务器,上面两条命令都执行过了,结果是正确的。所以问题出现在代码的最后一行(template.exec())。当底层 JedisClient 尝试从 EXEC 获得多批量响应时,似乎抛出 NullPointerException

我使用 spring-data-redis 1.0.0.RELEASE 谢谢帮助。

4

4 回答 4

14

异常的原因可能是 Spring 模板实现没有为.multi()和重用相同的连接.exec()。您可以尝试execute()通过回调使用:

private RedisTemplate template = ...;

template.execute(

  new RedisCallback() {

    @Override
    public Object doInRedis(RedisConnection connection)
      throws DataAccessException {

      connection.multi();

      //do whatever you need, like deleting and repopulating some keys

      connection.expire(CHANNEL_KEY.getBytes(), EXPIRE_SECS);
      connection.exec();
      return null;
    }

  }

);
于 2012-09-01T15:26:25.687 回答
11
template.setEnableTransactionSupport(true);

也许你可以用它来启用交易

于 2016-04-21T08:37:38.843 回答
6

彼得是对的。您需要将其放入 SessionCallback 中。

以下是使用 Spring Data Redis 进行事务的快速总结:

https://github.com/eric-wu/dashboard/wiki/3.-Redis-Transactions-via-Spring-Data-Redis

对于您的情况,向下滚动并查看“使用 WATCH、MULTI 和 EXEC 进行乐观锁定”。

于 2013-10-17T20:09:06.147 回答
0

看一下实现org.springframework.data.redis.support.collections.CollectionUtils.rename(K, K, RedisOperations<K, ?>)

并在此处阅读交易详情

static <K> void rename(final K key, final K newKey, RedisOperations<K, ?> operations) {
    operations.execute(new SessionCallback<Object>() {
        @SuppressWarnings("unchecked")
        public Object execute(RedisOperations operations) throws DataAccessException {
            do {
                operations.watch(key);

                if (operations.hasKey(key)) {
                    operations.multi();
                    operations.rename(key, newKey);
                } else {
                    operations.multi();
                }
            } while (operations.exec() == null);
            return null;
        }
    });
}

注意:while循环很重要

于 2020-10-06T18:05:46.913 回答