diff options
Diffstat (limited to 'src/main/java/at/ac/tuwien')
-rw-r--r-- | src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDAO.java | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDAO.java b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDAO.java index ef963d4..96703b1 100644 --- a/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDAO.java +++ b/src/main/java/at/ac/tuwien/sepm/assignment/groupphase/einsatzverwaltung/dao/VehicleDatabaseDAO.java @@ -11,6 +11,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Savepoint; import java.sql.Statement; import java.util.HashSet; import java.util.Set; @@ -37,9 +38,12 @@ public class VehicleDatabaseDAO implements VehicleDAO { String name = ""; int id = -1; int version = -1; + Connection connection = null; + Savepoint savepoint = null; try { - Connection connection = jdbcConnectionManager.getConnection(); + connection = jdbcConnectionManager.getConnection(); connection.setAutoCommit(false); + savepoint = connection.setSavepoint(); try (PreparedStatement p1 = connection.prepareStatement(query1, PreparedStatement.RETURN_GENERATED_KEYS)) { @@ -82,11 +86,26 @@ public class VehicleDatabaseDAO implements VehicleDAO { connection.commit(); connection.setAutoCommit(true); } catch (SQLException e) { + rollbackAndEnableAutoCommit(connection, savepoint); throw new PersistenceException(e); } return id; } + private void rollbackAndEnableAutoCommit(Connection connection, Savepoint savepoint) + throws PersistenceException { + try { + if (connection != null) { + if (savepoint != null) { + connection.rollback(savepoint); + } + connection.setAutoCommit(true); + } + } catch (SQLException e1) { + throw new PersistenceException(e1); + } + } + @Override public void update(Vehicle v) throws ElementNotFoundException, PersistenceException { String sql = "SELECT version FROM Vehicle WHERE id = ?"; @@ -97,10 +116,12 @@ public class VehicleDatabaseDAO implements VehicleDAO { long versionId; + Connection con = null; + Savepoint savepoint = null; try { - Connection con = jdbcConnectionManager.getConnection(); + con = jdbcConnectionManager.getConnection(); con.setAutoCommit(false); - + savepoint = con.setSavepoint(); try (PreparedStatement pstmt = con.prepareStatement(sql)) { pstmt.setLong(1, v.id()); @@ -137,6 +158,7 @@ public class VehicleDatabaseDAO implements VehicleDAO { con.commit(); con.setAutoCommit(true); } catch (SQLException e) { + rollbackAndEnableAutoCommit(con, savepoint); throw new PersistenceException(e); } } |