Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. package daoservice;
  2.  
  3. import java.util.function.Consumer;
  4.  
  5. @SuppressWarnings("unused")
  6. public final class DaoService {
  7. private ConnectionPool pool = new ConnectionPool(1);
  8.  
  9. public void updateTimestamp(long id) {
  10. perform(conn -> {
  11. if (!conn.recordExists(id))
  12. create(id);
  13.  
  14. conn.updateTimestamp(id, System.currentTimeMillis());
  15. });
  16. }
  17.  
  18. private void create(long id) {
  19. perform(conn -> {
  20. try {
  21. conn.createRecord(id);
  22. } catch (DuplicateKeyException ignored) {
  23.  
  24. }
  25. });
  26. }
  27.  
  28. private void perform(Consumer<Connection> c) {
  29. Connection conn = pool.borrowConnection();
  30. try {
  31. c.accept(conn);
  32. }
  33. finally {
  34. pool.returnConnection(conn);
  35. }
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement