Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
S
sentinel
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
曲欣亮
sentinel
Commits
55f1f662
Commit
55f1f662
authored
Jul 30, 2019
by
Quxl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
x
parent
bce81833
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
34 deletions
+46
-34
SqlConcurrentController.java
.../java/com/egolm/sentinel/web/SqlConcurrentController.java
+46
-34
No files found.
src/main/java/com/egolm/sentinel/web/SqlConcurrentController.java
View file @
55f1f662
...
...
@@ -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,30 +36,34 @@ 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
>();
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
;
List
<
String
>
stack
=
new
ArrayList
<
String
>();
Long
start
=
System
.
currentTimeMillis
();
try
{
dataSource
=
this
.
getDataSource
(
jdbcUrl
,
username
,
password
);
TransactionTemplate
txTemplate
=
null
;
if
(
isOpenTx
)
{
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
,
txTemplate
,
latch
,
sql
,
list
));
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
;
try
{
dataSource
=
this
.
getDataSource
(
jdbcUrl
,
username
,
password
);
TransactionTemplate
txTemplate
=
null
;
if
(
isOpenTx
)
{
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
,
txTemplate
,
latch
,
sql
,
list
,
stack
));
}
latch
.
await
();
}
finally
{
dataSource
.
close
();
}
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
);
...
...
@@ -66,6 +71,7 @@ public class SqlConcurrentController {
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
;
if
(
txTemplate
!=
null
)
{
result
=
txTemplate
.
execute
(
new
TransactionCallback
<
List
<
Object
>>()
{
public
List
<
Object
>
doInTransaction
(
TransactionStatus
status
)
{
return
executeSql
();
}
});
}
else
{
result
=
executeSql
();
try
{
if
(
txTemplate
!=
null
)
{
txTemplate
.
execute
(
new
TransactionCallback
<
List
<
Object
>>()
{
public
List
<
Object
>
doInTransaction
(
TransactionStatus
status
)
{
executeSql
();
return
null
;
}
});
}
else
{
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
();
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment