SHARE
TWEET

Untitled

a guest Nov 16th, 2019 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package de.cade.gameapi.sql;
  2.  
  3. import lombok.Getter;
  4.  
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.SQLException;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import java.util.concurrent.ExecutorService;
  11. import java.util.concurrent.Executors;
  12.  
  13. public class DatabaseHandler {
  14.  
  15.     @Getter
  16.     private ExecutorService executor = Executors.newFixedThreadPool(2);
  17.     private HashMap<Connection, Boolean> connections = new HashMap<>();
  18.     private String host, user, database, password, port;
  19.  
  20.     public DatabaseHandler(String host, String user, String database, String password, String port) {
  21.         this.host = host;
  22.         this.user = user;
  23.         this.database = database;
  24.         this.password = password;
  25.         this.port = port;
  26.     }
  27.  
  28.     public void releaseConnection(Connection connection) {
  29.  
  30.         if (connections.get(connection) == null)
  31.             connections.put(connection, false);
  32.  
  33.         if (connections.get(connection) == true)
  34.             connections.remove(connection);
  35.  
  36.         connections.put(connection, false);
  37.  
  38.     }
  39.  
  40.     public void closeConnections() throws SQLException {
  41.  
  42.         for (Map.Entry<Connection, Boolean> entry : connections.entrySet())
  43.             if (entry.getValue() == Boolean.FALSE)
  44.                 if (entry.getKey() != null)
  45.                     entry.getKey().close();
  46.  
  47.         if (!executor.isShutdown())
  48.             executor.shutdown();
  49.  
  50.     }
  51.  
  52.     private Connection currentConnection() throws SQLException {
  53.         String url = "jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true";
  54.         Connection connection = DriverManager.getConnection(url, user, password);
  55.         connections.put(connection, true);
  56.         return connection;
  57.     }
  58.  
  59.     public Connection getConnections() {
  60.  
  61.         for (Map.Entry<Connection, Boolean> entry : connections.entrySet()) {
  62.             if (entry.getValue() == Boolean.FALSE) {
  63.                 connections.remove(entry.getKey());
  64.                 connections.put(entry.getKey(), true);
  65.                 return entry.getKey();
  66.             }
  67.         }
  68.  
  69.         try {
  70.             return currentConnection();
  71.         } catch (SQLException e) {
  72.             e.printStackTrace();
  73.         }
  74.  
  75.         return null;
  76.  
  77.     }
  78.  
  79. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top