I'm trying to learn more about database transactions, I found the ACID rule of thumb for writing transactions and thought of a few questions.
The ACID rule of thumb:
A transaction must be:
- Atomic - it is one unit of work and does not dependent on previous and following transactions.
- Consistent - data is either committed or roll back, no “in-between” case where something has been updated and something hasn’t.
- Isolated - no transaction sees the intermediate results of the current transaction.
- Durable - the values persist if the data had been committed even if the system crashes right after.
I was wondering how they work under the hood, so I can better understand the factors that need to be considered when writing such a transaction. I guess the specific details will vary between the database implementations that are avaliable, but certain rules will always be in place.
A transaction is a logical unit of work (comprising one or more SQL statements) performed on the database to complete a common task and maintain data consistency. Transaction statements are closely related and perform interdependent actions.
Steps in a TransactionLocate the record to be updated from secondary storage. Transfer the block disk into the memory buffer. Make the update to tuple in the buffer buffer. Write the modified block back out to disk.
There are many different ways, including transaction queueing, optimistic concurrency control etc. This is actually a very complex question, there are books written about it:
http://www.amazon.co.uk/Databases-Transaction-Processing-Application-Oriented-Approach/dp/0201708728/ref=sr_1_3?ie=UTF8&s=books&qid=1281609705&sr=8-3
It depends on the level of logging in the database. If strict write-ahead logs are kept then in the case of a system crash, the database can be wound back to a consistent state.
It depends on the type of concurrency. Optimistic concurrency involves no locks, but if the state of the db has changed once the transaction has finished, it is abandoned and restarted. This can speed up dbs where collisions are rare. There are also different levels of locking: row,table, or even the entire db.
These are very complex questions, I'd advise buying a book, or attending a concurrent systems lecture series if you want to be able to fully answer them :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With