Commit 55f1f662 authored by Quxl's avatar Quxl

x

parent bce81833
......@@ -28,6 +28,7 @@ import com.alibaba.druid.pool.DruidDataSource;
import com.egolm.common.StringUtil;
import com.egolm.common.bean.Rjx;
import com.egolm.common.jdbc.JUtil;
import com.egolm.sentinel.config.ExceptionHandler;
import com.egolm.sentinel.config.XRException;
@RestController
......@@ -35,16 +36,17 @@ public class SqlConcurrentController {
@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) {
List<Object> list = new ArrayList<Object>();
List<String> stack = new ArrayList<String>();
Long start = System.currentTimeMillis();
try {
threadCount = threadCount == null ? 1 : threadCount;
executeCount = executeCount == null ? 1 : executeCount;
isOpenTx = isOpenTx == null ? false : isOpenTx;
ThreadPoolExecutor executor = new ThreadPoolExecutor(threadCount, threadCount, 5, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(executeCount));
CountDownLatch latch = new CountDownLatch(executeCount);
DruidDataSource dataSource = null;
Long start = System.currentTimeMillis();
try {
dataSource = this.getDataSource(jdbcUrl, username, password);
TransactionTemplate txTemplate = null;
......@@ -54,18 +56,22 @@ public class SqlConcurrentController {
}
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
for(int i = 0; i < executeCount; i++) {
executor.execute(new Runner(jdbcTemplate, txTemplate, latch, sql, list));
executor.execute(new Runner(jdbcTemplate, txTemplate, latch, sql, list, stack));
}
latch.await();
} finally {
dataSource.close();
}
} catch (Exception e) {
stack.add(ExceptionHandler.toStackString(e).toString());
}
Long end = System.currentTimeMillis();
return Rjx.jsonOk().setData(list).set("执行时间", (end-start)).set("执行次数", executeCount).set("并发线程", threadCount);
}
public static class Runner implements Runnable {
private List<String> stack;
private List<Object> list;
private TransactionTemplate txTemplate;
private JdbcTemplate jdbcTemplate;
......@@ -73,31 +79,35 @@ public class SqlConcurrentController {
private String sql;
private String parameters;
public Runner(JdbcTemplate jdbcTemplate, TransactionTemplate txTemplate, CountDownLatch latch, String sql, List<Object> list) {
public Runner(JdbcTemplate jdbcTemplate, TransactionTemplate txTemplate, CountDownLatch latch, String sql, List<Object> list, List<String> stack) {
super();
this.txTemplate = txTemplate;
this.jdbcTemplate = jdbcTemplate;
this.latch = latch;
this.sql = sql;
this.list = list;
this.stack = stack;
}
@Override
public void run() {
List<Object> result;
try {
if(txTemplate != null) {
result = txTemplate.execute(new TransactionCallback<List<Object>>() {
txTemplate.execute(new TransactionCallback<List<Object>>() {
public List<Object> doInTransaction(TransactionStatus status) {
return executeSql();
executeSql();
return null;
}
});
} else {
result = executeSql();
executeSql();
}
} catch (Exception e) {
stack.add(ExceptionHandler.toStackString(e).toString());
}
list.add(result);
}
public List<Object> executeSql() {
public void executeSql() {
try {
List<Object> argList = new ArrayList<Object>();
if(StringUtil.isNotBlank(parameters)) {
......@@ -119,7 +129,7 @@ public class SqlConcurrentController {
}
Object[] args = argList.toArray(new Object[argList.size()]);
return jdbcTemplate.execute(new CallableStatementCreator() {
list.add(jdbcTemplate.execute(new CallableStatementCreator() {
public CallableStatement createCallableStatement(final Connection con) throws SQLException {
final CallableStatement cs = con.prepareCall(sql);
for(int i = 0; i < args.length; i++) {
......@@ -147,7 +157,9 @@ public class SqlConcurrentController {
}
return datas;
}
});
}));
} catch (Exception e) {
stack.add(ExceptionHandler.toStackString(e).toString());
} finally {
latch.countDown();
}
......
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