Connection Pool

์ฝ”์ฝ”ยท2020๋…„ 8์›” 6์ผ
0

Servlet/JSP

๋ชฉ๋ก ๋ณด๊ธฐ
6/20
post-thumbnail

๐Ÿ“š ์ž๋ฐ” ์›น์„ ๋‹ค๋ฃจ๋Š” ๊ธฐ์ˆ ์„ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

์ปค๋„ฅ์…˜ ํ’€

์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹คํ–‰๋จ๊ณผ ๋™์‹œ์— ์—ฐ๋™ํ•  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ๋ฏธ๋ฆฌ ์—ฐ๊ฒฐ ํ•ด๋‘”๋‹ค. ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ๋ฏธ๋ฆฌ ์—ฐ๊ฒฐํ•ด๋†“์€ ์ƒํƒœ๋ฅผ ์ด์šฉํ•ด ๋น ๋ฅด๊ฒŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ จ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์š”์•ฝํ•˜์ž๋ฉด ๋ฏธ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ฒฐ ์‹œํ‚จ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๋Š” ๊ธฐ์ˆ ์„ ์ปค๋„ฅ์…˜ ํ’€์ด๋ผ ๋ถ€๋ฅธ๋‹ค.

JDBC์˜ ๋ฌธ์ œ์ ์€ ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.
Connection Pool๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฏธ๋ฆฌ ์—ฐ๊ฒฐ๋œ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๋ฏ€๋กœ ๋น ๋ฅด๊ฒŒ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ปค๋„ฅ์…˜ ํ’€ ๋™์ž‘๊ณผ์ •

  1. ํ†ฐ์บฃ ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰

  2. Connection pool ๊ฐ์ฒด ์ƒ์„ฑ

  3. ์ปค๋„ฅ์…˜ ๊ฐ์ฒด <-> DBMS ์—ฐ๊ฒฐ

  4. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๋™ ์ž‘์—… ์‹œ, ConnectionPool์ด ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ

    ํ†ฐ์บฃ์€ ์ž์ฒด์ ์œผ๋กœ ConnectionPool์„ ์ œ๊ณตํ•œ๋‹ค.

JNDI

Java Naming and Directory Interface
ํ•„์š”ํ•œ ์ž์›์„ key-value๋กœ ์ €์žฅํ•ด์„œ ํ•„์š”ํ•  ๋•Œ key๋ฅผ ์ด์šฉํ•ด value๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•.

์ฆ‰ ๋ฏธ๋ฆฌ ์ ‘๊ทผํ•  ์ž์›์— ํ‚ค๋ฅผ ์ง€์ •ํ•œ ๋‹ค์Œ, ์•ฑ์ด ์‹คํ–‰ ์ค‘์ผ ๋•Œ ํ•ด๋‹น ํ‚ค๋ฅผ ์ด์šฉํ•ด ์ž์›์— ์ ‘๊ทผํ•ด์„œ ์ž‘์—…ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์‹ค์ œ๋กœ ConnectionPool๊ฐ์ฒด๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ javaSE๊ฐ€ ์ œ๊ณตํ•˜๋Š” javax.sql.DataSourceํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ†ฐ์บฃ์ด ๋งŒ๋“ค์–ด๋†“์€ ConnectionPool๊ฐ์ฒด์— ์ ‘๊ทผํ•  ๋•Œ๋Š” JNDI๋ฅผ ์ด์šฉํ•œ๋‹ค.

JNDI ์‚ฌ์šฉ ์˜ˆ

  • ์›น ๋ธŒ๋ผ์šฐ์ € name / value ์Œ์œผ๋กœ ์ „์†กํ•œ ํ›„ ์„œ๋ธ”๋ฆฟ์—์„œ getParameter()๋กœ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ
  • HashMap์— key/value๋กœ ์ €์žฅํ•œ ํ›„ ํ‚ค๋ฅผ ์ด์šฉํ•ด ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ

ํ†ฐ์บฃ DataSource ์„ค์ • ๋ฐ ์‚ฌ์šฉ

