Atomic Transactions
By default, a newly created database Connection
object is in auto-commit mode.
That means that each update to the database is treated as a separate
transaction and is automatically committed to the database. Sometimes,
however, you want to group several updates into a single atomic
transaction, with the property that either all the updates complete
successfully or no updates occur at all. With a database system (and
JDBC driver) that supports it, you can take the Connection
out of auto-commit mode and
explicitly call commit( )
to commit
a batch of transactions or call rollback(
)
to abort a batch of transactions, undoing the ones that
have already been done. Example
21-3 in Chapter 21 uses
atomic transactions to ensure database consistency.
Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.