SQL工作台:添加暂停、等待提交/回滚超时界面设置和处理逻辑

This commit is contained in:
datagear 2019-03-26 20:57:55 +08:00
parent 09b9b63f64
commit cb912eba5b
4 changed files with 135 additions and 31 deletions

View File

@ -98,8 +98,8 @@ public class SqlpadController extends AbstractSchemaConnController
@RequestParam(value = "sqlStartRow", required = false) Integer sqlStartRow,
@RequestParam(value = "sqlStartColumn", required = false) Integer sqlStartColumn,
@RequestParam(value = "commitMode", required = false) CommitMode commitMode,
@RequestParam(value = "exceptionHandleMode", required = false) ExceptionHandleMode exceptionHandleMode)
throws Throwable
@RequestParam(value = "exceptionHandleMode", required = false) ExceptionHandleMode exceptionHandleMode,
@RequestParam(value = "overTimeThreashold", required = false) Integer overTimeThreashold) throws Throwable
{
Schema schema = getSchemaNotNull(request, response, schemaId);
@ -115,10 +115,17 @@ public class SqlpadController extends AbstractSchemaConnController
if (exceptionHandleMode == null)
exceptionHandleMode = ExceptionHandleMode.ABORT;
if (overTimeThreashold == null)
overTimeThreashold = 10;
else if (overTimeThreashold < 1)
overTimeThreashold = 1;
else if (overTimeThreashold > 60)
overTimeThreashold = 60;
List<SqlStatement> sqlStatements = sqlScriptParser.parse();
this.sqlpadExecutionService.submit(sqlpadId, schema, sqlStatements, commitMode, exceptionHandleMode,
WebUtils.getLocale(request));
overTimeThreashold, WebUtils.getLocale(request));
return buildOperationMessageSuccessEmptyResponseEntity();
}

View File

