最近开发程序的时候,出现数据库自增id跳数字情况,无奈之下dba遍查操作日志,没有delete记录。才开始慢慢来查询事物问题。多久以来欠下的账,今天该还给spring事物。 希望大家有所收获。2016年07月19日22:32:38
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 正常执行");
testBiz.insetTes();
return " 正常返回Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
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.
*/
@Component
public class TestBiz {
@Autowired
private TestService testService;
@Transactional
public void insetTes() {
for (int j = 0; j < 8; j++) {
testService.testInsert(j, j + "姓名");
}
System.out.println("biz层 正常执行");
}
}
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.
*/
@Service
public 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正常执行");
}
}
外部异常:
@Component
public class TestBiz {
@Autowired
private TestService testService;
@Transactional
public void insetTes() {
for (int j = 0; j < 8; j++) {
testService.testInsert(j, j + "姓名");
if (j == 3) {
int i = 1 / 0;// 此处会产生异常
}
}
System.out.println("biz层 正常执行");
}
}
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.
*/
@Service
public 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;// 此处会产生异常
}
System.out.println(num + "service正常执行");
}
}
@Component
public class TestBiz {
@Autowired
private TestService testService;
@Transactional
public void insetTes() {
try {
for (int j = 0; j < 8; j++) {
testService.testInsert(j, j + "姓名");
if (j == 3) {
int i = 1 / 0;// 此处会产生异常
}
}
} catch (Exception ex) {
System.out.println("异常日志处理");
}
System.out.println("biz层 正常执行");
}
}
打印结果:0-3执行正常数据库结果:4条数据
@Service
public 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;// 此处会产生异常
}
System.out.println(num + "service正常执行");
}
}
打印结果:0-2打印正常 数据库结果:无数据,全部数据回滚
@Component
public class TestBiz {
@Autowired
private TestService testService;
@Transactional
public void insetTes() {
for (int j = 0; j < 8; j++) {
testService.testInsert(j, j + "姓名");
if (j == 3) {
int i = 1 / 0;// 此处会产生异常
}
}
System.out.println("biz层 正常执行");
}
}
@Service
public 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;// 此处会产生异常
}
} catch (Exception ex) {
System.out.println(num + "service异常日志");
}
System.out.println(num + "service正常执行");
}
}
@Component
public class TestBiz {
@Autowired
private TestService testService;
@Transactional
public void insetTes() {
try {
for (int j = 0; j < 8; j++) {
testService.testInsert(j, j + "姓名");
if (j == 3) {
int i = 1 / 0;// 此处会产生异常
}
}
} catch (Exception ex) {
System.out.println("biz层异常日志处理");
}
System.out.println("biz层 正常执行");
}
}
@Service
public 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;// 此处会产生异常
}
} catch (Exception ex) {
System.out.println(num + "service异常日志处理");
}
System.out.println(num + "service正常执行");
}
}
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 正常执行");
try {
testBiz.insetTes();
} catch (Exception e) {
System.out.println("controller 异常日志执行");
}
return " 正常返回Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
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.
*/
@Component
public class TestBiz {
@Autowired
private TestService testService;
@Transactional
public void insetTes() throws Exception {
try {
for (int j = 0; j < 8; j++) {
testService.testInsert(j, j + "姓名");
if (j == 3) {
int i = 1 / 0;// 此处会产生异常
}
}
} catch (Exception ex) {
System.out.println("biz层异常日志处理");
throw new RuntimeException(ex);
}
System.out.println("biz层 正常执行");
}
}
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.
*/
@Service
public class TestServiceImpl implements com.ycy.service.TestService {
@Autowired
private YcyTableMapper ycyTableMapper;
@Transactional
public void testInsert(int num, String name) throws Exception {
try {
YcyTable ycyTable = new YcyTable();
ycyTable.setName(name);
ycyTable.setNum(num);
ycyTableMapper.insert(ycyTable);
if (num== 3) {
int i = 1 / 0;// 此处会产生异常
}
} catch (Exception ex) {
System.out.println(num + "service异常日志处理");
throw new RuntimeException(ex);
}
System.out.println(num + "service正常执行");
}
}