当前位置:网站首页>@Configuration, use, principle and precautions of transmission:

@Configuration, use, principle and precautions of transmission:

2022-07-07 10:23:00 thoughtCodes

One . Use :
@Transactional Annotations can act on interfaces 、 Interface method 、 Class and class methods , however Spring It is not recommended to use this annotation on interfaces or interface methods , Because it only works when using an interface based proxy .
@Transactional Annotations should only be applied to public On the way , This is from Spring AOP The essence of the decision . If you are in the protected、private Or the default visibility method @Transactional annotation , This will be ignored , No exceptions will be thrown .
By default , Only method calls from outside will be AOP Agent capture , That is to say , Even if used by the calling method @Transactional To modify , Calling other methods inside this class does not cause transactional behavior . ( The following example 1 This is a good example )

@Transactional annotation , In case of unchecked exception, It will happen rollback. That is to say, the exception thrown is RuntimeException Subclasses of (Errors It also causes transaction rollback ), And throw checked Exception will not cause transaction rollback . Sum up :NullPointerException Will roll back , image IOException、 Network class exception It won't roll back .
When ( Application tested )unchecked exception By try - catch when Will not be rolled back .
and Cheacked Exception, Also known as “ Exception checked ”, Such as IOException、SQLException And user-defined Exception abnormal . Such exceptions must be handled at compile time , Otherwise, it cannot be compiled .

@Transcation By default, only rollback (JVM Level ) Throw out RuntimeException And Error Abnormal transactions , So by default, if you throw SQLException、IOException You cannot rollback a transaction without processing .
error It's bound to roll back

Java Language norms will derive from Error class or RuntimeException All exceptions of the class become unobserved (unchecked) abnormal , All other exceptions (OI,net) Called observed (checked) abnormal .

Two . principle :
dynamic aop The way of weaving

3. Precautions .

Not specified RollbackOn, And the exception thrown is not RuntimeException
Therefore, Alibaba code specification initiative is to show the specified rollbackOn by Exception

Recommended reading :
https://blog.csdn.net/weixin_45433603/article/details/107959487?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_title~default-0-107959487-blog-121939722.pc_relevant_aa2&spm=1001.2101.3001.4242.1&utm_relevant_index=3

https://blog.csdn.net/maomaoqiukqq/article/details/103941157
https://blog.csdn.net/qq_35387940/article/details/107245613

原网站

版权声明
本文为[thoughtCodes]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070812249794.html