arjvik

ConnectionPool.java

Oct 7th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.24 KB | None | 0 0
  1. package com.weebly.arjunvikramprogramming.jsp.arjmart;
  2.  
  3. import javax.servlet.ServletContext;
  4. import javax.servlet.ServletContextEvent;
  5. import javax.servlet.ServletContextListener;
  6. import javax.servlet.annotation.WebListener;
  7.  
  8. import java.util.Iterator;
  9. import java.util.Map.Entry;
  10. import java.util.concurrent.ConcurrentHashMap;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.SQLException;
  14.  
  15. @WebListener
  16. public class ConnectionPool implements ServletContextListener{
  17.     public static ServletContext servletcontext;
  18.     public static ConcurrentHashMap<Connection,Boolean> connections;
  19.     public static boolean init=false;
  20.     private static final String dbURL="jdbc:mysql://localhost:3306/GroceryStore",
  21.                                 user="user",
  22.                                 password="pw",
  23.                                 driverClassName="com.mysql.jdbc.Driver";
  24.     private static final int initialConnections=10,
  25.                             increment=3,
  26.                             connectionValidityTimeout=0,
  27.                             retryCount=10;
  28.    
  29.     public static void init(){
  30.         try{
  31.             if(!init){
  32.                 init=true;
  33.                 Class.forName(driverClassName);
  34.                 loop:for(int i=0;i<initialConnections;i++){
  35.                     Connection connection = DriverManager.getConnection(dbURL, user, password);
  36.                     int j=retryCount;
  37.                     while(!valid(connection)){
  38.                         if(--j<=0){
  39.                             log("Connection retry count reached during initialization");
  40.                             continue loop;
  41.                         }
  42.                         log("Invalid connection during initialization - trying again");
  43.                         connection = DriverManager.getConnection(dbURL, user, password);
  44.                     }
  45.                     connections.put(connection, false);
  46.                 }
  47.             }
  48.         }catch(ClassNotFoundException e){
  49.             log("JDBC Driver could not be loaded:");
  50.             e.printStackTrace(System.err);
  51.         } catch (SQLException e) {
  52.             log("Connection creation error during initialization:");
  53.             e.printStackTrace(System.err);
  54.         }
  55.     }
  56.    
  57.     public static Connection getConnection(){
  58.         synchronized(connections){
  59.             Iterator<Entry<Connection,Boolean>> connectionIterator = connections.entrySet().iterator();
  60.             while (connectionIterator.hasNext()) {
  61.                 Entry<Connection, Boolean> entry = connectionIterator.next();
  62.                 if(!entry.getValue()){
  63.                     if(valid(entry.getKey())){
  64.                         entry.setValue(true);
  65.                         return entry.getKey();
  66.                     }else{
  67.                         log("Invalid connection - removing");
  68.                         connectionIterator.remove();
  69.                     }
  70.                 }
  71.             }
  72.             return repopulate();
  73.         }
  74.        
  75.     }
  76.  
  77.     private static Connection repopulate() {
  78.         try{
  79.             loop:for(int i=0;i<increment-1;i++){
  80.                 Connection connection = DriverManager.getConnection(dbURL, user, password);
  81.                 int j=retryCount;
  82.                 while(!valid(connection)){
  83.                     if(--j<=0){
  84.                         log("Connection retry count reached during repopulation");
  85.                         continue loop;
  86.                     }
  87.                     log("Invalid connection during repopulation - trying again");
  88.                     connection = DriverManager.getConnection(dbURL, user, password);
  89.                 }
  90.                 connections.put(connection, false);
  91.             }
  92.             Connection connection = DriverManager.getConnection(dbURL, user, password);
  93.             int j=retryCount;
  94.             while(!valid(connection)){
  95.                 if(--j<=0){
  96.                     log("Connection retry count reached during repopulation");
  97.                 }
  98.                 log("Invalid connection during repopulation - trying again");
  99.                 connection = DriverManager.getConnection(dbURL, user, password);
  100.             }
  101.             connections.put(connection, true);
  102.             return connection;
  103.         }catch (SQLException e) {
  104.             log("Connection creation error while repopultating:");
  105.             e.printStackTrace(System.err);
  106.             return null;
  107.         }
  108.     }
  109.  
  110.     private static boolean valid(Connection key) {
  111.         try {
  112.             return key.isValid(connectionValidityTimeout);
  113.         } catch (SQLException e) {
  114.             return false;
  115.         }
  116.     }
  117.    
  118.     public static boolean returnConnection(Connection connection){
  119.         return (connections.put(connection, false))!=null;
  120.     }
  121.    
  122.     private static void log(String s){
  123.         servletcontext.log(s);
  124.     }
  125.  
  126.     @Override
  127.     public void contextDestroyed(ServletContextEvent sce) {
  128.         sce.getServletContext().log("Removing all arjmart db connections - ServerletContextListener");
  129.     }
  130.  
  131.     @Override
  132.     public void contextInitialized(ServletContextEvent sce) {
  133.         sce.getServletContext().log("Creating arjmart db connections - ServerletContextListener");
  134.         servletcontext=sce.getServletContext();
  135.         init();
  136.         sce.getServletContext().log("Connections created successfully");
  137.     }
  138. }
Add Comment
Please, Sign In to add comment