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

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
小众软件
小众软件
V
Vulnerabilities – Threatpost
P
Proofpoint News Feed
The Register - Security
The Register - Security
A
About on SuperTechFans
L
LINUX DO - 热门话题
Blog — PlanetScale
Blog — PlanetScale
V
Visual Studio Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Google DeepMind News
Google DeepMind News
L
LangChain Blog
博客园_首页
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
Recent Announcements
Recent Announcements
NISL@THU
NISL@THU
P
Privacy & Cybersecurity Law Blog
MongoDB | Blog
MongoDB | Blog
C
Check Point Blog
C
Cybersecurity and Infrastructure Security Agency CISA
G
GRAHAM CLULEY
Scott Helme
Scott Helme
P
Palo Alto Networks Blog
博客园 - Franky
The Hacker News
The Hacker News
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Security Latest
Security Latest
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
T
The Exploit Database - CXSecurity.com
T
Tenable Blog
V
V2EX
Hacker News: Ask HN
Hacker News: Ask HN
大猫的无限游戏
大猫的无限游戏
MyScale Blog
MyScale Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
SegmentFault 最新的问题
Latest news
Latest news
S
Schneier on Security
博客园 - 三生石上(FineUI控件)
L
Lohrmann on Cybersecurity
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tor Project blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - 陈惟鲜的博客

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