打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
备忘使用spring
猎头职位:上海: Junior Product Manager
相关文章:,jedis 用连接池时超时返回值类型错误
自己写的一个jedis操作模板类
spring-data-redis 的序列化问题
推荐群组:Hello World 达人
更多相关推荐
SpringRedisspring-data-redis
在项目开发过程中,想要进行redis的并发控制,这时候,想当然地使用了spring-data-redis库中template里面提供的multi()和exec()方法,但是蛋疼地发现,使用了之后,就出现了如下异常:
Java代码
org.springframework.dao.InvalidDataAccessApiUsageException: ERR EXEC without MULTI; nested exception is redis.clients.jedis.exceptions.JedisDataException: ERR EXEC without MULTI
在google搜了一下,才知道,原来是目前该库的redistemplate的multi和exec方法,都是新产生连接,而非使用本来的连接,这个异常,也是由于这个原因所以才导致的。(因为新连接中,直接执行退出同步,系统肯定会去找是从哪儿开始同步的,这一找,发现没有开始同步的命令,就会抛出异常了)
这个时候,只能自己调用底层的RedisCallBack和Jedis去实现底层redis语句了。
类似这样:
Java代码
Jedis jedis = new Jedis("localhost",6379);
new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.multi();
return null;
}
}.doInRedis(new JedisConnection(jedis));
这时候肯定会有个疑问,既然这个template每次都会生成新连接,那这个multi和exec命令还有个蛋用??
官方是这么回答的:
The methods are exposed in case the connection is shared across methods. Currently we don't provide any out of the box support for connection binding but the RedisTemplate supports it - just like with the rest of the templates, one connection could be bound to the running thread and the RT will use it without creating new ones.
大致意思是这些方法目前是没有用的。等到以后template可以支持绑定connection到其他运行中的连接时,这俩方法就有用了。
参考资料
http://forum.springsource.org/showthread.php?113738-spring-data-redis-transactions
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Redis事务
PHP使用Redis的事务命令
Redis分布式事务
Redis系列~Java中使用Redis(十六)
不会正确使用Redis的同事,坑了我
四个主要的连接池介绍!
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服