栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在@Transactional内部提交try-catch中的更改

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在@Transactional内部提交try-catch中的更改

首先,调用flush()不会给您带来任何好处:flush()不会执行任何操作,并且由于您在同一事务中记录错误,因此插入将回滚。

因此,您需要启动一个新的“嵌套”事务来记录错误。

public class A {    @Autowired    private B b;    @Transactional    public void doSmt() {        try { someOperation(); // can throw exception b.logIntoDb(); // if everything is OK        } catch {Exception e} { b.logIntoDbWithException(e.getMessage()); // log error into db throw new MyCustomException(e);        }    }}public class B{    //outer transaction is still active    public  void logIntoDb(String message) {        final LogEntry logEntry = new LogEntry();        logEntry.setDate(new Date());        logEntry.setMessage(message);        logEntry.persist();    }    // 'outer' transaction will be suspended until this method completes    // this new transaction will commit/rollback independently of the outer transaction    @Transactional(propagation = Propagation.REQUIRES_NEW)    public  void logIntoDbWithException(String message) {        final LogEntry logEntry = new LogEntry();        logEntry.setDate(new Date());        logEntry.setMessage(message);        logEntry.persist();    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/452257.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号