@ -96,16 +96,17 @@ public class SqlpadExecutionService
* @param sqlStatements
* @param commitMode
* @param exceptionHandleMode
* @param overTimeThreashold
* @param locale
* @return
*/
public boolean submit(String sqlpadId, Schema schema, List<SqlStatement> sqlStatements, CommitMode commitMode,
ExceptionHandleMode exceptionHandleMode, Locale locale)
ExceptionHandleMode exceptionHandleMode, int overTimeThreashold, Locale locale)
{
String sqlpadChannelId = getSqlpadChannelId(sqlpadId);
SqlpadExecutionRunnable sqlpadExecutionRunnable = new SqlpadExecutionRunnable(schema, sqlpadId, sqlpadChannelId,
sqlpadCometdService, sqlStatements, commitMode, exceptionHandleMode, locale);
sqlpadCometdService, sqlStatements, commitMode, exceptionHandleMode, overTimeThreashold, locale);
SqlpadExecutionRunnable old = this._sqlpadExecutionRunnableMap.putIfAbsent(sqlpadId, sqlpadExecutionRunnable);
@ -222,6 +223,9 @@ public class SqlpadExecutionService
private ExceptionHandleMode exceptionHandleMode;
/** 超时分钟数 */
private int overTimeThreashold;
private Locale locale;
/** 发送给此Runnable的SQL命令 */
@ -236,7 +240,7 @@ public class SqlpadExecutionService
public SqlpadExecutionRunnable(Schema schema, String sqlpadId, String sqlpadChannelId,
SqlpadCometdService sqlpadCometdService, List<SqlStatement> sqlStatements, CommitMode commitMode,
ExceptionHandleMode exceptionHandleMode, Locale locale)
ExceptionHandleMode exceptionHandleMode, int overTimeThreashold, Locale locale)
{
super();
this.schema = schema;
@ -246,6 +250,7 @@ public class SqlpadExecutionService
this.sqlStatements = sqlStatements;
this.commitMode = commitMode;
this.exceptionHandleMode = exceptionHandleMode;
this.overTimeThreashold = overTimeThreashold;
this.locale = locale;
}
@ -319,6 +324,16 @@ public class SqlpadExecutionService
this.exceptionHandleMode = exceptionHandleMode;
}
public int getOverTimeThreashold()
{
return overTimeThreashold;
}
public void setOverTimeThreashold(int overTimeThreashold)
{
this.overTimeThreashold = overTimeThreashold;
}
public Locale getLocale()
{
return locale;
@ -469,15 +484,30 @@ public class SqlpadExecutionService
hasPaused = true;
if (sendMessageIfPause)
sendSqlCommandMessage(this.sqlCommand);
sendSqlCommandMessage(this.sqlCommand, this.overTimeThreashold);
// TODO 添加超时处理逻辑
while (SqlCommand.PAUSE.equals(this.sqlCommand))
long waitStartTime = System.currentTimeMillis();
while (SqlCommand.PAUSE.equals(this.sqlCommand)
&& (System.currentTimeMillis() - waitStartTime) <= this.overTimeThreashold * 60 * 1000)
sleepForSqlCommand();
// 暂停超时
if (SqlCommand.PAUSE.equals(this.sqlCommand))
{
this.sqlpadCometdService.sendTextMessage(this._sqlpadServerChannel,
getMessage(this.locale, "sqlpad.pauseOverTime"));
this.sqlCommand = SqlCommand.RESUME;
}
}
if (SqlCommand.RESUME.equals(this.sqlCommand))
;
{
sendSqlCommandMessage(this.sqlCommand);
this.sqlCommand = null;
}
else if (SqlCommand.STOP.equals(this.sqlCommand))
{
cn.rollback();
@ -530,14 +560,16 @@ public class SqlpadExecutionService
{
boolean sendWatingMessage = false;
// TODO 添加超时处理逻辑
while (!SqlCommand.COMMIT.equals(this.sqlCommand) && !SqlCommand.ROLLBACK.equals(this.sqlCommand))
long waitStartTime = System.currentTimeMillis();
while (!SqlCommand.COMMIT.equals(this.sqlCommand) && !SqlCommand.ROLLBACK.equals(this.sqlCommand)
&& (System.currentTimeMillis() - waitStartTime) <= this.overTimeThreashold * 60 * 1000)
{
if (!sendWatingMessage)
{
this.sqlpadCometdService.sendTextMessage(this._sqlpadServerChannel,
getMessage(this.locale, "sqlpad.waitingForCommitOrRollback"), "message-content-highlight",
sqlExecutionStat);
getMessage(this.locale, "sqlpad.waitingForCommitOrRollback", this.overTimeThreashold),
"message-content-highlight", sqlExecutionStat);
sendWatingMessage = true;
}
@ -545,6 +577,15 @@ public class SqlpadExecutionService
sleepForSqlCommand();
}
// 等待超时
if (!SqlCommand.COMMIT.equals(this.sqlCommand) && !SqlCommand.ROLLBACK.equals(this.sqlCommand))
{
this.sqlpadCometdService.sendTextMessage(this._sqlpadServerChannel,
getMessage(this.locale, "sqlpad.waitOverTime"));
this.sqlCommand = (sqlExecutionStat.getExceptionCount() > 0 ? SqlCommand.ROLLBACK : SqlCommand.COMMIT);
}
if (SqlCommand.COMMIT.equals(this.sqlCommand))
{
cn.commit();
@ -588,13 +629,14 @@ public class SqlpadExecutionService
* 发送命令已执行消息
*
* @param sqlCommand
* @param messageArgs
*/
protected void sendSqlCommandMessage(SqlCommand sqlCommand)
protected void sendSqlCommandMessage(SqlCommand sqlCommand, Object... messageArgs)
{
String messageKey = "sqlpad.SqlCommand." + sqlCommand.toString() + ".ok";
this.sqlpadCometdService.sendSqlCommandMessage(this._sqlpadServerChannel, sqlCommand,
getMessage(this.locale, messageKey));
getMessage(this.locale, messageKey, messageArgs));
}
/**

View File

@ -353,6 +353,10 @@ sqlpad.sqlExceptionHandleMode.ignore=\u5FFD\u7565
sqlpad.sqlCommitMode=\u63D0\u4EA4\u65B9\u5F0F
sqlpad.sqlCommitMode.auto=\u81EA\u52A8
sqlpad.sqlCommitMode.manual=\u624B\u52A8
sqlpad.overTimeThreashold=\u8D85\u65F6\u8BBE\u7F6E
sqlpad.overTimeThreashold.unit=\u5206\u949F
sqlpad.overTimeThreashold.desc=\u6682\u505C\u3001\u7B49\u5F85\u63D0\u4EA4/\u56DE\u6EDA\u7684\u8D85\u65F6\u65F6\u95F4
sqlpad.overTimeThreashold.validation=\u4EC5\u53EF\u586B\u5199[1-60]
sqlpad.executionStart=\u5F00\u59CB\u6267\u884C
sqlpad.executeionFinish=\u5B8C\u6210\u6267\u884C
sqlpad.clearSqlResult=\u6E05\u9664\u6267\u884C\u65E5\u5FD7
@ -363,10 +367,12 @@ sqlpad.executionSQLException=\u6267\u884C\u8BED\u53E5\u51FA\u9519
sqlpad.executionErrorOccure=\u6267\u884C\u51FA\u9519
sqlpad.SqlCommand.COMMIT.ok=\u5DF2\u63D0\u4EA4
sqlpad.SqlCommand.ROLLBACK.ok=\u5DF2\u56DE\u6EDA
sqlpad.SqlCommand.PAUSE.ok=\u5DF2\u6682\u505C
sqlpad.SqlCommand.PAUSE.ok=\u5DF2\u6682\u505C\uFF08{0}\u5206\u949F\u540E\u8D85\u65F6\uFF09
sqlpad.SqlCommand.RESUME.ok=\u5DF2\u7EE7\u7EED
sqlpad.SqlCommand.STOP.ok=\u5DF2\u505C\u6B62\u5E76\u56DE\u6EDA
sqlpad.waitingForCommitOrRollback=\u7B49\u5F85\u63D0\u4EA4\u6216\u8005\u56DE\u6EDA
sqlpad.waitingForCommitOrRollback=\u7B49\u5F85\u63D0\u4EA4\u6216\u8005\u56DE\u6EDA\uFF08{0}\u5206\u949F\u540E\u8D85\u65F6\uFF09
sqlpad.pauseOverTime=\u6682\u505C\u8D85\u65F6
sqlpad.waitOverTime=\u7B49\u5F85\u8D85\u65F6
sqlpad.sqlExecutionStat.quoteLeft=\uFF08
sqlpad.sqlExecutionStat.quoteRight=\uFF09
sqlpad.sqlExecutionStat.infoNoDuration=SQL\u603B\u8BA1{0}\u6761\uFF0C\u6210\u529F{1}\u6761\uFF0C\u5931\u8D25{2}\u6761\uFF0C\u672A\u6267\u884C{3}\u6761

View File

@ -30,7 +30,7 @@ Schema schema 数据库不允许为null
<div class="more-operation-wrapper">
<button id="moreOperationButton" class="ui-button ui-corner-all ui-widget ui-button-icon-only" title="<@spring.message code='sqlpad.moreOperation' />"><span class="ui-button-icon ui-icon ui-icon-triangle-1-s"></span><span class="ui-button-icon-space"> </span><@spring.message code='sqlpad.moreOperation' /></button>
<div class="more-operation-panel ui-widget ui-widget-content ui-corner-all ui-widget-shadow">
<form action="#">
<form id="moreOperationForm" method="POST" action="#">
<div class="form-content">
<div class="form-item">
<div class="form-item-label"><label><@spring.message code='sqlpad.sqlCommitMode' /></label></div>
@ -51,6 +51,13 @@ Schema schema 数据库不允许为null
</div>
</div>
</div>
<div class="form-item">
<div class="form-item-label"><label><@spring.message code='sqlpad.overTimeThreashold' /></label></div>
<div class="form-item-value">
<input type="text" name="overTimeThreashold" value="10" class="ui-widget ui-widget-content" style="width:4em;" title="<@spring.message code='sqlpad.overTimeThreashold.desc' />" />
<@spring.message code='sqlpad.overTimeThreashold.unit' />
</div>
</div>
</div>
</form>
</div>
@ -150,11 +157,11 @@ select count(*) from t_order where id = 3 and name = 'jack';
}
});
po.executeSql = function(sql, sqlStartRow, sqlStartColumn, commitMode, exceptionHandleMode)
po.executeSql = function(sql, sqlStartRow, sqlStartColumn, commitMode, exceptionHandleMode, overTimeThreashold)
{
if(po.cometdSubscribed)
{
po.requestExecuteSql(sql, sqlStartRow, sqlStartColumn, commitMode, exceptionHandleMode);
po.requestExecuteSql(sql, sqlStartRow, sqlStartColumn, commitMode, exceptionHandleMode, overTimeThreashold);
}
else
{
@ -170,13 +177,13 @@ select count(*) from t_order where id = 3 and name = 'jack';
{
po.cometdSubscribed = true;
po.requestExecuteSql(sql, sqlStartRow, sqlStartColumn, commitMode, exceptionHandleMode);
po.requestExecuteSql(sql, sqlStartRow, sqlStartColumn, commitMode, exceptionHandleMode, overTimeThreashold);
}
});
}
};
po.requestExecuteSql = function(sql, sqlStartRow, sqlStartColumn, commitMode, exceptionHandleMode)
po.requestExecuteSql = function(sql, sqlStartRow, sqlStartColumn, commitMode, exceptionHandleMode, overTimeThreashold)
{
if(!po.element("#toggleAutoClearResultButton").hasClass("ui-state-active"))
po.sqlResultContentElement.empty();
@ -192,7 +199,8 @@ select count(*) from t_order where id = 3 and name = 'jack';
"sqlStartRow" : sqlStartRow,
"sqlStartColumn" : sqlStartColumn,
"commitMode" : commitMode,
"exceptionHandleMode" : exceptionHandleMode
"exceptionHandleMode" : exceptionHandleMode,
"overTimeThreashold" : overTimeThreashold
},
error : function()
{
@ -302,8 +310,24 @@ select count(*) from t_order where id = 3 and name = 'jack';
}
else if(msgDataType == "SQLCOMMAND")
{
$("<span />").html(msgData.content).appendTo($msgContent);
po.appendSQLExecutionStatMessage($msgContent, msgData.sqlExecutionStat);
var appendContent = true;
if(msgData.sqlCommand == "RESUME")
{
po.updateExecuteSqlButtonState(po.element("#executeSqlButton"), "executing");
appendContent = false;
$msgDiv = null;
}
else if(msgData.sqlCommand == "PAUSE")
{
po.updateExecuteSqlButtonState(po.element("#executeSqlButton"), "paused");
}
if(appendContent)
{
$("<span />").html(msgData.content).appendTo($msgContent);
po.appendSQLExecutionStatMessage($msgContent, msgData.sqlExecutionStat);
}
}
else if(msgDataType == "EXCEPTION")
{
@ -389,13 +413,10 @@ select count(*) from t_order where id = 3 and name = 'jack';
if(executionState == "executing")
{
po.updateExecuteSqlButtonState($this, "paused");
po.sendSqlCommand("PAUSE", $this);
}
else if(executionState == "paused")
{
po.updateExecuteSqlButtonState($this, "executing");
po.sendSqlCommand("RESUME", $this);
}
else
@ -419,6 +440,14 @@ select count(*) from t_order where id = 3 and name = 'jack';
var commitMode = po.element("input[name='sqlCommitMode']:checked").val();
var exceptionHandleMode = po.element("input[name='sqlExceptionHandleMode']:checked").val();
var overTimeThreashold = parseInt(po.element("input[name='overTimeThreashold']").val());
if(isNaN(overTimeThreashold))
overTimeThreashold = 10;
else if(overTimeThreashold < 1)
overTimeThreashold = 1;
else if(overTimeThreashold > 60)
overTimeThreashold = 60;
var cometd = $.cometd;
@ -431,12 +460,12 @@ select count(*) from t_order where id = 3 and name = 'jack';
{
if(handshakeReply.successful)
{
po.executeSql(sql, sqlStartRow, sqlStartColumn, commitMode, exceptionHandleMode);
po.executeSql(sql, sqlStartRow, sqlStartColumn, commitMode, exceptionHandleMode, overTimeThreashold);
}
});
}
else
po.executeSql(sql, sqlStartRow, sqlStartColumn, commitMode, exceptionHandleMode);
po.executeSql(sql, sqlStartRow, sqlStartColumn, commitMode, exceptionHandleMode, overTimeThreashold);
}
po.sqlEditor.focus();
@ -521,6 +550,26 @@ select count(*) from t_order where id = 3 and name = 'jack';
po.sqlResultContentElement.empty();
});
po.element("#moreOperationForm").validate(
{
rules :
{
overTimeThreashold : { required : true, integer : true, min : 1, max : 60 }
},
messages :
{
overTimeThreashold : "<@spring.message code='sqlpad.overTimeThreashold.validation' />"
},
submitHandler : function(form)
{
return false;
},
errorPlacement : function(error, element)
{
error.appendTo(element.closest(".form-item-value"));
}
});
po.element("input[name='sqlCommitMode'][value='AUTO']").click();
})
(${pageId});