Commit 28c94434 authored by Quxl's avatar Quxl

x

parent 01c22ae6
......@@ -17,6 +17,10 @@ import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
......@@ -30,16 +34,25 @@ import com.egolm.sentinel.config.XRException;
public class SqlConcurrentController {
@PostMapping("concurrentSql")
public Rjx doExecute(String jdbcUrl, String username, String password, String sql, String parameters, int threadCount, int executeCount) throws InterruptedException, IOException, SQLException {
public Rjx doExecute(String jdbcUrl, String username, String password, String sql, String parameters, Integer threadCount, Integer executeCount, Integer isOpenTx) throws InterruptedException, IOException, SQLException {
threadCount = threadCount == null ? 1 : threadCount;
executeCount = executeCount == null ? 1 : executeCount;
isOpenTx = isOpenTx == null ? 0 : 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;
if(isOpenTx.intValue() == 1) {
DataSourceTransactionManager txManager = new DataSourceTransactionManager(dataSource);
txTemplate = new TransactionTemplate(txManager);
}
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
for(int i = 0; i < executeCount; i++) {
executor.execute(new Runner(jdbcTemplate, latch, sql));
executor.execute(new Runner(jdbcTemplate, txTemplate, latch, sql));
}
latch.await();
} finally {
......@@ -51,13 +64,15 @@ public class SqlConcurrentController {
public static class Runner implements Runnable {
JdbcTemplate jdbcTemplate;
private TransactionTemplate txTemplate;
private JdbcTemplate jdbcTemplate;
private CountDownLatch latch;
private String sql;
private String parameters;
public Runner(JdbcTemplate jdbcTemplate, CountDownLatch latch, String sql) {
public Runner(JdbcTemplate jdbcTemplate, TransactionTemplate txTemplate, CountDownLatch latch, String sql) {
super();
this.txTemplate = txTemplate;
this.jdbcTemplate = jdbcTemplate;
this.latch = latch;
this.sql = sql;
......@@ -65,6 +80,18 @@ public class SqlConcurrentController {
@Override
public void run() {
if(txTemplate != null) {
txTemplate.execute(new TransactionCallback<List<Object>>() {
public List<Object> doInTransaction(TransactionStatus status) {
return executeSql();
}
});
} else {
executeSql();
}
}
public List<Object> executeSql() {
try {
List<Object> argList = new ArrayList<Object>();
if(StringUtil.isNotBlank(parameters)) {
......@@ -86,7 +113,7 @@ public class SqlConcurrentController {
}
Object[] args = argList.toArray(new Object[argList.size()]);
jdbcTemplate.execute(new CallableStatementCreator() {
return 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++) {
......
......@@ -34,6 +34,9 @@
<el-form-item>
<el-input size="mini" placeholder="参数" v-model="form.parameters" style="width:300px;"></el-input>
</el-form-item>
<el-form-item>
<el-input size="mini" placeholder="参数" v-model="form.parameters" style="width:300px;"></el-input>
</el-form-item>
<el-form-item>
<el-button size="mini" type="primary" @click="doExecute">执行</el-button>
</el-form-item>
......
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