Take Assessment: Exercise 5: Programming With Transactions
Take Assessment: Exercise 5: Programming With Transactions
Take Assessment: Exercise 5: Programming With Transactions
Almost all commercial-strength DBMS software supports the ability to undo the effects
of a transaction before those effects are committed to the database. This is particularly
useful when an operation must be performed to completion or not performed at all. You
may undo these effects on the data using rollbacks. This portion of the exercise will
familiarize you with rollbacks and commits.
1. Run the SQL script provided to build a small bank database consisting of two
columns: an account id number and a balance. There are two accounts, a
savings account and a checking account. The savings account has id = 1 and
the checking account has id = 2.
2. Begin a new transaction.
3. Select balance of the checking account and paste the output in a file named
rollback.txt.
4. Delete both the savings and the checking accounts. Place the SQL query to
perform this operation in rollback.txt.
5. Select all of the data in the account table and paste the output in rollback.txt.
6. Roll back the transaction.
7. Select the balance of the savings and checking accounts and paste the output
in rollback.txt.
8. Begin a new transaction.
9. Delete the savings account. Place the SQL code to do this in rollback.txt.
10. Select all rows from the account table and paste the output in rollback.txt.
11. Commit the transaction.
12. Attempt to rollback the transaction that you have just committed. What values
are stored in the tables? Please explain the effects of transaction commits and
rollbacks you have observed above. Place your explanation in rollback.txt.
1. Run the SQL script provided to build a small bank database consisting of two
columns: an account id number and a balance. There are two accounts, a
savings account and a checking account. The savings account has id = 1 and
the checking account has id = 2.
2. Begin two sessions of the PostgreSQL client in two separate console windows.
3. Begin new transactions in both windows.
4. In the first window, update the checking account to have a balance of 455.66.
5. In the first window, select all of the data from the account table and place the
output into the file isolation.txt. Be sure to label the data so that it is clear this
data is from the first table.
6. In the second window, select all of the data from the account table and place
the output into the file isolation.txt. Be sure to label the data so that it is clear
this is data from the second window.
7. Commit the transaction in the first window to update the account table.
8. In the second window, select all of the data from the account table and place
the output into the file isolation.txt. Be sure to label the data so that it is clear
this is data from the second window. What has changed about the data and
why? Place the answer to this question in isolation.txt.
9. Commit the transaction in second window.
10. In both windows, begin new transactions.
11. Set the transaction isolation level of the transaction in the second window to
serializable. Place the code to do this in isolation.txt.
12. In the first window, set the balance of checking account balance to 1400.00.
13. In the second window, set the balance of savings account balance to 1.
14. Select all data from the account table in the first and second window and place
it into isolation.txt. Be sure to label the data clearly to denote what data came
from the first and second windows.
15. Commit the data in the first window.
16. Select all of the data from the account table in the second window. Has the
data for the checking account changed? Why or why not? Has the data for the
savings account changed in the first window? Why or why not? Places the
answers to these questions in isolation.txt
17. Commit the data in the second window. Display all of the data from the account
table in both windows. Place the output of both windows into isolation.txt.
Clearly label which data is from the first window and which data is from the
second window. What do you notice now about the balances of the checking
and savings accounts? Please explain. Place your explanation in isolation.txt.
Blocking and Deadlocks
In this portion of the exercise, you will cause two transactions to block and deadlock
two transactions in PostgreSQL.
1. Run the SQL script provided to build a small bank database consisting of two
columns: an account id number and a balance. There are two accounts, a
savings account and a checking account. The savings account has id = 1 and
the checking account has id = 2.
2. Begin two sessions of the PostgreSQL client in two separate console windows.
3. Begin new transactions in both windows.
4. Update the checking account's balance to 455.75 in the second window.
5. Update the checking account's balance to 1400.00 in the first window. Does the
update occur? Why or why not? Place your answer in deadlock.txt.
6. Commit the transaction in the second window. What do you notice happening in
the first window? Please explain. Place your answer in deadlock.txt.
7. Commit the transaction in the first window.
8. Begin a new transaction in each window.
9. Update the savings account balance to 2400.00 in the second window.
10. Update the checking account balance to 2000.00 in the first window.
11. Update the savings account balance to 1400.00 in the first window.
12. Update the checking account balance to 1000.50 in the second window.
13. What happens to the transactions? Why? Place your answer in deadlock.txt.
14. Try selecting the all of the data from the account table in each window. What
do you notice? Place your answer in deadlock.txt.
Application
The final exercises will test your knowledge of transactions and their behavior in real
world situations.
1. Suppose William and Julie share a savings and a checking account at a bank.
The bank has many automatic teller machines. William banks from one ATM
while Julie banks at another ATM. William wishes first to deposit four hundred
dollars into the savings account and then to transfer three hundred dollars from
the checking account to the savings account. Julie wishes first to deposit a
check for five hundred dollars into the checking account and then withdraw one
hundred dollars from the savings account.
o Select the current account balance for the customer from the database.
o Decrease this account balance by the withdrawal amount.
o Update the customer's account balance in the database to the new
amount.
o Select the current account balance for the account from where the
funds are transferred.
o Decrease the current balance by the amount transferred.
o Select the balance of the account to where the funds are being
transferred.
o Increase the balance of the account where money is transferred.
o Update both account balances in the database.
For each of these problems, you must state why they are a problem.
We will provide with help on one of them and you must explain the
rest.
The confirmation messages are placed such that a user can receive a
confirmation message before the transaction ends. This is a problem
because a transaction may roll back but the user believes his
transaction has ended successfully. All confirmation messages must be
displayed after a transaction has been committed.
3. Scenario B
William Julie
ATM prompts user for ATM prompts user for
operation. operation.
William Chooses Deposit Julie Chooses Deposit into
into Savings. Checking.
Begin Begin
transaction (read transaction (read
committed isolation committed isolation
level) level)
ATM prompts user for ATM prompts user for
amount. amount.
William enters 400. Julie enters 500.
Select Select
balance of savings balance of checking
savings_balance = checking_balance =
savings_balance + $400 checking_balance + $500
Update Update
savings balance in checking balance in
database. database.
End End
Transaction Transaction
ATM displays confirmation ATM displays confirmation
of deposit. of deposit.
ATM prompts user for ATM prompts user for
operation. operation.
William chooses transfer Julie chooses withdrawal
from checking to savings. from savings.
Begin Begin
transaction (read transaction (read
committed isolation committed isolation
level) level)
ATM prompts user for ATM prompts user for
amount. amount.
William enters 300. Julie enters 100.
Select Select
the checking the savings checking
balance. balance in database.
checking_balance = savings_balance =
checking_balance - $300 savings_balance - $100
Update
Select
savings balance in
the savings balance.
database.
savings_balance = End
savings_balance + $300 Transaction
Update
ATM displays confirmation
savings balance in
of withdrawal.
database.
Update
ATM prompts user for
checking balance in
operation.
database.
End Julie chooses no more
Transaction operations.
ATM displays confirmation
of transfer.
ATM prompts user for
operation.
William chooses no more
operations.
4. Scenario C
William Julie
ATM prompts user for ATM prompts user for
operation. operation.
William Chooses Deposit Julie Chooses Deposit into
into Savings. Checking.
Begin Begin
transaction transaction
(serializable (serializable
isolation level) isolation level)
ATM prompts user for ATM prompts user for
amount. amount.
William enters 400. Julie enters 500.
Select Select
balance of savings balance of checking
savings_balance = checking_balance =
savings_balance + $400 checking_balance + $500
Update Update
savings balance in checking balance in
database. database.
End End
Transaction Transaction
ATM displays confirmation ATM displays confirmation
of deposit. of deposit.
ATM prompts user for ATM prompts user for
operation. operation.
William chooses transfer Julie chooses withdrawal
from checking to savings. from savings.
Begin Begin
transaction transaction
(serializable (serializable
isolation level) isolation level)
ATM prompts user for ATM prompts user for
amount. amount.
William enters 300. Julie enters 100.
Select Select
the checking the savings checking
balance. balance in database.
checking_balance = savings_balance =
checking_balance - $300 savings_balance - $100
Update
Select
savings balance in
the savings balance.
database.
savings_balance = End
savings_balance + $300 Transaction
Update
ATM displays confirmation
savings balance in
of withdrawal.
database.
Update
ATM prompts user for
checking balance in
operation.
database.
End Julie chooses no more
Transaction operations.
ATM displays confirmation
of transfer.
ATM prompts user for
operation.
William chooses no more
operations.
5. You must write the final sequence of events that lead to a correct
execution while allowing the maximum amount of concurrency. Be sure
to state each step for both William and Julie. Also, state what actions
are handled by the DBMS and what actions are handled by the ATM
machine.
The table structure for this scenario along with some sample data is given
below. Create a new database in PostgreSQL named movie and run the SQL
script provided to build these tables.
Movies
Movie Id (PK) Movie Title
001 The Terminator
002 James Bond
003 The Matrix
Movie Showings
Movie Id Movie Total Available Standard
(PK,FK) Time(PK) Seats Seats
001 1:00 PM 40 25
001 4:00 PM 30 27
002 12:30 PM 50 32
002 7:30 PM 75 17
003 9:30 PM 100 10
You must modify the Java console application provided that simulates this
scenario using PostgreSQL meaning that you can only use those isolation levels
supported by PostgreSQL. This application sets incorrect transaction boundaries
and uses an incorrect isolation level. Your task is to correct these mistakes by
moving the transaction boundaries to where they allow the best performance
for all system users. Note that you may use more than one transaction in your
solution. For each transaction that is used in your solution, you must include
the following: