First, why is there no exception in the framework of the general subclass of the rollback configuration, when the exception occurs, the transaction has been rolled back, a good deal will only runtimeexception (unchecked non-inspected exception) rollback it?
At this point, it is necessary to understand what subclasses of RuntimeException contain:
At this point, perhaps you understand: the common code in the run-time phase often encountered those anomalies, in fact, are runtimeexception subclasses.
Checking for exceptions (Checked) is usually checked out at compile time, which gives you a misunderstanding that spring will rollback for all exceptions.
Here are some of the checked exceptions to be inspected:
Org.springframework.dao.DuplicateKeyException should be a runtimeexception subclass.
Three, the conclusion is that the SQL exception belongs to RuntimeException subclass? Or does the default configuration general exception also rollback?
& nbsp ① viewing interface documentation java.lang.sqlexception,
&NBS P Java.lang.Object
&NBS P |____java.lang.throwable
&NBS P |____ java.lang.Exception
&N Bsp |____ java. Lang. SQLException
It can be seen that java.lang.SqlException, is indeed a direct subclass of exception, belongs to the checked examined exception, the transaction is not because it occurred rollback!
② in fact, when we add the spring framework to the project development, the SQL exception is org.springframework rewritten as the SQL exception for the duplicate primary key above.
Cause: It is obvious that the cause of the exception is a generic exception, and the other custom runtimeexception are thrown after spring captures
We know org.springframework.dao.DuplicateKeyException from Spring-tx-4.0.0.release.jar.
Anti-compilation is visible:
Java.lang.Object
|____java.lang.throwable
|____ java.lang.Exception
|____ java.lang.RuntimeException
|____ org.springframework.core.NestedRuntimeException
|____org.springframework.dao.dataaccessexception
|____ org.springframework.dao.NonTransientDataAccessException
|____org.springframework.dao.dataintegrityviolationexception
|____org.springframework.dao.duplicatekeyexception
The same method can be found: Exceptions in Org.springframework.dao are runtimeexception subclasses
Come to the conclusion thatall SQL exceptions are org.springframework rewritten as runtimeexception in the spring framework, and transactions can also be rolled back!
Original link: 76525372
Which exceptions are runtimeexception? Does SQL exception belong to RuntimeException? SQL exception transaction rollback under Spring