Jedis中watch监控对象在exec之前修改值后事务并未中断

程序猿老高
博客专家认证
2020-08-29 05:05:37
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

public class Test {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.18.130", 6379);
int balance;// 余额
int debt;// 欠额
int expense = 100;// 实刷额度

jedis.watch("balance");
balance = Integer.parseInt(jedis.get("balance"));
if (balance < expense) {
System.out.println("信用卡余额不足!");
jedis.unwatch();
jedis.close();
return;
}

System.out.println("*****begin*****");
Transaction transaction = jedis.multi();
try {
transaction.decrBy("balance", expense);
transaction.incrBy("debt", expense);
try {
Thread.sleep(10000);// 此时balance的值被修改,但本次事务没有中断
} catch (Exception e) {
e.printStackTrace();
}
transaction.exec();
balance = Integer.parseInt(jedis.get("balance"));
debt = Integer.parseInt(jedis.get("debt"));

System.out.println("balance=" + balance);
System.out.println("debt=" + debt);
System.out.println("*****end*****");
} catch (Exception e) {
e.printStackTrace();
transaction.discard();
}finally {
jedis.unwatch();
jedis.close();
}
}
}
...全文
9639 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
天一i 2021-07-07
  • 打赏
  • 举报
回复

解决了吗?
我也没中断

街头小贩 2020-09-11
  • 打赏
  • 举报
回复
难道应该 中断吗?
transaction.exec();
开始后才提交事务
天一i 2021-07-07
  • 举报
回复
@街头小贩 正常提交事务不应该失败吗?

10,607

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