Spring Learning note--spring Transaction only exception rollback on execution

Source: Internet
Author: User

When we use spring, we generally know that transactions are rolled back when they encounter an exception. Do you not know that spring's transaction defaults only to the exception that occurs when execution occurs: runtimeexception, assuming a method throws exception or checked exception spring's transaction is not rolled back.


Let's take a look at the classification of exceptions. Exceptions are generally divided into checked anomalies and runtime anomalies.


checkedexception:
Java thinks that checked exceptions are exceptions that can be handled, so Java programs must explicitly handle checked exceptions, assuming that the program does not handle checked exceptions, and that the program will error when compiling.


We're more familiar with checked anomalies.
Java.lang.ClassNotFoundException
Java.lang.NoSuchMetodException
Java.io.IOException
runtimeexception:
Runtime such as divisor is 0 and set subscript out of bounds, and its frequent, processing trouble, if the display of the declaration or capture will have a significant impact on the readability and execution efficiency of the program. Therefore, the system proactively detects and hands them to the default exception handlers.

Of course you have the processing requirements to be able to show capture them as well.
We are more familiar with the subclasses of the Rumtimeexception class have
Java.lang.ArithmeticException
Java.lang.ArrayStoreExcetpion
Java.lang.ClassCastException
Java.lang.IndexOutOfBoundsException
Java.lang.NullPointerException
We can change the way transactions are handled by default in the following ways:
Define Norollbackfor and Rollbackfor in the @transaction annotation to specify whether an exception is rolled back.
@Transaction (Norollbackfor=runtimeexception.class)
@Transaction (Rollbackfor=exception.class)
This requires that when we define an exception, the exception that we define is inherited from RuntimeException, so that it will be handled accurately by spring's default transaction.

Spring Learning note--spring Transaction only exception rollback on execution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.