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&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>
서블릿 컨트롤러, 커맨드 패턴 (0) | 2020.07.05 |
---|---|
컨트롤러 서블릿 패턴 (0) | 2020.07.05 |
커넥션 풀 connection pool (0) | 2020.07.05 |
서블릿 구현, 애노테이션 맵핑 (0) | 2020.07.05 |
[Mac, IntelliJ, tomcat] 맥 인텔리제이에서 톰캣 연동 (0) | 2020.06.19 |
댓글 영역