How to establish a connection pool in JDBC?

前端 未结 13 2273
一个人的身影
一个人的身影 2020-11-22 02:43

Can anybody provide examples or links on how to establish a JDBC connection pool?

From searching google I see many different ways of doing this and it is rather conf

13条回答
  •  -上瘾入骨i
    2020-11-22 02:54

    Pool

    • Pooling Mechanism is the way of creating the Objects in advance. When a class is loaded.
    • It improves the application performance [By re using same object's to perform any action on Object-Data] & memory [allocating and de-allocating many objects creates a significant memory management overhead].
    • Object clean-up is not required as we are using same Object, reducing the Garbage collection load.

    « Pooling [ Object pool, String Constant Pool, Thread Pool, Connection pool]

    String Constant pool

    • String literal pool maintains only one copy of each distinct string value. which must be immutable.
    • When the intern method is invoked, it check object availability with same content in pool using equals method. « If String-copy is available in the Pool then returns the reference. « Otherwise, String object is added to the pool and returns the reference.

    Example: String to verify Unique Object from pool.

    public class StringPoolTest {
        public static void main(String[] args) { // Integer.valueOf(), String.equals()
            String eol = System.getProperty("line.separator"); //java7 System.lineSeparator();
    
            String s1 = "Yash".intern();
            System.out.format("Val:%s Hash:%s SYS:%s "+eol, s1, s1.hashCode(), System.identityHashCode(s1));
            String s2 = "Yas"+"h".intern();
            System.out.format("Val:%s Hash:%s SYS:%s "+eol, s2, s2.hashCode(), System.identityHashCode(s2));
            String s3 = "Yas".intern()+"h".intern();
            System.out.format("Val:%s Hash:%s SYS:%s "+eol, s3, s3.hashCode(), System.identityHashCode(s3));
            String s4 = "Yas"+"h";
            System.out.format("Val:%s Hash:%s SYS:%s "+eol, s4, s4.hashCode(), System.identityHashCode(s4));
        }
    }
    

    Connection pool using Type-4 Driver using 3rd party libraries[ DBCP2, c3p0, Tomcat JDBC]

    Type 4 - The Thin driver converts JDBC calls directly into the vendor-specific database protocol Ex[Oracle - Thick, MySQL - Quora]. wiki

    In Connection pool mechanism, when the class is loaded it get's the physical JDBC connection objects and provides a wrapped physical connection object to user. PoolableConnection is a wrapper around the actual connection.

    • getConnection() pick one of the free wrapped-connection form the connection objectpool and returns it.
    • close() instead of closing it returns the wrapped-connection back to pool.

    Example: Using ~ DBCP2 Connection Pool with Java 7[try-with-resources]

    public class ConnectionPool {
        static final BasicDataSource ds_dbcp2 = new BasicDataSource();
        static final ComboPooledDataSource ds_c3p0 = new ComboPooledDataSource();
        static final DataSource ds_JDBC = new DataSource();
    
        static Properties prop = new Properties();
        static {
            try {
                prop.load(ConnectionPool.class.getClassLoader().getResourceAsStream("connectionpool.properties"));
    
                ds_dbcp2.setDriverClassName( prop.getProperty("DriverClass") );
                ds_dbcp2.setUrl( prop.getProperty("URL") );
                ds_dbcp2.setUsername( prop.getProperty("UserName") );
                ds_dbcp2.setPassword( prop.getProperty("Password") );
                ds_dbcp2.setInitialSize( 5 );
    
                ds_c3p0.setDriverClass( prop.getProperty("DriverClass") );
                ds_c3p0.setJdbcUrl( prop.getProperty("URL") );
                ds_c3p0.setUser( prop.getProperty("UserName") );
                ds_c3p0.setPassword( prop.getProperty("Password") );
                ds_c3p0.setMinPoolSize(5);
                ds_c3p0.setAcquireIncrement(5);
                ds_c3p0.setMaxPoolSize(20);
    
                PoolProperties pool = new PoolProperties();
                pool.setUrl( prop.getProperty("URL") );
                pool.setDriverClassName( prop.getProperty("DriverClass") );
                pool.setUsername( prop.getProperty("UserName") );
                pool.setPassword( prop.getProperty("Password") );
                pool.setValidationQuery("SELECT 1");// SELECT 1(mysql) select 1 from dual(oracle)
    
                pool.setInitialSize(5);
                pool.setMaxActive(3);
                ds_JDBC.setPoolProperties( pool );
            } catch (IOException e) {   e.printStackTrace();
            } catch (PropertyVetoException e) { e.printStackTrace(); }
        }
    
        public static Connection getDBCP2Connection() throws SQLException {
            return ds_dbcp2.getConnection();
        }
    
        public static Connection getc3p0Connection() throws SQLException {
            return ds_c3p0.getConnection();
        }
    
        public static Connection getJDBCConnection() throws SQLException {
            return ds_JDBC.getConnection();
        }
    }
    public static boolean exists(String UserName, String Password ) throws SQLException {
        boolean exist = false;
        String SQL_EXIST = "SELECT * FROM users WHERE username=? AND password=?";
        try ( Connection connection = ConnectionPool.getDBCP2Connection();
              PreparedStatement pstmt = connection.prepareStatement(SQL_EXIST); ) {
            pstmt.setString(1, UserName );
            pstmt.setString(2, Password );
    
            try (ResultSet resultSet = pstmt.executeQuery()) {
                exist = resultSet.next(); // Note that you should not return a ResultSet here.
            }
        }
        System.out.println("User : "+exist);
        return exist;
    }
    

    jdbc::::: jdbc:oracle:thin:@localhost:1521:myDBName jdbc:mysql://localhost:3306/myDBName

    connectionpool.properties

    URL         : jdbc:mysql://localhost:3306/myDBName
    DriverClass : com.mysql.jdbc.Driver
    UserName    : root
    Password    :
    

    Web Application: To avoid connection problem when all the connection's are closed[MySQL "wait_timeout" default 8 hours] in-order to reopen the connection with underlying DB.

    You can do this to Test Every Connection by setting testOnBorrow = true and validationQuery= "SELECT 1" and donot use autoReconnect for MySQL server as it is deprecated. issue

    ===== ===== context.xml ===== =====
    
    
    
        
    
    
    ===== ===== web.xml ===== =====
    
        DB Connection
        jdbc/MyAppDB
        javax.sql.DataSource
        Container
    
    ===== ===== DBOperations ===== =====
    servlet «   init() {}
    Normal call used by sevlet  « static {}
    
    static DataSource ds;
    static {
        try {
            Context ctx=new InitialContext();
            Context envContext = (Context)ctx.lookup("java:comp/env");
            ds  =   (DataSource) envContext.lookup("jdbc/MyAppDB");
        } catch (NamingException e) {   e.printStackTrace();    }
    }
    

    See these also:

    • am-i-using-jdbc-connection-pooling
    • configuring-jdbc-pool-high-concurrency

提交回复
热议问题