Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. public class SQLConnection extends Thread {
  2. private String password, url,user;
  3. private Queue<Consumer<Connection>> queue = new ConcurrentLinkedQueue<>();
  4. private boolean running = true;
  5.  
  6. public SQLConnection(String url, String user, String password) {
  7. this.url = url;
  8. this.user = user;
  9. this.password = password;
  10. }
  11.  
  12. public void commencer() {
  13. start();
  14. }
  15.  
  16. @Override
  17. public void run() {
  18. while (running) {
  19. if (queue.isEmpty())
  20. continue;
  21.  
  22. try {
  23. Connection connection = DriverManager.getConnection(url, user, password);
  24. queue.poll().accept(connection);
  25.  
  26. if (!connection.isClosed())
  27. connection.close();
  28. } catch (SQLException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }
  33.  
  34.  
  35. public void query(Consumer<Connection> consumer) {
  36. queue.offer(consumer);
  37. }
  38.  
  39. public void cancel() {
  40. this.running = false;
  41. }
  42.  
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement