Connection con = null;
try {
con = datasource.getConnection();
ScriptRunner sr = new ScriptRunner(con);
Reader reader = new BufferedReader(new FileReader("filePath");
sr.runScript(reader);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (con!=null) try {con.close();}catch (Exception ignore) {}
}
上面是我用来执行 SQL 脚本文件的代码片段。
如果我在脚本内有多个插入,我想执行一个事务内的所有插入,如果其中一个失败,则不应将任何内容写入数据库。
我想通过代码而不是脚本来处理这个问题。
最佳答案
通过将“StopOnError”参数设置为 true 来实现此目的。
ScriptRunner sr = new ScriptRunner(con);
sr.setStopOnError(true);
关于java - 使用 ScriptRunner "runScript"方法时,如果一次插入失败则回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59730342/