Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package daoservice;
- import java.util.function.Consumer;
- @SuppressWarnings("unused")
- public final class DaoService {
- private ConnectionPool pool = new ConnectionPool(1);
- public void updateTimestamp(long id) {
- perform(conn -> {
- if (!conn.recordExists(id))
- create(id);
- conn.updateTimestamp(id, System.currentTimeMillis());
- });
- }
- private void create(long id) {
- perform(conn -> {
- try {
- conn.createRecord(id);
- } catch (DuplicateKeyException ignored) {
- }
- });
- }
- private void perform(Consumer<Connection> c) {
- Connection conn = pool.borrowConnection();
- try {
- c.accept(conn);
- }
- finally {
- pool.returnConnection(conn);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement