First, preface
The recent development of the program, the database has been the number of self-increment ID jump, but the DBA to check the operation log, there is no delete record. began to query things slowly. How long has owed the account, today should return to spring things. I hope you have something to gain. July 19, 2016 22:32:38
Two, spring nested things 1, display project code-Simple Measurement Springboot project overall project is so simple, for convenience. Here is only the Biz Layer and service layer, mainly as a two-tier nesting, we just look at the approximate OK. The following will give the GIT project address, download down to see and understand, and strive for the simplest. Below we introduce the exception. The controller calls the layer (not using it as an outer layer, because the Controller acts as an outer layer to be configured on the Servlet-mvc.xml OK.) But I feel more troublesome, generally not recommended)
<pre name= "code" class= "HTML" >package Com.ycy.app;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.enableautoconfiguration;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.boot.autoconfigure.jdbc.datasourceautoconfiguration;import Org.springframework.context.annotation.importresource;import Org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.restcontroller;/** * Created by Ycy on 16/7/19. */@RestController @springbootapplication@enableautoconfiguration (exclude = {Datasourceautoconfiguration.class}) @ Importresource ({"Classpath:/applicationcontext.xml"}) public class application {@Autowired private testbiz testbiz; @RequestMapping ("/") String Home () throws Exception {System.out.println ("Controller performs normally"); Testbiz.insettes (); Return "Normally returns Hello world!"; } public Static void Main (string[] args) throws Exception {Springapplication.run (application.class, args); }}
Biz Layer (outer)
<pre name= "code" class= "HTML" >package com.ycy.app;import Com.ycy.service.testservice;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.component;import org.springframework.transaction.annotation.transactional;/** * Created by Ycy on 16/7/20. */@Componentpublic class Testbiz { @Autowired private testservice testservice; @Transactional public void Insettes () { for (int j = 0; J < 8; J + +) { Testservice.testinsert (J, j + "name"); c7/>} System.out.println ("Biz layer Normal Execution");} }
Service layer (inner layer)
<pre name= "code" class= "HTML" ><pre name= "code" class= "HTML" ><pre name= "code" class= "HTML" >package Com.ycy.service.impl;import Com.ycy.center.dao.entity.ycytable;import Com.ycy.center.dao.mapper.YcyTableMapper; Import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.Service; Import org.springframework.transaction.annotation.transactional;/** * Created by Ycy on 16/7/19. */@Servicepublic class Testserviceimpl implements Com.ycy.service.TestService { @Autowired private Ycytablemapper Ycytablemapper; @Transactional public void testinsert (int num,string name) { ycytable ycytable=new ycytable (); Ycytable.setname (name); Ycytable.setnum (num); Ycytablemapper.insert (ycytable); SYSTEM.OUT.PRINTLN (num+ "service Normal Execution");} }
2, external things, internal things, inside and outside are no try Catch
External exception:
Code show, modify the outer Biz layer code as follows
<pre name= "code" class= "HTML" ><pre name= "code" class= "HTML" > @Componentpublic class Testbiz { @ autowired private Testservice testservice; @Transactional public void Insettes () { for (int j = 0; J < 8; J + +) { Testservice.testinsert (J, j + "name"); C8/>if (J = = 3) { int i = 1/0;//here will produce an exception } } System.out.println ("Biz layer Normal Execution");} }
Print Execution Result: 0-3service normal execution of database results: all data rollback
Summary of external exceptions:when there is no try catch inside and outside, external exception, all rollback.
Internal exception:Code presentation, modifying service layer code
package Com.ycy.service.impl;import Com.ycy.center.dao.entity.ycytable;import Com.ycy.center.dao.mapper.YcyTableMapper; Import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.Service; Import org.springframework.transaction.annotation.transactional;/** * Created by Ycy on 16/7/19. */@Servicepublic class Testserviceimpl implements Com.ycy.service.TestService {@Autowired private ycytablemapper Ycyta Blemapper; @Transactional public void Testinsert (int num, String name) {ycytable ycytable = new ycytable (); Ycytable.setname (name); Ycytable.setnum (num); Ycytablemapper.insert (ycytable); if (num = = 3) {int i = 1/0;//An exception is generated here} System.out.println (num + "service normal execution"); }}
Print Execution Result: 0-3service normal execution of database results: all data rollbackInternal Exception Summary:internal exception, all rollback when there is no try catch inside and outside.
3. External starting things, internal things, external try Catch
External exception:code presentation, modifying the Biz Layer code
@Componentpublic class Testbiz { @Autowired private testservice testservice; @Transactional public void Insettes () { try { for (int j = 0; J < 8; J + +) { Testservice.testinsert (J, J + "name"); if (j = = 3) { int i = 1/0;//here will produce an exception }} } catch (Exception ex) { System.out.println ("Exception Log Processing");
} System.out.println ("Biz layer Normal Execution");} }
Printed results: 0-3 performing normal Database results: 4 data
External Exception Summary: External with try catch, external exception, cannot rollback (thing error)
Internal exception:code presentation, modifying service layer code:
@Servicepublic class Testserviceimpl implements Com.ycy.service.TestService { @Autowired private Ycytablemapper Ycytablemapper; @Transactional public void testinsert (int num, String name) { ycytable ycytable = new ycytable (); Ycytable.setname (name); Ycytable.setnum (num); Ycytablemapper.insert (ycytable); if (num = = 3) { int i = 1/0;//An exception is generated here } System.out.println (num + "service normal Execution");} }
print Result: 0-2 print normal Database results: No data, all data rollback
Internal Exception Summary: external Try catch time, internal exception, all rollback
4, external things, internal things, inside have try Catch
External exception:Code show, modifying the biz layer:
@Componentpublic class Testbiz { @Autowired private testservice testservice; @Transactional public void Insettes () { for (int j = 0; J < 8; J + +) { Testservice.testinsert (J, j + "name" ); if (j = = 3) { int i = 1/0;//here will produce an exception } } System.out.println ("Biz layer Normal Execution");} }
print Result: 0-3service print normal Database results: No data, all data rollback
Summary of external exceptions:internal try Catch, external exception, all rollback
Internal exception:To modify the service layer code:
@Servicepublic class Testserviceimpl implements Com.ycy.service.TestService { @Autowired private Ycytablemapper Ycytablemapper; @Transactional public void testinsert (int num, String name) { try { ycytable ycytable = new ycytable (); Ycytable.setname (name); Ycytable.setnum (num); Ycytablemapper.insert (ycytable); if (num = = 3) { int i = 1/0;//here will produce an exception } } catch (Exception ex) { System.out.println (num + "service XOR Log "); } SYSTEM.OUT.PRINTLN (num + "service normal Execution");} }
print Result: 0-0service print normal Database results: no rollbackInternal Exception Summary:internal try Catch, inner exception, all do not rollback (things fail);
5, external things, internal things, inside and outside have try Catch
External exception:Code show, modifying the biz layer:
@Componentpublic class Testbiz { @Autowired private testservice testservice; @Transactional public void Insettes () { try { for (int j = 0; J < 8; J + +) { Testservice.testinsert (J, J + "name"); if (j = = 3) { int i = 1/0;//here will produce an exception }} } catch (Exception ex) { System.out.println ("Biz Layer Exception Log "); } SYSTEM.OUT.PRINTLN ("Biz layer Normal Execution");} }
print Result: 0-3service print normal Database results: Insert three data, no rollback
Summary of external exceptions:try Catch, external exception, half thing execution (things fail) inside and outside
Internal exception:Code presentation, modifying service layer code
@Servicepublic class Testserviceimpl implements Com.ycy.service.TestService { @Autowired private Ycytablemapper Ycytablemapper; @Transactional public void testinsert (int num, String name) { try { ycytable ycytable = new ycytable (); Ycytable.setname (name); Ycytable.setnum (num); Ycytablemapper.insert (ycytable); if (num = = 3) { int i = 1/0;//here will produce an exception } } catch (Exception ex) { System.out.println (num + "service XOR Log processing "); } SYSTEM.OUT.PRINTLN (num + "service normal Execution");} }
print Result: 0-7service print normal, 3 abnormal days good Database results: Insert all, no rollbackInternal things Summary:Inside and outside there are try Catch, inner exception, things never roll (things fail)
Three, nested things summary of things successful summary1 . When there is no try catch inside and outside, external exception, all rollback.
2, inside and outside of no try catch, internal exception, all rollback.
3, external try catch time, internal exception, all rollback
4. Internal try Catch, external exception, all rollback
5, Friendship tips: The outer method to adjust the other interface, or another open the operation of the thread, it must be put to the end!!! (Because the tuning interface cannot be rolled back, it must be processed at the end)
Summary: Many transaction rollback failures occur because the above exception was caught. If you must capture, capture and then throwruntimeexception (default is exception capture runtimeexception).
Iv. Proper nesting of things instancesController layer
Package Com.ycy.app;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.enableautoconfiguration;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.boot.autoconfigure.jdbc.datasourceautoconfiguration;import Org.springframework.context.annotation.importresource;import Org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.restcontroller;/** * Created by Ycy on 16/7/19. */@RestController @springbootapplication@enableautoconfiguration (exclude = {Datasourceautoconfiguration.class}) @ Importresource ({"Classpath:/applicationcontext.xml"}) public class application {@Autowired private testbiz testbiz; @RequestMapping ("/") String Home () {SYSTEM.OUT.PRINTLN ("Controller performs normally"); try {testbiz.insettes (); } catch (Exception e) {System.out.println ("Controller Exception Log Execution"); } return"Normal return Hello world!"; } public static void Main (string[] args) throws Exception {Springapplication.run (application.class, args); }}
Outer biz Layer:
Package Com.ycy.app;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.component;import Org.springframework.transaction.annotation.transactional;import com.ycy.service.testservice;/** * Created by Ycy on 16/7/20. */@Componentpublic class Testbiz { @Autowired private testservice testservice; @Transactional public void Insettes () throws Exception { try {for (int j = 0; J < 8; J + +) { Testservic E.testinsert (J, j + "name"); if (j = = 3) { int i = 1/0;//here will produce an exception }} } catch (Exception ex) { System.out.println ("Biz Layer Exception Log Management "); throw new RuntimeException (ex); } SYSTEM.OUT.PRINTLN ("Biz layer Normal Execution");} }
Internal service Layer
Package Com.ycy.service.impl;import Com.ycy.center.dao.entity.ycytable;import Com.ycy.center.dao.mapper.ycytablemapper;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.service;import org.springframework.transaction.annotation.transactional;/** * Created by Ycy on 16/7/19. */@Servicepublic class Testserviceimpl implements Com.ycy.service.TestService {@Autowired private ycytablemapper Ycyta Blemapper; @Transactional public void Testinsert (int num, String name) throws Exception {try {ycytable ycytable = new Ycyt Able (); Ycytable.setname (name); Ycytable.setnum (num); Ycytablemapper.insert (ycytable); if (num== 3) {int i = 1/0;//here will produce an exception}} catch (Exception ex) {System.out.println (num + "service exception Log processing "); throw new RuntimeException (ex); } System.out.println (num + "service normal execution"); }}
Project Address: https://github.com/yangchangyong0/springTransactional Project git address
Spring Transaction Management--(ii) a detailed description of nested things