当前位置: 首页 > 知识库问答 >
问题:

如何利用Spring-data-Redis实现Redis多执行

松国兴
2023-03-14
@Autowired
@Qualifier("stringRedisTemplate")
StringRedisTemplate template;

void test(){
  template.multi();
  template.boundValueOps("somevkey").increment(1);
  template.boundZSetOps("somezkey").add("zvalue", timestamp);
  template.exec();
}
    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

我使用spring-data-redis 1.0.0。

共有1个答案

梁承恩
2023-03-14

查看org.springframework.data.redis.support.collections.collectionutils.rename(K,K,redisoperations ) 的实现

并在此阅读交易详细信息

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循环很重要

 类似资料:
  • 我试图与一个springboot应用程序连接到2个不同的redis实例:一个用作数据库,一个只用作缓存。我添加了不同的连接工厂和不同名称的redis模板,我使用@Qualifer来链接它们。我试图禁用从自动配置类RedisAutoConfiguration,但没有工作。 我总是收到这个错误: 包装人:org。springframework。豆。工厂UnsatifiedPendencyExcepti

  • 我目前使用Redis(3.2.100)和Spring data Redis(1.8.9)以及Jedis Connector。当我在现有实体上使用save()函数时,Redis删除我的实体并重新创建该实体。 有另一种方法更新实体redis之前不删除? 谢谢

  • Spring Data Redis, 是 Spring Data 家族的子项目。它提供了 Spring 应用对 Redis 的简单配置和访问。低级和高级的抽象用于存储,使用户无需考虑考虑基础。

  • 问题内容: 我使用spring-data-redis版本1.7.0.M1和jedis版本2.8.0 并使用【redisTemplate.opsForValue()。get(“ foo”)】进行测试 抛出异常 使用spring-data-redis 1.7.0.M1时如何配置redis-cluster? 问题答案: 基本上,所需要做的只是在中设置群集节点的初始集合,并将其提供给或。 spring启动

  • 本文向大家介绍如何在 Java 中利用 redis 实现 LBS 服务,包括了如何在 Java 中利用 redis 实现 LBS 服务的使用技巧和注意事项,需要的朋友参考一下 前言 LBS(基于位置的服务) 服务是现在移动互联网中比较常用的功能。例如外卖服务中常用的我附近的店铺的功能,通常是以用户当前的位置坐标为基础,查询一定距离范围类的店铺,按照距离远近进行倒序排序。 自从 redis 4 版本

  • 本文向大家介绍利用Redis如何实现自动补全功能,包括了利用Redis如何实现自动补全功能的使用技巧和注意事项,需要的朋友参考一下 忘了redis从哪个版本开启,能够根据输入的部分命令前缀给出提示,即自动补全。接下来笔者介绍基于redis实现这个很酷的功能。 about sorted set 假设结果中有mara,marabel,marcela。现在我们输入mar,就能得到这三个名字,并且输出结果