读书人

备忘应用spring-data-redis中的redist

发布时间: 2012-10-26 10:30:58 作者: rapoo

备忘使用spring-data-redis中的redistemplate的一个大坑
在项目开发过程中,想要进行redis的并发控制,这时候,想当然地使用了spring-data-redis库中template里面提供的multi()和exec()方法,但是蛋疼地发现,使用了之后,就出现了如下异常:

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

读书人网 >软件架构设计

热点推荐