<<

Programs often need to perform multiple operations to achieve a goal. 1) Moving money between bank accounts 2) Adding or dropping a class 3) Removing a job agency applicant

Such a group of database operations is called a “Transaction”

Database transactions should have four properties (the ACID properties)

1) Atomic – All or none 2) Consistent – The sequence of operations takes the database from one consistent state to another 3) Isolated – Concurrently executing transactions are isolated from each other so that effects of one are invisible to the others. The result of executing multiple transactions concurrently must be the same as some serialized execution of the transactions 4) Durable – The changes made by a transaction are permanent

(Optional: R/W locks on rows are used to achieve serializability. Logging is used to achieve atomicity and durability – rollback & )

Commit and Rollback The effects of a sequence of related operations don’t become permanent until a SQL COMMIT command is executed

A partially-completed sequence of operations can be undone in case of error using the SQL ROLLBACK command

Automatic rollback is performed by the database in case of program failure, power failure, etc.

Grouping SQL Commands into Transactions Every SQL statement is executed as part of a transaction

By default, every SQL command you execute is part of a transaction. The database automatically starts a new transaction before executing the command, and automatically commits the transaction after executing the command. This is called “auto commit” mode. From the user’s perspective, transactions are invisible.

In order to group multiple commands into a single transaction, you must first turn off auto commit mode. The database still automatically starts a new transaction whenever the current transaction ends, but you now have control over when COMMIMT and ROLLBACK occur. Demo Start the database console and login to jdbc:pointbase://localhost:9092/CS462

Turn off auto commit in the SQL menu

Deleting an applicant from the job agency database requires three SQL operations: 1) Delete the applicant’s rows from the Matched 2) Delete the applicant’s rows from the ApplicantSkill table 3) Delete the applicant’s from the Applicant table

Execute the following SQL commands in the console:

delete from matched where applicant = 'juliet';

delete from applicantskill where applicant = 'juliet';

delete from applicant where login = 'juliet';

View the table contents to see the effects of these commands.

Execute a ROLLBACK command, and show how the effects of the previous commands have been undone.

rollback;

If we had executed a COMMIT command instead, the changes would have become permanent.

If the power had gone out, the database would automatically rollback the transaction the next time it started up.

Show the code in TransactionExample. that shows how to do the same thing through JDBC.