1.dbpc.jarํŒŒ์ผ ์„ค์น˜

  • jarํŒŒ์ผ ๋‹ค์šด ๋ฐ›๊ณ  ์••์ถ• ํ‘ผ๋‹ค.

  • ํ”„๋กœ์ ํŠธ ๋นŒ๋“œํŒจ์Šค์— jarํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•œ๋‹ค
    ์ฐธ๊ณ  : ์ด์ „ ๊ธ€

์ด์ „ ๊ธ€ ์—์„œ ์•ฝ๊ฐ„ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ–ˆ๋‹ค.

package pro07.sec01.ex01;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

public class MemberDAO {
	/* JDBC ์„ค์ •
	 * private static final String DRIVER = "com.mysql.cj.jdbc.Driver"; //DRIVER
	 * NAME private static final String URL =
	 * "jdbc:mysql://127.0.0.1:3306/servletex?serverTimezone=Asia/Seoul"; //JDBC
	 * URL/์Šคํ‚ค๋งˆ private static final String USER = "servlet"; //DB ID private static
	 * final String PWD = "1234"; //DB PW
	 */	
	
	private Connection con;
	private PreparedStatement pstmt;
	//DataSource์„ค์ •
	private DataSource ds;
	
	public MemberDAO() {
		try {
			//JNDI์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ๋ณธ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •
			Context ctx = new InitialContext();
			Context envContext = (Context) ctx.lookup("java:/comp/env");
			//context.xml์— ์„ค์ •ํ•œ name๊ฐ’์„ ์ด์šฉํ•ด ํ†ฐ์บฃ์ด ๋ฏธ๋ฆฌ ์—ฐ๊ฒฐํ•œ DataSource๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.
			ds = (DataSource) envContext.lookup("jdbc/mysql");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	} //์ƒ์„ฑ์ž
	
	List<MemberVO> listMembers() {
		List<MemberVO> list = new ArrayList<>();

		try {
//			connectDB(); //JDBC์„ค์ •
			
			//DataSource๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์—ฐ๊ฒฐํ•œ๋‹ค.
			con = ds.getConnection();
			String sql = "select * from t_member";
			System.out.println("Query : " + sql);
			pstmt = con.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			
			while(rs.next()) {
				/*
				 * select๋ฌธ์„ ๋‚ ๋ ค์„œ ๋ฐ›์•„์˜ฌ ์นผ๋Ÿผ๋“ค.
				 * getString(String columnLabel)์œผ๋กœ ๋ฐ›์•„์˜จ๋‹ค.
				 * */
				String id = rs.getString("id");
				String pwd = rs.getString("pwd");
				String name = rs.getString("name");
				String email = rs.getString("email");
				Date regdate = rs.getDate("regdate");
				
				/*
				 * ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ
				 * MemberVO๊ฐ์ฒด์— ๋‹ด๋Š”๋‹ค.
				 * */
				MemberVO vo = new MemberVO();
				vo.setId(id);
				vo.setPwd(pwd);
				vo.setName(name);
				vo.setEmail(email);
				vo.setRegdate(regdate);
				
				list.add(vo);
			}
			//์—ฐ๊ฒฐํ–ˆ๋˜ ๋ฐ˜๋Œ€์ˆœ์„œ๋กœ ๋‹ซ๋Š”๋‹ค.
			rs.close();
			pstmt.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	} //listMembers()
	
	/* JDBC์„ค์ •
	 * private void connectDB() { try { Class.forName("com.mysql.cj.jdbc.Driver");
	 * System.out.println("DRIVER LOADING....."); con =
	 * DriverManager.getConnection(URL, USER, PWD);
	 * System.out.println("Connection ์ƒ์„ฑ");
	 * 
	 * } catch (Exception e) { e.printStackTrace(); } }
	 */
}

๊ฒฐ๊ณผ๋Š” ๊ฐ™๋‹ค. ๋‹ค๋งŒ ์ด๋ฒˆ์—” ์ปค๋„ฅ์…˜ํ’€์„ ์ด์šฉํ•ด DB์™€ ์—ฐ๋™ํ–ˆ๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€