Spring Redis 事务Multi Exec Watch

本文探讨了使用Spring Data Redis进行Multi-Exec操作时遇到的异常问题,并提供了可能的解决方案,包括通过回调方式确保使用同一连接进行.multi()和.exec()操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

今天在测试spring Redis的时候出现这么个:

org.springframework.dao.InvalidDataAccessApiUsageException: ERR EXEC without MULTI; nested exception is redis.clients.jedis.exceptions.JedisDataException: ERR EXEC without MULTI 

另外还报: java.lang.NullPointerException

先记录下, http://stackoverflow.com/questions/11148383/how-to-implement-redis-multi-exec-by-using-spring-data-redis All Spring Configuration is written properly. Non-Multi-Exec Redis operations work perfectly. @Autowired @Qualifier("stringRedisTemplate") StringRedisTemplate template; void test(){   template.multi();   template.boundValueOps("somevkey").increment(1);   template.boundZSetOps("somezkey").add("zvalue", timestamp);   template.exec(); } After running above code through Junit Test, Exceptions are thrown. Unknown exception; nested exception is org.springframework.data.redis.RedisSystemException: Unknown jedis exception; nested exception is java.lang.NullPointerException The reason for the exception is probably that the Spring template implementation does not reuse the same connection for .multi() and .exec(). You can try to use execute() via a callback: 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; });

先记录下,希望明天去公司能搞定 http://forum.springsource.org/archive/index.php/t-125387.html Hello! I am getting very many exceptions and strange results using redisTemplate and it's callbacks. Problems we spotted in:

Multi-Discard using RedisCallback Multi-Exec using RedisCallback Watch-Multi-Exec using RedisCallback Multi-Exec in pipelined RedisCallback Watch-Multi-Unwatch-Exec using RedisCallback Also two bugs we spotted and added to JIRA: DATAREDIS-87, DATAREDIS-88 For some reason I was not able to attach file with code. Will email it on demand. Sample:

@Test public void testMultiDiscard1() throws Exception { redisTemplate.execute(new RedisCallback<Object>() { @Override public Object doInRedis(RedisConnection connection) throws DataAccessException { connection.set("a".getBytes(), "b".getBytes()); assertEquals(1, redisTemplate.keys("a").size()); connection.multi(); assertEquals(1, redisTemplate.keys("a").size()); connection.set("a".getBytes(), "bbb".getBytes()); connection.append("a".getBytes(), "ccc".getBytes()); connection.hSet("a1".getBytes(), "field1".getBytes(), "1000".getBytes()); connection.hIncrBy("a1".getBytes(), "field1".getBytes(), 10); Map<byte[], byte[]> args = new HashMap<byte[], byte[]>(3); args.put("field2".getBytes(), "valueX".getBytes()); args.put("field3".getBytes(), "valueY".getBytes()); args.put("field4".getBytes(), "000".getBytes()); connection.hMSet("a2".getBytes(), args); connection.append("a3".getBytes(), "bunny".getBytes()); assertEquals(1, redisTemplate.keys("a").size()); assertTrue(redisTemplate.keys("a1").isEmpty()); connection.discard(); return null; } }, true); assertNotNull(redisTemplate.opsForValue().get("a")); assertTrue(redisTemplate.opsForHash().values("a1").isEmpty()); }

I was working on a redis mock to be able to test without real redis. When run the tests on real one quite a big number of them failed, but shouldn't (as for me). Maybe I'm doing smth wrong? Thanks in advance!

Building Applications with Spring Data Redis http://www.packtpub.com/article/building-applications-spring-data-redis

 

 
 
 
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值