본문 바로가기

Backend/JSP & Servlet

[JSP & Servlet] WAS에서 제공하는 Connection Pool 이용하는 방법

728x90

커넥션풀을 직접 구현하면 개발자의 역량에 따라 성능이 다르고 보안이슈가 있을 수 있기 때문에 WAS에서 제공해주는 Connection Pool을 이용하는 것이 좋다. 이것을 DBCP API라고 한다. 

 

<개발환경>

jdk 1.8 |  oracle 11g | tomcat 9.0

 

 

## WAS 에서 제공하는 Connection Pool 이용하기
DBCP API 를 이용한 커넥션 풀을 사용하기 위하여 관련 jar 파일을 다운로드 한다. 

 

1) DBCP 

commons.apache.org/proper/commons-dbcp/download_dbcp.cgi

 

DBCP – Download Apache Commons DBCP

Download Apache Commons DBCP Using a Mirror We recommend you use a mirror to download our release builds, but you must verify the integrity of the downloaded files using signatures downloaded from our main distribution directories. Recent releases (48 hour

commons.apache.org

2) Collections 

commons.apache.org/proper/commons-collections/download_collections.cgi

 

Collections – Download Apache Commons Collections

Download Apache Commons Collections Using a Mirror We recommend you use a mirror to download our release builds, but you must verify the integrity of the downloaded files using signatures downloaded from our main distribution directories. Recent releases (

commons.apache.org

3) pool

commons.apache.org/proper/commons-pool/download_pool.cgi

 

Pool – Download Apache Commons Pool

Download Apache Commons Pool Using a Mirror We recommend you use a mirror to download our release builds, but you must verify the integrity of the downloaded files using signatures downloaded from our main distribution directories. Recent releases (48 hour

commons.apache.org

 

이 3파일을 받아서, 파일에서 jar 파일중 뒤에 -~없는 것을 lib 폴더에 넣어서 사용하면 된다. 

등록

# context.xml (WebContent/META-INF/context.xml)

META-INF 파일 안에  context.xml 을 추가한다.

<?xml version="1.0" encoding="UTF-8"?>
<Context>
	<Resource name="jdbc/myOracle" 
	auth="Container" 
	driverClassName="oracle.jdbc.driver.OracleDriver" 
	type="javax.sql.DataSource" 
	url ="jdbc:oracle:thin:@localhost:1521:xe" 
	username="mytest" 
	password="mytest" 
	maxTotal ="8" 
	maxIdle="10" 
	maxWaitMillis="-1"/>
</Context>

 

# web.xml 

	<resource-ref>
		<description>ConnectionPool</description>
		<res-ref-name>jdbc/myOracle</res-ref-name>
		<res-type>javax.sql.DataSource</res-type>
		<res-auth>Container</res-auth>
	</resource-ref>

- 이부분을 추가해준다.

 

# 실행 코드 

1) TempMemberDACP.java

package jdbc;

import java.sql.*;
import java.util.Vector;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class TempMemberDBCP {

	public TempMemberDBCP() {}
	private Connection getConnection() {
		Connection conn =null;
		try {
			Context init = new InitialContext();
			DataSource ds = (DataSource) init.lookup("java:comp/env/jdbc/myOracle");  //web.xml 에서 보고 가져옴.
			conn = ds.getConnection();
		} catch(NamingException ne) {
			ne.printStackTrace();
		} catch(SQLException sqle) {
			sqle.printStackTrace();
		}
		return conn;
	}
	
	public Vector<TempMemberVO> getMemberList() {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		Vector<TempMemberVO> vecList = new Vector<>();

		try {
			conn = this.getConnection(); 
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select * from tempmember");
			while (rs.next()) {
				TempMemberVO vo = new TempMemberVO();
				vo.setId(rs.getString("id"));
				vo.setPasswd(rs.getString("passwd"));
				vo.setName(rs.getString("name"));
				vo.setMem_num1(rs.getString("mem_num1"));
				vo.setMem_num2(rs.getString("mem_num2"));
				vo.setEmail(rs.getString("e_mail"));
				vo.setPhone(rs.getString("phone"));
				vo.setZipcode(rs.getString("zipcode"));
				vo.setAddress(rs.getString("address"));
				vo.setJob(rs.getString("job"));
				vecList.add(vo);
			}
		} catch (Exception e) {
			System.out.println(e);
	    }finally{
	    	if(rs != null) try{ rs.close();} catch(SQLException e){}
	    	if(stmt != null) try{ stmt.close();} catch(SQLException e){}
	    	if(conn != null) try{ conn.close();} catch(SQLException e){}
	    }
		return vecList;
	}
	
}

2)  usingBeanBool.jsp

<%@page import="jdbc.TempMemberVO"%>
<%@page import="java.sql.*, java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DBCP <title>
</head>
<body>
	<h2>DBCP</h2>
	<br>
	<h3>회원정보</h3>
	<table border="1">
		<tr>
			<td>id</td>
			<td>passwd</td>
			<td>name</td>
			<td>mem_num1</td>
			<td>mem_num2</td>
			<td>email</td>
			<td>phone</td>
			<td>zipcode</td>
			<td>address</td>
			<td>job</td>
		</tr>
		<jsp:useBean id="dao" class="jdbc.TempMemberDBCP" scope="page" />

		<%
		Vector <TempMemberVO> vlist =dao.getMemberList();
		int counter = vlist.size();
		for(int i = 0 ; i<vlist.size(); i++){
			TempMemberVO vo = vlist.elementAt(i);	
		%>
		<tr>
			<td><%= vo.getId()%></td>
			<td><%= vo.getPasswd()%></td>
			<td><%= vo.getName()%></td>
			<td><%= vo.getMem_num1() %></td>
			<td><%= vo.getMem_num2() %></td>
			<td><%= vo.getEmail()%></td>
			<td><%=vo.getPhone() %></td>
			<td><%=vo.getZipcode()%></td>
			<td><%=vo.getAddress()%></td>
			<td><%=vo.getJob()%></td>
		</tr>
		<%
	  	  }
		%>
	</table>                                        
	<br><br> total records :<%=counter %>
</body>
</html>

 

실행결과

728x90