Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SQLConnection extends Thread {
- private String password, url,user;
- private Queue<Consumer<Connection>> queue = new ConcurrentLinkedQueue<>();
- private boolean running = true;
- public SQLConnection(String url, String user, String password) {
- this.url = url;
- this.user = user;
- this.password = password;
- }
- public void commencer() {
- start();
- }
- @Override
- public void run() {
- while (running) {
- if (queue.isEmpty())
- continue;
- try {
- Connection connection = DriverManager.getConnection(url, user, password);
- queue.poll().accept(connection);
- if (!connection.isClosed())
- connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public void query(Consumer<Connection> consumer) {
- queue.offer(consumer);
- }
- public void cancel() {
- this.running = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement