Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. import java.util.*;
  2. import java.sql.*;
  3.  
  4. class DBConnectionManager
  5. {
  6.  
  7.     String databaseUrl = "jdbc:mysql://localhost:3306/myDatabase";
  8.     String userName = "userName";
  9.     String password = "userPass";
  10.  
  11.     Vector connectionPool = new Vector();
  12.  
  13.     public DBConnectionManager()
  14.     {
  15.         initialize();
  16.     }
  17.  
  18.     public DBConnectionManager(
  19.         //String databaseName,
  20.         String databaseUrl,
  21.         String userName,
  22.         String password
  23.         )
  24.     {
  25.         this.databaseUrl = databaseUrl;
  26.         this.userName = userName;
  27.         this.password = password;
  28.         initialize();
  29.     }
  30.  
  31.     private void initialize()
  32.     {
  33.         //Here we can initialize all the information that we need
  34.         initializeConnectionPool();
  35.     }
  36.  
  37.     private void initializeConnectionPool()
  38.     {
  39.         while(!checkIfConnectionPoolIsFull())
  40.         {
  41.             System.out.println("Connection Pool is NOT full. Proceeding with adding new connections");
  42.             //Adding new connection instance until the pool is full
  43.             connectionPool.addElement(createNewConnectionForPool());
  44.         }
  45.         System.out.println("Connection Pool is full.");
  46.     }
  47.  
  48.     private synchronized boolean checkIfConnectionPoolIsFull()
  49.     {
  50.         final int MAX_POOL_SIZE = 5;
  51.  
  52.         //Check if the pool size
  53.         if(connectionPool.size() < 5)
  54.         {
  55.             return false;
  56.         }
  57.  
  58.         return true;
  59.     }
  60.  
  61.     //Creating a connection
  62.     private Connection createNewConnectionForPool()
  63.     {
  64.         Connection connection = null;
  65.  
  66.         try
  67.         {
  68.             Class.forName("com.mysql.jdbc.Driver");
  69.             connection = DriverManager.getConnection(databaseUrl, userName, password);
  70.             System.out.println("Connection: "+connection);
  71.         }
  72.         catch(SQLException sqle)
  73.         {
  74.             System.err.println("SQLException: "+sqle);
  75.             return null;
  76.         }
  77.         catch(ClassNotFoundException cnfe)
  78.         {
  79.             System.err.println("ClassNotFoundException: "+cnfe);
  80.             return null;
  81.         }
  82.  
  83.         return connection;
  84.     }
  85.  
  86.     public synchronized Connection getConnectionFromPool()
  87.     {
  88.         Connection connection = null;
  89.  
  90.         //Check if there is a connection available. There are times when all the connections in the pool may be used up
  91.         if(connectionPool.size() > 0)
  92.         {
  93.             connection = (Connection) connectionPool.firstElement();
  94.             connectionPool.removeElementAt(0);
  95.         }
  96.         //Giving away the connection from the connection pool
  97.         return connection;
  98.     }
  99.  
  100.     public synchronized void returnConnectionToPool(Connection connection)
  101.     {
  102.         //Adding the connection from the client back to the connection pool
  103.         connectionPool.addElement(connection);
  104.     }
  105.  
  106.     public static void main(String args[])
  107.     {
  108.         DBConnectionManager dbConnectionManager = new DBConnectionManager();
  109.     }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement