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

推荐订阅源

量子位
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Scott Helme
Scott Helme
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
Project Zero
Project Zero
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
S
Schneier on Security
S
SegmentFault 最新的问题
G
GRAHAM CLULEY
J
Java Code Geeks
IT之家
IT之家
爱范儿
爱范儿
Latest news
Latest news
阮一峰的网络日志
阮一峰的网络日志
P
Palo Alto Networks Blog
Jina AI
Jina AI
腾讯CDC
V
Visual Studio Blog
Apple Machine Learning Research
Apple Machine Learning Research
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tor Project blog
T
Threatpost
V
V2EX
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
美团技术团队
小众软件
小众软件
NISL@THU
NISL@THU
Simon Willison's Weblog
Simon Willison's Weblog
T
The Exploit Database - CXSecurity.com
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Securelist
V
Vulnerabilities – Threatpost
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
A
Arctic Wolf
U
Unit 42
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org

博客园 - 陈惟鲜的博客

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();
        }
    }