【发布时间】:2020-02-06 06:25:16
【问题描述】:
我有一个具有“解释器 SQL”功能的网络应用程序。用户可以编写一些命令并执行它。
例如,用户想要执行 3x 操作:
1. ALTER TABLE "aaab".WHATEVER RENAME TO "Something";
2. ALTER TABLE "aaab".RESOURCES RENAME TO "SomethingElse";
3. ALTER TABLE "aaab".EXAMCATEGORIES MODIFY NAME NUMBER;
我想要达到的目标:
- 如果操作 1、2、3 成功,则
commit所有 3x 操作 - 如果操作 1、2 成功但 3 不成功,则
rollback所有 3x 操作
一般来说,如果列表中的 any 操作不成功,那么我想 rollback 所有操作。
这是我的翻译:
public ArrayList<String> executeSQL(String[] queryRows) {
ArrayList<String> listException = new ArrayList<String>();
for (int i = 0; i < queryRows.length; ++i) {
String query = queryRows[i];
if(query.trim().length() > 5 && query.trim().substring(0, 6).toUpperCase().equals("SELECT")){
try{
mapList = jdbcTemplate.queryForList(query);
int rows = mapList.size();
listException.add("Success! { affected rows --> [" + rows + "] }");
updateFlag = true;
}catch (DataAccessException exceptionSelect){
listException.add(exceptionSelect.getCause().getMessage());
updateFlag = false;
break;
}
}
else if(whatKindOfStatementIsThat(query,"DDL")){
try{
jdbcTemplate.execute(query);
listException.add("Success!");
updateFlag = true;
}catch (DataAccessException exceptionDDL){
listException.add(exceptionDDL.getCause().getMessage());
updateFlag = false;
break;
}
}
else if (whatKindOfStatementIsThat(query,"DML")){
try {
int rows = jdbcTemplate.update(query);
listException.add("Success! { zaafektowane wiersze --> [" + rows + "] }");
updateFlag = true;
}catch (DataAccessException exceptionDML){
listException.add(exceptionDML.getCause().getMessage());
updateFlag = false;
break;
}
}
else{
try{
jdbcTemplate.execute(query);
listException.add("Success!");
updateFlag = true;
}catch (Exception exception){
listException.add(exception.getCause().getMessage());
updateFlag = false;
break;
}
}
}
return listException;
}
真的很简单,首先我检查输入了什么样的语句。
1. 如果语句是select,那么我需要结果列表mapList = jdbcTemplate.queryForList(query);
2. 如果语句是DDL,那么我不需要任何东西jdbcTemplate.execute(query);
3. 如果语句是DML,那么我需要受影响的行数int rows = jdbcTemplate.update(query);
4. 其他的,执行原生查询jdbcTemplate.execute(query);
我将我的语句保存在 ArrayList 中,它们在循环中一个接一个地执行。
例如,如果我有那个循环:
public void executeSQL (String[] queryRows){
for(int i = 0; i < queryRows.length; ++i){
// statements are executed there one after another.
}
}
我怎样才能实现这样的目标?
public void executeSQL(String[] queryRows){
...begin()
for(int i = 0; i < queryRows.length; ++i){
// statements are executed there one after another.
}
if(myCondition)
...commit()
else
...rollback()
}
【问题讨论】:
-
根据您的数据库,DDL 语句可能是事务性的,但通常不是。我知道 Postgres 确实支持这一点,但 Oracle 不支持。所以YMMV。
-
使用的数据库是什么? PostgreSQL ? mysql ?如果是 MySQL,引擎是什么? (InnoDb,MyIsam)
-
甲骨文............
标签: java spring hibernate jdbctemplate