============================================
Schema reposted from previous block for convenience:
Tracks(trackId, title, length)
length > 0
Artists(artistId, name)
Albums(albumId, title, yearReleased)
TracksOnAlbum(album, trackNr, track)
album -> Albums.albumId
track -> Tracks.trackId
(album, track) unique
trackNr > 0
Participates(track, artist)
track -> Tracks.trackId
artist -> Artists.artistId
Users(username, email, name)
email unique
Playlists(user, playlistName)
user -> Users.username
InList(user, playlist, number, track)
(user, playlist) -> Playlists.(user, playlistName)
track -> Tracks.trackId
PlayLog(user, time, track)
user -> Users.username
track -> Tracks.trackId
(user, time) unique
Use the relations for the music site from [above] when answering the following problems.
============================================
6A (3p)
Consider the situation where an administrator adds a new track to the database. Data then needs to be added to the tables Tracks, Participates and TracksOnAlbum. We assume for the sake of simplicity that relevant data already exists in the Artists and Albums tables, and also that the track being added is recorded by one single artist, and appears on one single album.
Consider the following program (partly in pseudo-code), for handling this situation. In the code I prefix program variables with : just to distingush them from attributes (i.e. you don’t need to worry about any connection to PSM or the like).
1 ... admin submits :title, :length, :artist, :album and :trackNr ...
2 SELECT MAX(trackId)+1 INTO newTrackId
FROM Tracks
WHERE post = :post;
3 INSERT INTO Tracks VALUES (:newTrackId, :title, :length);
4 INSERT INTO Participates VALUES (:newTrackId, :artist);
5 INSERT INTO TracksOnAlbum VALUES (:album, :trackNr, :newTrackId);
(i) (1p)
For the program as specified above, what atomicity problems could arise if it was not run as a transaction?
(ii) (2p)
For the program as specified above, what isolation problems could arise if it was not run as a serializable transaction?
============================================
6B (6p)
Consider the situation where a user asks the system to play the songs on one of her playlists, in order. Each time a new song begins playing, the system should log this fact. Consider the following program (partly in pseudo-code), for handling this situation. In the code I prefix program variables with : just to distingush them from attributes (i.e. you don’t need to worry about any connection to PSM or the like).
1 ... user (:user) asks to play list (:playlist) ...
2 SELECT MIN(number), MAX(number)
INTO (currentTrackNr, lastTrackNr)
FROM InList
WHERE user = :user AND playlist = :playlist;
3 while (:currentTrackNr <= :lastTrackNr) {
4 SELECT track INTO currentTrack
FROM InList
WHERE user = :user AND playlist = :playlist
AND number = :currentTrackNr;
5 INSERT INTO PlayLog VALUES (:user, NOW(), :track);
6 ... fetch and stream the requested track ...
7 SET currentTrackNr = currentTrackNr+1;
8 }
Compare what would happen if the program above was run as a transaction with isolation level SERIALIZABLE, to if it was run with isolation level READ COMMITTED. Point out benefits and drawbacks of the two choices for this particular problem, and suggest a suitable transaction strategy to use.
============================================