The import redis. Clients. Jedis. Transaction;
Public class Test {
Public static void main (String [] args) {
Jedis Jedis=new Jedis (" 192.168.18.130 ", 6379);
Int the balance;//the balance
Int debt;//in
Int expense=100;//real brush lines
Jedis. Watch (" balance ");
The balance=Integer. ParseInt (jedis. Get (" balance "));
If (the balance & lt; Expense) {
System. Out.println (" credit card balances shortage!" );
Jedis. Unwatch ();
jedis.close();
return;
}
System. The out. Println (" * * * * * the begin * * * * * ");
The Transaction Transaction=jedis. Multi ();
Try {
Transaction. DecrBy (" balance ", expense);
Transaction. IncrBy (" debt ", expense);
Try {
Thread.sleep (10000);// the balance value is modified at this time, but the transaction did not interrupt ,
} the catch (Exception e) {
e.printStackTrace();
}
Transaction. The exec ();
The balance=Integer. ParseInt (jedis. Get (" balance "));
Debt=Integer. ParseInt (jedis. Get (" debt "));
System. The out. Println (" balance="+ balance);
System. The out. Println (" debt="+ debt);
System. The out. Println (" * * * * * end * * * * * ");
} the catch (Exception e) {
e.printStackTrace();
Transaction. The discard ();
} the finally {
Jedis. Unwatch ();
jedis.close();
}
}
}
CodePudding user response:
Don't should interrupt?transaction. The exec ();began to commit the transaction