Middle+
Tell about ACID — properties of transactions in databases.
sobes.tech AI
Answer from AI
ACID is a set of properties that guarantee reliable transactions in databases:
- Atomicity: the transaction is completed in full or not at all. If something goes wrong, all changes are rolled back.
- Consistency: after the transaction completes, the database remains in a correct state, satisfying all constraints and rules.
- Isolation: concurrent transactions do not affect each other; their results do not intersect as if they were executed sequentially.
- Durability: after a transaction is committed, changes are permanently saved, even in case of system failures.
Example in Go using database transactions:
trx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
_, err = trx.Exec("INSERT INTO users(name) VALUES(?)", "Alice")
if err != nil {
trx.Rollback() // rollback on error
log.Fatal(err)
}
err = trx.Commit() // commit the transaction
if err != nil {
log.Fatal(err)
}
Here, either the user will be added, or the changes will not be applied.