commit method
Commit the current transaction, making all changes permanent.
Makes all data changes since the last commit or rollback permanent. Oracle Database uses explicit transaction control - changes are not automatically saved until you call commit.
Example - Basic Commit:
await connection.executeUpdate(
'INSERT INTO employees (id, name) VALUES (:1, :2)',
params: [101, 'Alice'],
);
await connection.commit(); // Make change permanent
Example - Transaction with Multiple Operations:
try {
// Transfer money between accounts
await connection.executeUpdate(
'UPDATE accounts SET balance = balance - :1 WHERE id = :2',
params: [100, accountFrom],
);
await connection.executeUpdate(
'UPDATE accounts SET balance = balance + :1 WHERE id = :2',
params: [100, accountTo],
);
// Both updates succeed - commit the transaction
await connection.commit();
print('Transfer successful');
} catch (e) {
// If any operation fails, rollback all changes
await connection.rollback();
print('Transfer failed: $e');
}
Best Practice: Always commit after DML operations (INSERT, UPDATE, DELETE) unless you explicitly want to discard changes with rollback.
See also:
- rollback to discard uncommitted changes
- executeUpdate for DML operations
Implementation
Future<void> commit() async {
_ensureConnected();
final result = _dpiOracle.dpiConn_commit(_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 commit transaction',
errorMessage: errorMsg,
);
}
}