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