setAutocommit method
Set the autocommit mode.
When enabled, each DML statement (INSERT, UPDATE, DELETE) is automatically committed after execution. This is convenient but may impact performance for batch operations.
Parameters:
enabled: true to enable autocommit, false to disable (default)
Example - Enable Autocommit:
await connection.setAutocommit(true);
// This INSERT is automatically committed
await connection.executeUpdate(
'INSERT INTO users VALUES (?, ?)',
params: [1, 'Alice'],
);
// No need to call commit()
Example - Disable for Batch Operations:
await connection.setAutocommit(false);
try {
for (var i = 0; i < 1000; i++) {
await connection.executeUpdate(
'INSERT INTO logs VALUES (?, ?)',
params: [i, 'Log entry $i'],
);
}
await connection.commit(); // Single commit for all 1000 inserts
} catch (e) {
await connection.rollback();
}
Performance Note: Autocommit can be slower for bulk operations because each statement incurs a commit overhead. For best performance with batch operations, disable autocommit and commit once at the end.
See also:
- autocommit getter to check current mode
- commit for manual commits
- rollback to undo changes
Implementation
Future<void> setAutocommit(bool enabled) async {
_ensureConnected();
_autocommit = enabled;
}