Advertisement
Guest User

Untitled

a guest
Feb 24th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. package connection;
  2.  
  3. import exceptions.DAOException;
  4. import exceptions.JDBCConnectionException;
  5. import logger.Logger;
  6.  
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.SQLException;
  10. import java.util.ArrayList;
  11. import java.util.Collections;
  12. import java.util.List;
  13. import java.util.ResourceBundle;
  14.  
  15. public class ConnectionPool {
  16.     private List<Connection> connections;
  17.     private List<Connection> usedConnections;
  18.  
  19.     /**
  20.      * init connection pool
  21.      * @throws JDBCConnectionException
  22.      */
  23.     public ConnectionPool() throws JDBCConnectionException {
  24.         connections = Collections.synchronizedList(new ArrayList<Connection>());
  25.         usedConnections = Collections.synchronizedList(new ArrayList<Connection>());
  26.     }
  27.  
  28.     /**
  29.      * get connection
  30.      * @throws JDBCConnectionException
  31.      */
  32.     public Connection getConnection() throws JDBCConnectionException {
  33.         Connection conn = null;
  34.         if(connections.size() < 1) {
  35.             ResourceBundle resource = ResourceBundle.getBundle("properties/database");
  36.             String url = resource.getString("url");
  37.             String driver = resource.getString("driver");
  38.             String user = resource.getString("user");
  39.             String pass = "";
  40.             try {
  41.                 Class.forName(driver).newInstance();
  42.                 conn = DriverManager.getConnection(url, user, pass);
  43.             } catch (ClassNotFoundException e) {
  44.                 throw new JDBCConnectionException ("Driver is not loaded!");
  45.             } catch (InstantiationException | IllegalAccessException | SQLException e) {
  46.                 Logger.logError(Logger.connectorLogger, e);
  47.             }
  48.             usedConnections.add(conn);
  49.         } else {
  50.             conn = connections.get(connections.size() - 1);
  51.             connections.remove(connections.size() - 1);
  52.             usedConnections.add(conn);
  53.         }
  54.         return conn;
  55.     }
  56.  
  57.     /**
  58.      * release connection
  59.      * @throws JDBCConnectionException
  60.      */
  61.     public void releaseConnection() throws JDBCConnectionException {
  62.         Connection conn = usedConnections.get(usedConnections.size() - 1);
  63.         usedConnections.remove(usedConnections.size() - 1);
  64.         connections.add(conn);
  65.  
  66.     }
  67.  
  68.     /**
  69.      * close func
  70.      * @throws JDBCConnectionException
  71.      */
  72.     public void close(Connection conn) throws JDBCConnectionException {
  73.         if (conn != null) {
  74.             try {
  75.                 conn.close();
  76.             } catch (SQLException e) {
  77.                 throw new JDBCConnectionException("Can't close connection");
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement