简体   繁体   中英

Rollback if one insert fails, when using ScriptRunner "runScript" method

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) {}
    }

Above is the snippet I'm using to execute the SQL script file.

If I have multiple inserts inside the script, I want to execute all the inserts inside a transaction, where If one fails, nothing should be written into the database.

And I want to handle this from the code, instead of the script.

Achieved this by setting the 'StopOnError' parameter to true.

        ScriptRunner sr = new ScriptRunner(con);
        sr.setStopOnError(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM