org.springframework.jdbc数据库回滚不会还原状态

时间:2017-06-25 09:42:39

标签: java database exception jdbc rollback

我尝试回滚我的数据库更改

roollback代码运行时没有异常,但我的数据库因更改而变脏。

我错过了什么吗?

    final Connection dbConnection = rulesUiRepository.getConnection();
    dbConnection.setAutoCommit(false);
    try {

        if (rulesUiRepository.updateRulesUiSnapshot(this.nonSplittedRulesSnapshot) == -1)
            throw new RuntimeException("cannot save ui snapshot to DB");

       ...more code

    } catch (Exception e) {
        logger.error("transaction to update db and cofman failed", e);

        //did work
        //dbConnection.rollback();

        throw new Exception("transaction to update db and cofman failed", e);

    } finally {
        //or
        if (dbConnection != null) {
            dbConnection.close();
        }
    }

使用该代码:

public synchronized void rollback() throws SQLException {
    try {
        this.txn_known_resolved = true;
        this.inner.rollback();
    } catch (NullPointerException var2) {
        if(this.isDetached()) {
            throw SqlUtils.toSQLException("You can't operate on a closed Connection!!!", var2);
        } else {
            throw var2;
        }
    } catch (Exception var3) {
        if(!this.isDetached()) {
            throw this.parentPooledConnection.handleThrowable(var3);
        } else {
            throw SqlUtils.toSQLException(var3);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

Rollbak自上次提交后做回滚。你有代码没有显示可以通过使用@Transactional或者显式地隐式提交,而回滚事务只对数据库事务后生效