Commit bce81833 authored by Quxl's avatar Quxl

x

parent 5a4412f3
...@@ -33,8 +33,10 @@ import com.egolm.sentinel.config.XRException; ...@@ -33,8 +33,10 @@ import com.egolm.sentinel.config.XRException;
@RestController @RestController
public class SqlConcurrentController { public class SqlConcurrentController {
@PostMapping("concurrentSql") @PostMapping("concurrentSql")
public Rjx doExecute(String jdbcUrl, String username, String password, String sql, String parameters, Integer threadCount, Integer executeCount, Boolean isOpenTx) throws InterruptedException, IOException, SQLException { public Rjx doExecute(String jdbcUrl, String username, String password, String sql, String parameters, Integer threadCount, Integer executeCount, Boolean isOpenTx) throws InterruptedException, IOException, SQLException {
List<Object> list = new ArrayList<Object>();
threadCount = threadCount == null ? 1 : threadCount; threadCount = threadCount == null ? 1 : threadCount;
executeCount = executeCount == null ? 1 : executeCount; executeCount = executeCount == null ? 1 : executeCount;
isOpenTx = isOpenTx == null ? false : isOpenTx; isOpenTx = isOpenTx == null ? false : isOpenTx;
...@@ -52,43 +54,47 @@ public class SqlConcurrentController { ...@@ -52,43 +54,47 @@ public class SqlConcurrentController {
} }
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
for(int i = 0; i < executeCount; i++) { for(int i = 0; i < executeCount; i++) {
executor.execute(new Runner(jdbcTemplate, txTemplate, latch, sql)); executor.execute(new Runner(jdbcTemplate, txTemplate, latch, sql, list));
} }
latch.await(); latch.await();
} finally { } finally {
dataSource.close(); dataSource.close();
} }
Long end = System.currentTimeMillis(); Long end = System.currentTimeMillis();
return Rjx.jsonOk().set("执行时间", (end-start)).set("执行次数", executeCount).set("并发线程", threadCount); return Rjx.jsonOk().setData(list).set("执行时间", (end-start)).set("执行次数", executeCount).set("并发线程", threadCount);
} }
public static class Runner implements Runnable { public static class Runner implements Runnable {
private List<Object> list;
private TransactionTemplate txTemplate; private TransactionTemplate txTemplate;
private JdbcTemplate jdbcTemplate; private JdbcTemplate jdbcTemplate;
private CountDownLatch latch; private CountDownLatch latch;
private String sql; private String sql;
private String parameters; private String parameters;
public Runner(JdbcTemplate jdbcTemplate, TransactionTemplate txTemplate, CountDownLatch latch, String sql) { public Runner(JdbcTemplate jdbcTemplate, TransactionTemplate txTemplate, CountDownLatch latch, String sql, List<Object> list) {
super(); super();
this.txTemplate = txTemplate; this.txTemplate = txTemplate;
this.jdbcTemplate = jdbcTemplate; this.jdbcTemplate = jdbcTemplate;
this.latch = latch; this.latch = latch;
this.sql = sql; this.sql = sql;
this.list = list;
} }
@Override @Override
public void run() { public void run() {
List<Object> result;
if(txTemplate != null) { if(txTemplate != null) {
txTemplate.execute(new TransactionCallback<List<Object>>() { result = txTemplate.execute(new TransactionCallback<List<Object>>() {
public List<Object> doInTransaction(TransactionStatus status) { public List<Object> doInTransaction(TransactionStatus status) {
return executeSql(); return executeSql();
} }
}); });
} else { } else {
executeSql(); result = executeSql();
} }
list.add(result);
} }
public List<Object> executeSql() { public List<Object> executeSql() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment