views:

343

answers:

6

So I know that autocommit commits every sql statement, but do updates to the database go directly to the disk or do they remain on cache until flushed?

I realize it's dependent on the database implementation.

Does auto-commit mean a) every statement is a complete transaction AND it goes straight to disk or b) every statement is a complete transaction and it may go to cache where it will be flushed later or it may go straight to disk

Clarification would be great.

Thanks in advance, jbu

+4  A: 

Auto-commit simply means that each statement is in its own transaction which commits immediately. This is in contrast to the "normal" mode, where you must explicitly BEGIN a transaction and then COMMIT once you are done (usually after several statements).

The phrase "auto-commit" has nothing to do with disk access or caching. As an implementation detail, most databases will write to disk on commit so as to avoid data loss, but this isn't mandatory in the spec.

Daniel Spiewak
+1  A: 

For ARIES-based protocols, committing a transaction involves logging all modifications made within that transaction. Changes are flushed immediately to logfile, but not necessarily to datafile (that is dependent on the implementation). That is enough to ensure that the changes can be recovered in the event of a failure. So, (b).

Justice
A: 

It's impossible to guarantee that commits are atomic, so modern databases use two-phase or three phase commit strategies. See Atomic Commit

Paul Tomblin
Not accurate. DBMS pay a lot of attention to ensuring that commits are atomic. The 2PC or 3PC protocols are for use when you have multiple servers at work - not for a single server.
Jonathan Leffler
ARIES can be said to be a form of two phase commit, and it is used on single servers.
Paul Tomblin
Can you provide a reliable, cited source? As of this writing, the Wikipedia article is just a stub with no citations.
James Schek
A: 

It depends on the DBMS you're using. For example, Firebird has it as an option in configuration file. If you turn Forced Writes on, the changes go directly to the disk. Otherwise they are submitted to the filesystem, and the actual write time depends on the operating system caching.

Milan Babuškov
+1  A: 

Commit provides no guarantee that something has been written to disk, only that your transaction has been completed and the changes are now visible to other users.

Permanent does not necessarily mean written to disk (i.e. durable)... Even if a "commit" waits for the transaction to complete can be configured with some databases.

For example, Oracle 10gR2 has several commit modes, including IMMEDIATE,WAIT,BATCH,NOWAIT. BATCH will queue the buffer the changes and the writer will write the changes to disk at some future time. NOWAIT will return immediately without regard for I/O.

The exact behavior of commmit is very database specific and can often be configured depending on your tolerance for data loss.

James Schek
A: 

If the database transaction is claimed to be ACID, then the D (durability) mandates that the transaction committed should survive the crash immediately after the successful commit. For single server database, that means it's on the disk (disk commit). For some modern multi-server databases, it can also means that the transaction is sent to one or more servers (network commit, which are typically much faster than disk), under the assumption that the probability of multiple server crash at the same time is much smaller.

ididak