惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
V
Visual Studio Blog
宝玉的分享
宝玉的分享
月光博客
月光博客
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Y
Y Combinator Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
博客园_首页
Recent Announcements
Recent Announcements
小众软件
小众软件
MongoDB | Blog
MongoDB | Blog
Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
Cisco Talos Blog
Cisco Talos Blog
L
LINUX DO - 最新话题
V2EX - 技术
V2EX - 技术
Simon Willison's Weblog
Simon Willison's Weblog
P
Palo Alto Networks Blog
PCI Perspectives
PCI Perspectives
T
Troy Hunt's Blog
Hacker News: Ask HN
Hacker News: Ask HN
S
Security Affairs
量子位
The Register - Security
The Register - Security
腾讯CDC
T
The Exploit Database - CXSecurity.com
P
Privacy & Cybersecurity Law Blog
V
Vulnerabilities – Threatpost
L
LINUX DO - 热门话题
N
News and Events Feed by Topic
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
D
Darknet – Hacking Tools, Hacker News & Cyber Security
TaoSecurity Blog
TaoSecurity Blog
Scott Helme
Scott Helme
C
Cybersecurity and Infrastructure Security Agency CISA
The Last Watchdog
The Last Watchdog
W
WeLiveSecurity
H
Hacker News: Front Page
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
NISL@THU
NISL@THU
Know Your Adversary
Know Your Adversary
C
CXSECURITY Database RSS Feed - CXSecurity.com

博客园 - 陈惟鲜的博客

win11安装mysql8 AI软件修改脚本与代码,还真不敢完全操作 linux 磁盘满了,排查 Ecelipse 安装 MAT linux 增加新磁盘 redis自身查询很慢 排查redis-benchmark prometheus监控mysql数据库 prometheus监控springboot项目配置 监控工具prometheus配置-docker版 linux 文件属性被替换修改查询并修改 ----i----------- ZonedDateTime 转为 java.util.Date docker 容器查看jvm参数配置 linux 下安装使用jmeter 执行压测 大批量订单来了由于入库慢,先缓存后通知入库 eclipse 合并错分支代码还原,合并到本分支但未push到库上 mysql 查询jason格式数据 maven打包慢,使用maven-mvnd 打包可以快一半 postman 参数化构建 批量测试 postman 常用参数例子 使用AOP实现+自定义注解 实现 缓存 如何判断redis慢了
redis 事务处理,一旦异常,则回滚
陈惟鲜的博客 · 2023-09-11 · via 博客园 - 陈惟鲜的博客

事务操作: RedisTemplate支持事务操作,您可以使用multi()exec()discard()来开启、提交或取消事务。

于是使用 

	/**
	 * redis事务测试
	 * @author 陈惟鲜
	 * @date 2023年6月10日 下午2:07:03
	 * @throws Exception
	 */
	@Test
	public void redis_transaction_test2() throws Exception {
		String pattern = "test:";
		try {
			// 事务开启
			redisTemplate.multi();
			redisTemplate.opsForValue().set(pattern + "aa", "aa123456");
			redisTemplate.opsForValue().set(pattern + "bb", "bb123456");
			// 异常
			System.out.println(5 / 0);
			// 事务提交
			redisTemplate.exec();
		} catch (Exception e) {
			// 事务回滚
			redisTemplate.discard();
			e.printStackTrace();
		}
	}

  这样执行会异常,但是事务并未回滚,

提交事务,提示

org.springframework.dao.InvalidDataAccessApiUsageException: No ongoing transaction; Did you forget to call multi
    at org.springframework.data.redis.connection.jedis.JedisConnection.exec(JedisConnection.java:466)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.springframework.data.redis.core.CloseSuppressingInvocationHandler.invoke(CloseSuppressingInvocationHandler.java:61)
    at jdk.proxy2/jdk.proxy2.$Proxy695.exec(Unknown Source)

修改为下面方式出现异常,能正常回滚

/**
     * redis事务测试
     * @author 陈惟鲜
     * @date 2023年6月10日 下午2:07:03
     * @throws Exception
     */
    @Test
    public void redis_transaction_test() throws Exception {
        String pattern ="test:";
        try {
             // 使用sessionCallBack处理
            SessionCallback<Boolean> sessionCallback = new SessionCallback<Boolean>() {
                List<Object> exec = null;
                @Override
                @SuppressWarnings("unchecked")
                public Boolean execute(RedisOperations operations) throws DataAccessException {
                    operations.multi();
                    redisTemplate.opsForValue().set(pattern+"cc", "aa123456");
                    redisTemplate.opsForValue().set(pattern+"dd", "bb123456");
                    exec = operations.exec();
                    if(exec.size() > 0) {
                        return (Boolean) exec.get(0);
                    }
                    return false;
                }
            };
            System.out.println(5/0);
            redisTemplate.execute(sessionCallback);
        }catch(Exception e) {
            e.printStackTrace();
            // 事务回滚
            redisTemplate.discard();
        }
    }