开发者社区> 问答> 正文

使用jedis 报错ERR EXEC without MULTI?报错


我的RedisClient.java类

@Component
public class RedisDaoSupport{
	private static Logger logger = LoggerFactory.getLogger(RedisDaoSupport.class);

	@Autowired
	private JedisPool jedisPool;

    /**
     * 添加key value
     * @param key
     * @param value
     */
	public void set(String key,String value){
		Jedis jedis = null;
		try{
			jedis = jedisPool.getResource();
			jedis.set(key, value);
		}
		catch(Exception e){
			jedisPool.returnBrokenResource(jedis);
			logger.error(e.getMessage());
		}
		finally{
			jedisPool.returnResource(jedis);
		}
	}

    /**
     * 获取redis value (String)
     * @param key
     * @return
     */
	public String get(String key){
		Jedis jedis = null;
		try{
			jedis = jedisPool.getResource();
			return jedis.get(key);
		}
		catch(Exception e){
			jedisPool.returnBrokenResource(jedis);
			logger.error(e.getMessage());
		}
		finally{
			jedisPool.returnResource(jedis);
		}
		return "";
	}

	/**
	 * 模糊查询所有keys 遍历所有集群redis节点,性能会比较慢
	 * @param pattern
	 * @return
	 */
	public Set<String> keys(String pattern){
		Jedis jedis = null;
		try{
			jedis = jedisPool.getResource();
			Set<String> set = jedis.keys(pattern);
			return set;
		}
		catch(Exception e){
			jedisPool.returnBrokenResource(jedis);
			logger.error(e.getMessage());
		}
		finally{
			jedisPool.returnResource(jedis);
		}
		return null;
	}

public Transaction multi(){
		Jedis jedis = null;
		try{
			jedis = jedisPool.getResource();
			Transaction tx = jedis.multi();
			return tx;
		}
		catch(Exception e){
			jedisPool.returnBrokenResource(jedis);
			logger.error(e.getMessage());
		}
		finally{
			jedisPool.returnResource(jedis);
		}
		return null;
	}

}



在其他类中调用

@Autowired
	RedisDaoSupport redisDao;


	    String key = "XXXX";
	    Set<String> set = redisDao.keys(key);
	    Transaction tx = redisDao.multi();//事务处理
		for (String s : set) {
			tx.set(s, msg);//更新短信内容
		}
		tx.exec();//提交事务



相关的代码就这些,其他方式都能运行,但是写事务就报错

求助!!!!


展开
收起
爱吃鱼的程序员 2020-06-14 20:04:29 1018 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    jedis2.2.1没这个问题,但jedis2.5.2有

    2020-06-14 20:04:48
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spark SQL: Past, Present and Future 立即下载
Spark SQL:Past Present &Future 立即下载
How to Build a Successful Data 立即下载