6

ConstraintViolationException最后不也抛出了,为什么没被catch到?

 2 years ago
source link: https://blog.didispace.com/why-catch-exception-transaction-rollback-2/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
ConstraintViolationException最后不也抛出了,为什么没被catch到? | 程序猿DD

ConstraintViolationException最后不也抛出了,为什么没被catch到?

上周,我们通过这篇文章《为什么catch了异常,但事务还是回滚了?》来解释了,之前test4为什么会回滚的原因。

但还是收到了很多没有理解的反馈,主要是根据前文给出的线索去跟踪,是获得到了回滚的标示和异常,而让大家不理解的是,javax.validation.ConstraintViolationException异常不是最后也向外抛出了,那么为什么test4里catch没有能够捕获到呢?

其实这个问题并不难解释,下面就通过这篇文章,做个小实验,帮助大家进一步理解大家的这个疑问!

如果你还不了解这篇文章在讨论什么,建议先看之前的两篇:

动手尝试一下

由于@Transactional注解的事务是通过切面来实现的,所以要通过源码去了解整个过程,可能还是不容易理解。

所以,这里教大家一个简单方法来理解这次test4的catch为什么没有捕获异常。

我们通过类似下面的方式,给test4多加一些日志信息和断点:



同时,记得也在上一篇说到的事务提交入口,也加上断点。

org.springframework.orm.jpa.JpaTransactionManager

然后尝试触发test4的执行,通过DEBUG,我们都可以观察到:

test4中我们加的断点,除了47行没进入,其他的一次性都执行完了。然后才进入了org.springframework.orm.jpa.JpaTransactionManagerdoCommit方法。

所以,前文中我们跟踪的事务回滚所抛出的异常,其实是在test4中的try-catch块执行完之后才抛出的,所以内部的这个catch是无法捕获异常的,这里完全就是catch了个寂寞。

通过日志,我们也能观察到这样的执行顺序:



好了,通过这样来看,是不是要比之前有进一步的理解了呢?如果您还想跟深入的了解事务的底层运行机制,一定要debug一下源码,自己过一遍,理解会深刻哦!如果你还有其他疑问,欢迎加入我们的Spring技术交流群,参与交流与讨论,一起学习与进步!如果您正在学习Spring Boot,我的免费教程一直在持续连载,欢迎关注Spring Boot 2.x基础教程

下一篇

✕Error. Try later.You have blocked Push Notifications. Follow these instructions to enable Push Notifications.Subscribe to receive push notifications on latest updates第一时间获得DD的最新分享!You are subscribed to Push NotificationsSUBSCRIBE订阅!UNSUBSCRIBE⚡ by  Webpushr

Logo答应我!

好好学习,天天向上!必须的Cookie & Data Storage DetailCookies & Server Data Storage DisclosureBelow are all cookies & data points that will be stored to help us enable web push notifications.Go BackCookies (3)A Cookie is a small piece of data that we will store in your browser.Show cookie info_webpushrPromptActionThis cookie is stored if you do not accept to receive push notifications from us and dismiss this prompt. This helps us remember your decision._webpushrEndPointThis cookie stores a unique URL associated with your push subscription. It is only stored if you accept to receive push notifications from us. This helps us successfully manage and send push notifications to you._webpushrLastVisitThis cookie stores timestamp associated with your last visit on our site. It is only stored if you accept to receive push notifications from us. This helps us successfully manage and send push notifications to you.Server Data Storage (11)Server Data Storage are small data points that will be securely stored by us only if you accept to receive push notifications.Show data infodateTimestamp associated with your push subscription start dateendpointA custom URL and associated encrypted keys pointing to a push server that enable us to send you push notifications.timezoneYour local timezone relative to GMTdevice_typeYour device type. Possible values are Mobile or Desktop.operating_systemYour operating system such as Mac OS, Windows, Android, etc.browserYour browser detail, such as Chrome, Firefox, Opera, Edge, etc.ip_addressYour IP addresscityGeographic Location: CitystateGeographic Location: StatecountryGeographic Location: Countrysession_dateDate corresponding to your last visit on our sitePowered By Webpushr


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK