Transaction
This commit is contained in:
parent
570b0c319b
commit
92441ad91f
1 changed files with 18 additions and 2 deletions
|
@ -204,8 +204,15 @@ public Show getShowData(String movie, String date) {
|
||||||
|
|
||||||
System.out.println("Adding reservation for " + username + " for " + movie + " on " + date);
|
System.out.println("Adding reservation for " + username + " for " + movie + " on " + date);
|
||||||
|
|
||||||
try {
|
try(Statement stmt = conn.createStatement()) {
|
||||||
Statement stmt = conn.createStatement();
|
conn.setAutoCommit(false);
|
||||||
|
Show s = getShowData(movie, date);
|
||||||
|
|
||||||
|
if (s.getSeats() <= 0) {
|
||||||
|
System.err.println("addReservation: no free seats for " + movie + " on " + date);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
ResultSet rs = stmt.executeQuery("SELECT * FROM Users WHERE username = '" + username + "'");
|
ResultSet rs = stmt.executeQuery("SELECT * FROM Users WHERE username = '" + username + "'");
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
int user_id = rs.getInt("id");
|
int user_id = rs.getInt("id");
|
||||||
|
@ -216,14 +223,23 @@ public Show getShowData(String movie, String date) {
|
||||||
String theater = rs.getString("theater_id");
|
String theater = rs.getString("theater_id");
|
||||||
stmt.executeUpdate("INSERT INTO Reservations (user_id, showing_id) VALUES (" + user_id + ", " + showing_id + ")");
|
stmt.executeUpdate("INSERT INTO Reservations (user_id, showing_id) VALUES (" + user_id + ", " + showing_id + ")");
|
||||||
System.out.println("Reservation added");
|
System.out.println("Reservation added");
|
||||||
|
conn.commit();
|
||||||
|
conn.setAutoCommit(true);
|
||||||
int bookingId = stmt.getGeneratedKeys().getInt(1);
|
int bookingId = stmt.getGeneratedKeys().getInt(1);
|
||||||
return new Reservation(bookingId, movie, date, theater);
|
return new Reservation(bookingId, movie, date, theater);
|
||||||
}
|
}
|
||||||
|
rs.close();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
System.err.println(String.format("addReservation: user %s not found", username));
|
System.err.println(String.format("addReservation: user %s not found", username));
|
||||||
}
|
}
|
||||||
|
conn.commit();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
|
try {
|
||||||
|
conn.rollback();
|
||||||
|
} catch (SQLException e2) {
|
||||||
|
e2.printStackTrace();
|
||||||
|
}
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
Loading…
Reference in a new issue