rollback method
Rollback the current transaction, discarding all uncommitted changes.
Undoes all data changes since the last commit or rollback. Use this when an error occurs or when you want to cancel pending changes.
Example - Error Recovery:
try {
await connection.executeUpdate('UPDATE employees SET salary = :1', params: [value]);
// Validate the change
final results = await connection.query('SELECT SUM(salary) FROM employees');
if (results[0]['SUM(SALARY)'] > maxBudget) {
await connection.rollback(); // Cancel if over budget
throw Exception('Salary budget exceeded');
}
await connection.commit();
} catch (e) {
await connection.rollback(); // Undo changes on error
rethrow;
}
Example - Canceling Changes:
// Make some test changes
await connection.executeUpdate('DELETE FROM test_data');
final results = await connection.query('SELECT COUNT(*) FROM test_data');
print('Deleted ${results[0]['COUNT(*)']} rows');
// Discard the changes
await connection.rollback();
print('Changes discarded');
Note: Rollback only affects uncommitted changes. Once you call commit, changes are permanent and cannot be rolled back.
See also:
- commit to make changes permanent
- executeUpdate for DML operations
Implementation
Future<void> rollback() async {
_ensureConnected();
final result = _dpiOracle.dpiConn_rollback(_connectionPtr.value);
if (result == DPI_FAILURE) {
final errorInfo = _memoryManager.allocate<dpiErrorInfo>(sizeOf<dpiErrorInfo>());
_dpiOracle.dpiContext_getError(_context, errorInfo);
final errorMsg = StringUtils.fromNativeUtf8(errorInfo.ref.message.cast<Char>());
throw OracleConnectionException(
'Failed to rollback transaction',
errorMessage: errorMsg,
);
}
}