상세 컨텐츠

본문 제목

커넥션 풀 connection pool : 초기화파라미터 지정

JSP

by kwanghyup 2020. 7. 5. 17:09

본문

DBCPinit

package jdbc;

import org.apache.commons.dbcp2.*;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBCPInit extends HttpServlet {

    @Override
    public void init() throws ServletException {
        System.out.println("잘되니?");
        loadJDBCDriver();
        initConnectionPool();
    }

    private void loadJDBCDriver()  {
        String driverClass = getInitParameter("jdbcDriver"); //[여기]
        try {
            Class.forName(driverClass);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("fail to load jdbc Driver",e);
        }
    }

    private void initConnectionPool() {

        String url = getInitParameter("jdbcUrl"); //[여기]
        String uid = getInitParameter("dbUser"); //[여기]
        String upw = getInitParameter("dbPassword"); //[여기]

        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url,uid,upw);
        PoolableConnectionFactory poolableConnectionFactory =
                new PoolableConnectionFactory(connectionFactory,null);
        poolableConnectionFactory.setValidationQuery("select 1");

        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setTestWhileIdle(true);
        poolConfig.setMinIdle(4);
        poolConfig.setMaxTotal(50);

        GenericObjectPool<PoolableConnection> connectionPool =
                new GenericObjectPool<>(poolableConnectionFactory,poolConfig);

        try {
            Class.forName("org.apache.commons.dbcp2.PoolingDriver");
            PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
            String poolName = getInitParameter("poolName"); //[여기]
            driver.registerPool(poolName,connectionPool);
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

 

web.xml

<servlet>
        <servlet-name>dbcpInit</servlet-name>
        <servlet-class>jdbc.DBCPInit</servlet-class>
        <init-param>
            <param-name>jdbcDriver</param-name>
            <param-value>com.mysql.jdbc.Driver</param-value>
        </init-param>
        <init-param>
            <param-name>jdbcUrl</param-name>
            <param-value>
                jdbc:mysql://localhost:3306/guestbook?characterEncoding=UTF-8&amp;useSSL=false
            </param-value>
        </init-param>
        <init-param>
            <param-name>dbUser</param-name>
            <param-value>root</param-value>
        </init-param>
        <init-param>
            <param-name>dbPassword</param-name>
            <param-value>1234</param-value>
        </init-param>
        <init-param>
            <param-name>poolName</param-name>
            <param-value>guestbook</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

관련글 더보기

댓글 영역