Need Code to create Connection Pool in java

后端 未结 10 1634
轻奢々
轻奢々 2020-11-30 20:50

Need code to create the connection pool in java? How does we make sure that connection pool doesn\'t return the same object which is already in use? How happens if client cl

相关标签:
10条回答
  • 2020-11-30 21:32

    Java Connection Pool ?
    There are three way to create JDBC Connection Pooling is very easy...

    1. Apache Commons DBCP

      public class DBCPDataSource {
      
      private static BasicDataSource ds = new BasicDataSource();
      
      static {
          ds.setUrl("jdbc:h2:mem:test");
          ds.setUsername("user");
          ds.setPassword("password");
          ds.setMinIdle(5);
          ds.setMaxIdle(10);
          ds.setMaxOpenPreparedStatements(100);
      }
      
      public static Connection getConnection() throws SQLException {
          return ds.getConnection();
      }
      
      private DBCPDataSource(){ }
      }
      

      Now you can get connection

      Connection con = DBCPDataSource.getConnection();
      
    2. HikariCP

      public class HikariCPDataSource {
      
      private static HikariConfig config = new HikariConfig();
      private static HikariDataSource ds;
      
      static {
          config.setJdbcUrl("jdbc:h2:mem:test");
          config.setUsername("user");
          config.setPassword("password");
          config.addDataSourceProperty("cachePrepStmts", "true");
          config.addDataSourceProperty("prepStmtCacheSize", "250");
          config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
          ds = new HikariDataSource(config);
      }
      
      public static Connection getConnection() throws SQLException {
          return ds.getConnection();
      }
      
      private HikariCPDataSource(){}
      }
      

    Now you can get connection

    Connection con = HikariCPDataSource.getConnection();
    
    1. C3PO

       public class C3poDataSource {
      
      private static ComboPooledDataSource cpds = new ComboPooledDataSource();
      
      static {
          try {
              cpds.setDriverClass("org.h2.Driver");
              cpds.setJdbcUrl("jdbc:h2:mem:test");
              cpds.setUser("user");
              cpds.setPassword("password");
          } catch (PropertyVetoException e) {
              // handle the exception
          }
      }
      
      public static Connection getConnection() throws SQLException {
          return cpds.getConnection();
      }
      
      private C3poDataSource(){}
      }
      

    Now you can get connection

    Connection con = C3poDataSource.getConnection();
    
    0 讨论(0)
  • 2020-11-30 21:33

    I hope this source code helps http://jagadeeshmanne.blogspot.com/2014/03/connection-pool-in-java-jdbc.html

    Configuration.java

    package com.jmanne.utils;
     
    public class Configuration {
      
     public String DB_USER_NAME ;
      
     public String DB_PASSWORD ;
      
     public String DB_URL;
      
     public String DB_DRIVER;
      
     public Integer DB_MAX_CONNECTIONS;
      
     public Configuration(){
      init();
     }
      
     private static Configuration configuration = new Configuration();
      
     public static Configuration getInstance(){ 
      return configuration;
     }
      
     private void init(){
      DB_USER_NAME = "root"
      DB_PASSWORD = "root"
      DB_URL = "jdbc:mysql://localhost:3306/jmanne"
      DB_DRIVER = "com.mysql.jdbc.Driver"
      DB_MAX_CONNECTIONS = 5
     }     
    }
    

    JdbcConnectionPool.java

    package com.jmanne.db;
     
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
     
    import com.jmanne.utils.Configuration;
    import com.mysql.jdbc.Connection;
     
    public class JdbcConnectionPool {
      
     List<connection> availableConnections = new ArrayList<connection>();
     
     public JdbcConnectionPool()
     {
      initializeConnectionPool();
     }
     
     private void initializeConnectionPool()
     {
      while(!checkIfConnectionPoolIsFull())
      {
       availableConnections.add(createNewConnectionForPool());
      }
     }
     
     private synchronized boolean checkIfConnectionPoolIsFull()
     {
      final int MAX_POOL_SIZE = Configuration.getInstance().DB_MAX_CONNECTIONS;
     
      if(availableConnections.size() < MAX_POOL_SIZE)
      {
       return false;
      }
     
      return true;
     }
     
     //Creating a connection
     private Connection createNewConnectionForPool()
     {
      Configuration config = Configuration.getInstance();
      try {
       Class.forName(config.DB_DRIVER);
       Connection connection = (Connection) DriverManager.getConnection(
         config.DB_URL, config.DB_USER_NAME, config.DB_PASSWORD);
       return connection;
      } catch (ClassNotFoundException e) {
       e.printStackTrace();
      } catch (SQLException e) {
       e.printStackTrace();
      }
      return null;
       
     }
     
     public synchronized Connection getConnectionFromPool()
     {
      Connection connection = null;
      if(availableConnections.size() > 0)
      {
       connection = (Connection) availableConnections.get(0);
       availableConnections.remove(0);
      }
      return connection;
     }
     
     public synchronized void returnConnectionToPool(Connection connection)
     {
      availableConnections.add(connection);
     }
    }
    

    DataSource.java

    package com.jmanne.db;
     
    import java.sql.SQLException;
     
    import com.mysql.jdbc.Connection;
     
    public class DataSource {
      
     static JdbcConnectionPool pool = new JdbcConnectionPool();
      
     public static Connection getConnection() throws ClassNotFoundException, SQLException{
      Connection connection = pool.getConnectionFromPool();
      return connection;
     }
      
     public static void returnConnection(Connection connection) {
      pool.returnConnectionToPool(connection);
     }
    }
    
    0 讨论(0)
  • 2020-11-30 21:35

    One argument for rolling your own connpool is the configuration and additional jars that are avoided. I agree you need to enable the 3rd party interfaces so you can swap in a mature connpool, but having your own tiny solution can have its place. Self cleaning Vector with synchronized block and a conn wrapper with close() marking the conn as available works very well for servlet apps.

    0 讨论(0)
  • 2020-11-30 21:36

    If your application runs on a server, then configure as Data Source, where server will take care of Pooling or else if a simple Java Client then use Apache DBCP(if to the database) or else use Apache Commons Pooling API See here: Apache Commons

    0 讨论(0)
提交回复
热议问题