新增事务测试代码

This commit is contained in:
thinkgem
2019-07-07 00:48:34 +08:00
parent 875a4526cf
commit b5fd643070
7 changed files with 65 additions and 18 deletions

View File

@@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.jeesite.common.entity.Page;
import com.jeesite.common.idgen.IdGen;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.file.utils.FileUploadUtils;
@@ -126,4 +127,30 @@ public class TestDataService extends CrudService<TestDataDao, TestData> {
+ ", l: " + l + ", f: " + f + ", d: " + d + ", s: " + s);
}
/**
* 事务测试,若 Child 报错,则回滚
*/
@Transactional(readOnly=false/*, propagation=Propagation.NOT_SUPPORTED*/)
public void transTest(TestData testData) {
testData.setTestInput("transTest");
testData.setTestTextarea(IdGen.randomBase62(5));
dao.insert(testData);
TestDataChild testDataChild = new TestDataChild();
testDataChild.setTestData(testData);
// 设置一个超出数据库范围的值,抛出数据库异常
StringBuilder sb = new StringBuilder();
for (int i=0; i<500; i++){
sb.append("transTest" + i);
}
testDataChild.setTestInput(sb.toString());
testDataChildDao.insert(testDataChild);
}
/**
* 事务验证,返回空,则事务回滚成功
*/
public boolean transValid(TestData testData) {
return dao.get(testData) == null;
}
}

View File

@@ -120,4 +120,20 @@ public class TestDataController extends BaseController {
return renderResult(Global.TRUE, text("删除数据成功!"));
}
/**
* 事务测试
*/
@RequiresPermissions("test:testData:edit")
@RequestMapping(value = "transTest")
@ResponseBody
public String transTest(TestData testData) {
try{
testDataService.transTest(testData);
}catch (Exception e) {
logger.debug("事务测试信息,报错回滚:" + e.getMessage());
}
boolean bl = testDataService.transValid(testData);
return renderResult(Global.TRUE, "事务测试"+(bl?"成功,数据已":"失败,数据未")+"回滚!");
}
}