Which exceptions are runtimeexception? Does SQL exception belong to RuntimeException? SQL exception transaction rollback under Spring

Source: Internet
Author: User
Tags rollback throwable

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

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.