新增事务测试代码

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

@@ -41,15 +41,16 @@ jdbc:
maxEvictableIdleTimeMillis: 1800000
# 是否自动回收泄露的连接和超时时间单位秒35分钟4.0.6+
removeAbandoned: true
removeAbandoned: false
removeAbandonedTimeout: 2100
# Oracle 下会自动开启 PSCache并指定每个连接上 PSCache 大小。若不指定,则与 maxActive 相同4.1.5+
maxPoolPreparedStatementPerConnectionSize: ~
# JTA 分布式事务v4.0.4+
# JTA 分布式事务,建议启用多数据源的时候开启v4.0.4+
jta:
enabled: false
# 注意:如果报 oracle.jdbc.xa.OracleXAResource.recover 错误,则需要授权如下:
# grant select on sys.dba_pending_transactions to jeesite;
# grant select on sys.pending_trans$ to jeesite;

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?"成功,数据已":"失败,数据未")+"回滚!");
}
}

View File

@@ -99,7 +99,7 @@ jdbc:
# maxEvictableIdleTimeMillis: 1800000
#
# # 是否自动回收泄露的连接和超时时间单位秒35分钟4.0.6+
# removeAbandoned: true
# removeAbandoned: false
# removeAbandonedTimeout: 2100
#
# # Oracle 下会自动开启 PSCache并指定每个连接上 PSCache 大小。若不指定,则与 maxActive 相同4.1.5+
@@ -124,9 +124,10 @@ jdbc:
# minIdle: 3
# maxActive: 20
# # JTA 分布式事务v4.0.4+
# # JTA 分布式事务,建议启用多数据源的时候开启v4.0.4+
# jta:
# enabled: false
# 注意:如果报 oracle.jdbc.xa.OracleXAResource.recover 错误,则需要授权如下:
# grant select on sys.dba_pending_transactions to jeesite;
# grant select on sys.pending_trans$ to jeesite;

View File

@@ -10,6 +10,7 @@
<% if(hasPermi('test:testData:edit')){ %>
<a href="${ctx}/test/testData/form" class="btn btn-default btnTool" title="${text('新增数据')}"><i class="fa fa-plus"></i> ${text('新增')}</a>
<% } %>
<a href="#" class="btn btn-default" id="btnTrunsTest" title="事务测试"><i class="fa fa-refresh"></i> 事务测试</a>
</div>
</div>
<div class="box-body">
@@ -175,5 +176,12 @@ $('#dataGrid').dataGrid({
// $("#dataGrid").parent().append("<div class=\"ml10\">没有符合数据</div>");
// }
}
});
});
$("#btnTrunsTest").click(function(){
js.ajaxSubmit("${ctx}/test/testData/transTest", function(data){
js.showMessage(data.message);
page();
});
return false;
});
</script>

View File

@@ -22,7 +22,10 @@ import com.jeesite.modules.test.entity.TestDataChild;
import com.jeesite.modules.test.service.TestDataService;
/**
* 多数据源并发测试,将 TestDataChildDao 的数据源设置为 ds2
* 多数据源并发测试<br>
* 1、将 TestDataChildDao 的数据源设置为 ds2<br>
* 2、将 TestDataChild 的表名设置为 test_data_child2<br>
* 3、配置 ds2 数据源,并创建 test_data_child2 表
* @author ThinkGem
* @version 2019-6-26
*/
@@ -47,12 +50,12 @@ public class MultiDataSourceTest extends BaseSpringContextTests {
new TestDataChild(), new TestDataChild(), new TestDataChild()));
testDataService.save(testData);
List<TestData> list = testDataService.findList(new TestData());
System.out.println(list.size());
System.out.println("size: " + list.size());
list.forEach(e -> {
System.out.println(testDataService.get(e));
System.out.println("get: " + testDataService.get(e));
});
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
} finally {
latch.countDown();
}

View File

@@ -1,9 +0,0 @@
package com.jeesite.test;
public class Test {
public static void main(String[] args) {
}
}