Skip to content

Commit 0901c3a

Browse files
author
Tanechka
committed
Lesson14 Transaction_Batch
1 parent 57c5687 commit 0901c3a

3 files changed

Lines changed: 45 additions & 14 deletions

File tree

src/ru/javawebinar/basejava/sql/SqlHelper.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ru.javawebinar.basejava.sql;
22

3+
import ru.javawebinar.basejava.exception.StorageException;
4+
35
import java.sql.Connection;
46
import java.sql.PreparedStatement;
57
import java.sql.SQLException;
@@ -23,4 +25,20 @@ public <T> T execute(String sql, SqlExecutor<T> executor) {
2325
throw ExceptionUtil.convertException(e);
2426
}
2527
}
28+
29+
public <T> T transactionalExecute(SqlTransaction<T> executor) {
30+
try (Connection conn = connectionFactory.getConnection()) {
31+
try {
32+
conn.setAutoCommit(false);
33+
T res = executor.execute(conn);
34+
conn.commit();
35+
return res;
36+
} catch (SQLException e) {
37+
conn.rollback();
38+
throw ExceptionUtil.convertException(e);
39+
}
40+
} catch (SQLException e) {
41+
throw new StorageException(e);
42+
}
43+
}
2644
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ru.javawebinar.basejava.sql;
2+
3+
import java.sql.Connection;
4+
import java.sql.SQLException;
5+
6+
public interface SqlTransaction<T> {
7+
T execute(Connection conn) throws SQLException;
8+
}

src/ru/javawebinar/basejava/storage/SqlStorage.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import ru.javawebinar.basejava.sql.SqlHelper;
77

88
import java.sql.DriverManager;
9+
import java.sql.PreparedStatement;
910
import java.sql.ResultSet;
1011
import java.util.ArrayList;
1112
import java.util.List;
@@ -61,20 +62,24 @@ public void update(Resume r) {
6162

6263
@Override
6364
public void save(Resume r) {
64-
sqlHelper.<Void>execute("INSERT INTO resume (uuid, full_name) VALUES (?,?)", ps -> {
65-
ps.setString(1, r.getUuid());
66-
ps.setString(2, r.getFullName());
67-
ps.execute();
68-
return null;
69-
});
70-
for (Map.Entry<ContactType, String> e : r.getContacts().entrySet()) {
71-
sqlHelper.<Void>execute("INSERT INTO contact (resume_uuid, type, value) VALUES (?,?,?)", ps -> {
72-
ps.setString(1, r.getUuid());
73-
ps.setString(2, e.getKey().name());
74-
ps.setString(3, e.getValue());
75-
return null;
76-
});
77-
}
65+
sqlHelper.transactionalExecute(conn -> {
66+
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO resume (uuid, full_name) VALUES (?,?)")) {
67+
ps.setString(1, r.getUuid());
68+
ps.setString(2, r.getFullName());
69+
ps.execute();
70+
}
71+
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO contact (resume_uuid, type, value) VALUES (?,?,?)")) {
72+
for (Map.Entry<ContactType, String> e : r.getContacts().entrySet()) {
73+
ps.setString(1, r.getUuid());
74+
ps.setString(2, e.getKey().name());
75+
ps.setString(3, e.getValue());
76+
ps.addBatch();
77+
}
78+
ps.executeBatch();
79+
}
80+
return null;
81+
}
82+
);
7883
}
7984

8085
@Override

0 commit comments

Comments
 (0)