Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.weebly.arjunvikramprogramming.jsp.arjmart;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- import javax.servlet.annotation.WebListener;
- import java.util.Iterator;
- import java.util.Map.Entry;
- import java.util.concurrent.ConcurrentHashMap;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- @WebListener
- public class ConnectionPool implements ServletContextListener{
- public static ServletContext servletcontext;
- public static ConcurrentHashMap<Connection,Boolean> connections;
- public static boolean init=false;
- private static final String dbURL="jdbc:mysql://localhost:3306/GroceryStore",
- user="user",
- password="pw",
- driverClassName="com.mysql.jdbc.Driver";
- private static final int initialConnections=10,
- increment=3,
- connectionValidityTimeout=0,
- retryCount=10;
- public static void init(){
- try{
- if(!init){
- init=true;
- Class.forName(driverClassName);
- loop:for(int i=0;i<initialConnections;i++){
- Connection connection = DriverManager.getConnection(dbURL, user, password);
- int j=retryCount;
- while(!valid(connection)){
- if(--j<=0){
- log("Connection retry count reached during initialization");
- continue loop;
- }
- log("Invalid connection during initialization - trying again");
- connection = DriverManager.getConnection(dbURL, user, password);
- }
- connections.put(connection, false);
- }
- }
- }catch(ClassNotFoundException e){
- log("JDBC Driver could not be loaded:");
- e.printStackTrace(System.err);
- } catch (SQLException e) {
- log("Connection creation error during initialization:");
- e.printStackTrace(System.err);
- }
- }
- public static Connection getConnection(){
- synchronized(connections){
- Iterator<Entry<Connection,Boolean>> connectionIterator = connections.entrySet().iterator();
- while (connectionIterator.hasNext()) {
- Entry<Connection, Boolean> entry = connectionIterator.next();
- if(!entry.getValue()){
- if(valid(entry.getKey())){
- entry.setValue(true);
- return entry.getKey();
- }else{
- log("Invalid connection - removing");
- connectionIterator.remove();
- }
- }
- }
- return repopulate();
- }
- }
- private static Connection repopulate() {
- try{
- loop:for(int i=0;i<increment-1;i++){
- Connection connection = DriverManager.getConnection(dbURL, user, password);
- int j=retryCount;
- while(!valid(connection)){
- if(--j<=0){
- log("Connection retry count reached during repopulation");
- continue loop;
- }
- log("Invalid connection during repopulation - trying again");
- connection = DriverManager.getConnection(dbURL, user, password);
- }
- connections.put(connection, false);
- }
- Connection connection = DriverManager.getConnection(dbURL, user, password);
- int j=retryCount;
- while(!valid(connection)){
- if(--j<=0){
- log("Connection retry count reached during repopulation");
- }
- log("Invalid connection during repopulation - trying again");
- connection = DriverManager.getConnection(dbURL, user, password);
- }
- connections.put(connection, true);
- return connection;
- }catch (SQLException e) {
- log("Connection creation error while repopultating:");
- e.printStackTrace(System.err);
- return null;
- }
- }
- private static boolean valid(Connection key) {
- try {
- return key.isValid(connectionValidityTimeout);
- } catch (SQLException e) {
- return false;
- }
- }
- public static boolean returnConnection(Connection connection){
- return (connections.put(connection, false))!=null;
- }
- private static void log(String s){
- servletcontext.log(s);
- }
- @Override
- public void contextDestroyed(ServletContextEvent sce) {
- sce.getServletContext().log("Removing all arjmart db connections - ServerletContextListener");
- }
- @Override
- public void contextInitialized(ServletContextEvent sce) {
- sce.getServletContext().log("Creating arjmart db connections - ServerletContextListener");
- servletcontext=sce.getServletContext();
- init();
- sce.getServletContext().log("Connections created successfully");
- }
- }
Add Comment
Please, Sign In to add comment