Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. public final class DaoService {
  2.     @Autowired
  3.     private ConnectionPool pool;
  4.  
  5.     public void updateTimestamp(long id) {
  6.         perform(conn -> {
  7.             if (!conn.recordExists(id))
  8.                 create(id);
  9.  
  10.             conn.updateTimestamp(id, System.currentTimeMillis());
  11.         });
  12.     }
  13.  
  14.     private void create(long id) {
  15.         try {
  16.             perform(conn -> conn.createRecord(id));
  17.         }
  18.         catch (DuplicateKeyException ex) {
  19.             // ignored
  20.         }
  21.     }
  22.  
  23.     private void perform(Consumer<Connection> c) {
  24.         Connection conn = pool.borrowConnection();
  25.         try {
  26.             c.accept(conn);
  27.         }
  28.         finally {
  29.             pool.returnConnection(conn);
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement