Spring Transaction Transaction Roll back

Source: Internet
Author: User
Tags rollback throw exception

test method:

Write a unit test that invokes a service layer method (the method--insert, update, delete) that occurs to write to the database.

Test Process:

Define a service method as follows:

Public Smstimingcreatesmstiming(smstiming smstiming) {

Smstiming s= this. Getsmstimingdao (). createsmstiming (smstiming);

return s;

}

Define two exceptions (default configuration testexception for spring transaction rollback exception):

Publicclass Mytestexceptionextends Exception

Publicclass TestExceptionextends Exception

Note the following: Each time this method is different (throws an exception differently).

 

Test 1:

Public Smstimingcreatesmstiming(smstiming smstiming) {

Smstiming s= this. Getsmstimingdao (). createsmstiming (smstiming);

int i = 4/0;//to create an exception (actually throws a arithmeticexception run exception here)

return s;

}

Test 1 Results: The transaction is rolled back----no new data is inserted in the database.

Test 2:

Public Smstimingcreatesmstiming(smstiming smstiming)throwsexception{//the exception to be inspected (non-running exception) must be thrown

Smstiming s= this. Getsmstimingdao (). createsmstiming (smstiming);

Try {

int i = 4/0;//Human-generated exception

}catch(Exception e) {

thrownew Exception ("");//Throw Exception exception

}

return s;

}

Test 2 results: no transaction rollback----inserting new data into the database.

Test 3:

Public Smstimingcreatesmstiming(smstiming smstiming)throwsruntimeexception{//Run exception (non-inspected exception) can not be thrown

Smstiming s= this. Getsmstimingdao (). createsmstiming (smstiming);

Try {

int i = 4/0;//Human-generated exception

}catch(Exception e) {

thrownewruntimeexception ("");//Throw RuntimeException exception

}

return s;

}

Test 3 results: Transaction rollback----New data is not inserted in the database

Test 4:

Public Smstimingcreatesmstiming(smstiming smstiming)throws testexception{//the exception to be inspected (non-running exception) must be thrown

Smstiming s= this. Getsmstimingdao (). createsmstiming (smstiming);

Try {

int i = 4/0;//Human-generated exception

}catch(Exception e) {

thrownewtestexception ("");//Throw TestException exception

}

return s;

}

Test 4 Results: The transaction is rolled back----no new data is inserted in the database.

Test 5:

Public Smstimingcreatesmstiming(smstiming smstiming)throws mytestexception{//the exception to be inspected (non-running exception) must be thrown

Smstiming s= this. Getsmstimingdao (). createsmstiming (smstiming);

Try {

int i = 4/0;//Human-generated exception

}catch(Exception e) {

thrownewmytestexception ("");//Throw mytestexception exception

}

return s;

}

Test 5 results: no transaction rollback----inserting new data into the database.

Test 6:

Public Smstimingcreatesmstiming(smstiming smstiming)throws mytestexception{//the exception to be inspected (non-running exception) must be thrown ( Note: At this time spring specifies that this exception is configured to roll back)

Smstiming s= this. Getsmstimingdao (). createsmstiming (smstiming);

Try {

int i = 4/0;//Human-generated exception

}catch(Exception e) {

thrownewmytestexception ("");//Throw mytestexception exception

}

return s;

}

Test 6 Results: The transaction is rolled back----no new data is inserted in the database.

Test Summary:

Test 1, Test 3, Test 4, Test 6 will be a transaction rollback, test 2, Test 5 will not be a transaction rollback.

why is that. Because it is an exception type (inspected exception, run-time exception) is different or uses spring's rollback-for configuration.

Test 1 and Test 3 because a run-time exception was thrown and the transaction is rolled back.

Test 4 and Test 5, test 6 throws the exception testexception, mytestexception, then why Test 4 and test 6 transaction rollback.

Because we specified this exception in the Spring transaction configuration (Specify Rollback-for).

the transaction infrastructure code for the spring framework will default to only transaction rollback is only identified when the runtime and unchecked exceptions are thrown. That is, when an instance of a runtimeexception or its subclasses is thrown. (Errors also-default-Identifies transaction rollback.) Checked exceptions thrown from the transaction method will not be identified for transaction rollback

 

 

Information:

1. http://hi.baidu.com/thrunder_liu/blog/item/e3a5b60132b31281e950cd87.html

2. http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html


Another, Tomcat source analysis ebook:

Code and UML Diagrams: Https://github.com/Aresyi/HowTomcatWorks
Layout better Baidu ebook: https://yuedu.baidu.com/ebook/ac92f0d35122aaea998fcc22bcd126fff7055d60

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.