aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFelix Kehrer <felix.kehrer@gmail.com>2018-05-07 10:27:34 +0200
committerFelix Kehrer <felix.kehrer@gmail.com>2018-05-07 14:42:56 +0200
commita2cb1b69f512f18400cb6a048c08074907053648 (patch)
tree5245be4da7f90816afae9bcc6a5f2b90c5814ab5
parent8c73a66236c9a3c416fdb7d337725ec9d4ab6583 (diff)
downloadsepm-groupproject-a2cb1b69f512f18400cb6a048c08074907053648.tar.gz
sepm-groupproject-a2cb1b69f512f18400cb6a048c08074907053648.tar.xz
sepm-groupproject-a2cb1b69f512f18400cb6a048c08074907053648.zip
Changed behaviour to only prepare statement at construction time
-rw-r--r--src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAO.java46
1 files changed, 23 insertions, 23 deletions
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAO.java
index c1ea533..fb9bad5 100644
--- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAO.java
+++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/H2RegistrationDAO.java
@@ -26,14 +26,20 @@ public class H2RegistrationDAO implements RegistrationDAO {
private static final String UPDATE_VEHICLE =
"UPDATE Vehicle SET status = 'frei_wache' WHERE id = ?;";
+ private PreparedStatement addRegistration;
+ private PreparedStatement updateVehicle;
+
private Connection connection;
@Autowired
public H2RegistrationDAO(JDBCConnectionManager connectionManager) throws PersistenceException {
try {
connection = connectionManager.getConnection();
+ addRegistration =
+ connection.prepareStatement(ADD_REGISTRATION, Statement.RETURN_GENERATED_KEYS);
+ updateVehicle = connection.prepareStatement(UPDATE_VEHICLE);
} catch (SQLException e) {
- LOG.error("Could not get connection!");
+ LOG.error("Could not get connection or preparation of statement failed");
throw new PersistenceException(e);
}
}
@@ -45,32 +51,26 @@ public class H2RegistrationDAO implements RegistrationDAO {
try {
connection.setAutoCommit(false);
for (Registration registration : registrations) {
- try (PreparedStatement addRegistration =
- connection.prepareStatement(
- ADD_REGISTRATION, Statement.RETURN_GENERATED_KEYS)) {
- addRegistration.setLong(1, vehicleId);
- addRegistration.setLong(2, registration.employee().id());
- addRegistration.setObject(3, registration.start());
- addRegistration.setObject(4, registration.end());
- addRegistration.setBoolean(
- 5, true); // ASSUMPTION: Registration gets created as active
- addRegistration.executeUpdate();
- try (ResultSet rs = addRegistration.getGeneratedKeys()) {
- if (rs.next()) {
- returnValues.add(rs.getLong(1));
- } else {
- LOG.error("No ResultSet was created while adding registration");
- throw new PersistenceException(
- "Anmeldung konnte nicht gespeichert werden.");
- }
+ addRegistration.setLong(1, vehicleId);
+ addRegistration.setLong(2, registration.employee().id());
+ addRegistration.setObject(3, registration.start());
+ addRegistration.setObject(4, registration.end());
+ addRegistration.setBoolean(
+ 5, true); // ASSUMPTION: Registration gets created as active
+ addRegistration.executeUpdate();
+ try (ResultSet rs = addRegistration.getGeneratedKeys()) {
+ if (rs.next()) {
+ returnValues.add(rs.getLong(1));
+ } else {
+ LOG.error("No ResultSet was created while adding registration");
+ throw new PersistenceException(
+ "Anmeldung konnte nicht gespeichert werden.");
}
}
}
- try (PreparedStatement updateVehicle = connection.prepareStatement(UPDATE_VEHICLE)) {
- updateVehicle.setLong(1, vehicleId);
- updateVehicle.executeUpdate();
- }
+ updateVehicle.setLong(1, vehicleId);
+ updateVehicle.executeUpdate();
connection.commit();
return returnValues;