From a2cb1b69f512f18400cb6a048c08074907053648 Mon Sep 17 00:00:00 2001
From: Felix Kehrer <felix.kehrer@gmail.com>
Date: Mon, 7 May 2018 10:27:34 +0200
Subject: Changed behaviour to only prepare statement at construction time

---
 .../einsatzverwaltung/dao/H2RegistrationDAO.java   | 46 +++++++++++-----------
 1 file changed, 23 insertions(+), 23 deletions(-)

(limited to 'src')

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;
-- 
cgit v1.2.3-70-g09d2