Sobes.tech
Senior

In which case does a write to disk occur in the database?

sobes.tech AI

Answer from AI

Writing to disk in a database occurs at the moment of transaction commit. Before that, changes may reside in memory (cache, buffers), and only after a successful commit are the data guaranteed to be saved to disk.

Depending on the DBMS and settings, writing can occur:

  • When the transaction commit is called.
  • During automatic commit (auto-commit) after each operation.
  • During periodic buffer flushes to disk (checkpoint).

For example, in Java using JDBC:

Connection conn = dataSource.getConnection();
conn.setAutoCommit(false); // disable auto-commit
// perform operations
conn.commit(); // here the data is written to disk