Using transactions
In this recipe, you will learn what a database transaction is and how it can be used in Java code.
Getting ready
A transaction is a unit of work that includes one or many operations that change data. If successful, all the data changes are committed (applied to the database). If one of the operations errors out, the transaction is rolled back, and none of the changes included in the transaction will be applied to the database.
Transaction properties are set up on the Connection
object. They can be changed without closing the connection, so different transactions can reuse the same Connection
object.
JDBC allows transaction control only for CRUD operations. Table modification (CREATE TABLE
, ALTER TABLE
, and so on) is committed automatically and cannot be controlled from the Java code.
By default, a CRUD operation transaction is set to be auto-committed. This means that every data change that was introduced by a SQL statement is applied to the database as soon as the execution...