[JSP] Connection pool & JNDI

ํ˜œ์ฟฐยท2023๋…„ 8์›” 23์ผ
1
post-thumbnail

๐Ÿ“Œ ๊ฐœ๋…

๐Ÿงฉ Connection pool

Connection pool์ด๋ž€? Connection ๊ฐ์ฒด๋ฅผ ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋  ๋•Œ๋งˆ๋‹ค ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์„œ๋น„์Šค๋˜๊ธฐ ์ „์— ์›น์„œ๋ฒ„์—์„œ ๋ฏธ๋ฆฌ ์ƒ์„ฑํ•˜์—ฌ ์ค€๋น„ํ•œ ๋‹ค์Œ, ํ•„์š”ํ•  ๋•Œ ์ค€๋น„๋œ Connection์„ ๊ฐ€์ ธ๋‹ค ์‚ฌ์šฉํ•จ์œผ๋กœ์จ JDBC ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ๋ฌธ์ œ์ ๋“ค์„ ๊ฐœ์„ ํ•œ ๊ธฐ์ˆ ์ด๋‹ค. ์„œ๋ฒ„๊ฐ€ ์‹œ์ž˜ ๋  ๋•Œ Connection๊ฐ์ฒด๋Š” ์„œ๋ฒ„์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ์ค€๋น„ํ•  ์ˆ˜ ์žˆ๊ณ , ์ด๋ ‡๊ฒŒ ์ค€๋น„๋œ Connection ๊ฐ์ฒด๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ Connection pool ์ด๋ผ๊ณ  ํ•œ๋‹ค.

๐Ÿ“ DBCP

DBCP๋ž€? DataBase Connection Pool์˜ ์•ฝ์ž๋กœ ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ์‚ฌ์ด๋“œ์ธ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ, ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์— ๋”ฐ๋ผ Connection์ด ์ƒ์„ฑ๋œ๋‹ค๋ฉด ์ˆ˜ ๋งŽ์€ ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญ์„ ํ–ˆ์„ ๋•Œ ์„œ๋ฒ„์— ๊ณผ๋ถ€ํ•˜๊ฐ€ ๊ฑธ๋ฆฌ๊ฒŒ ๋œ๋‹ค. ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์„ ์˜ˆ๋ฐฉํ•˜๊ธฐ ์œ„ํ•ด ๋ฏธ๋ฆฌ ์ผ์ • ๊ฐฏ์ˆ˜์˜ Connection์„ ๋งŒ๋“ค์–ด Pool์— ์ €์žฅ์„ ํ•˜๊ณ , ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์ด ๋ฐœ์ƒํ•˜๋ฉด Connection์„ ์ œ๊ณตํ•˜๊ณ  ์‚ฌ์šฉ์ž์™€์˜ ์—ฐ๊ฒฐ์ด ์ข…๋ฃŒ๋œ๋‹ค๋ฉด Pool์— ๋‹ค์‹œ ๋ฐ˜ํ™˜ํ•˜์—ฌ ๋ณด๊ด€ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

๐Ÿ“ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

Connection pool์„ ์‚ฌ์šฉํ•˜๋Š” ์ฃผ๋œ ์ด์œ ๋Š” "์†๋„ ํ–ฅ์ƒ"๊ณผ "์ž์› ๊ณต์œ "์— ์žˆ๋‹ค.
Connectionํด๋ž˜์Šค๊ฐ€ ๊ฐ์ฒดํ™” ๋  ๋•Œ, ๋‹ค๋ฅธ ์ผ๋ฐ˜์ ์ธ ํด๋ž˜์Šค๋“ค๋ณด๋‹ค ๋‹ค์†Œ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค. ๋งŒ์•ฝ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์›๊ฒฉ์ง€์— ์กด์žฌํ•œ๋‹ค๋ฉด, ๋„คํŠธ์›Œํฌ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋” ์ง€์—ฐ์ด ๋  ์ˆ˜๋„ ์žˆ๋‹ค. ๋˜ํ•œ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ปค๋„ฅ์…˜์„ ์ƒ์„ฑํ•œ๋‹ค๋ฉด, ์ด๋Ÿฌํ•œ ์‹œ๊ฐ„์ ์ธ ์˜ค๋ฒ„ํ—ค๋“œ๋Š” ์ „์ฒด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋Š๋ฆฌ๊ฒŒ ๋งŒ๋“ ๋‹ค. ์ด ๊ฒฝ์šฐ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹œ์ž‘๋  ๋•Œ ์ผ์ •์ˆ˜์˜ Connection์„ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด ๋†“๊ณ , Connection Pool์„ ํ†ตํ•ด ์žฌ์‚ฌ์šฉ ํ•œ๋‹ค๋ฉด ์ƒ๋‹นํ•œ ์†๋„ ํ–ฅ์ƒ์„ ๊ธฐ๋Œ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿงฉ JNDI

JNDI๋ž€? Java Naming and Directory Interface์˜ ์•ฝ์ž๋กœ ๋””๋ ‰ํ„ฐ๋ฆฌ ์„œ๋น„์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋ฐ ๊ฐ์ฒด๋ฅผ ๋ฐœ๊ฒฌ(discover)ํ•˜๊ณ  ์ฐธ๊ณ (lookup) ํ•˜๊ธฐ ์œ„ํ•œ ์ž๋ฐ” API๋‹ค.

๐Ÿ“Œ ๋ฐฉ๋ฒ•

๐Ÿงฉ Context.xml

ํ”„๋กœ์ ํŠธ์— WebContent\META-INF ํด๋”์— context.xml ํŒŒ์ผ์„ ์ž‘์„ฑํ•œ๋‹ค.

<Context>

	<Resource name="jdbc_maria" auth="Container" type="javax.sql.DataSource"

		driverClassName="org.mariadb.jdbc.Driver" loginTimeout="10" maxWait="5000"

		username="root" password="๋น„๋ฐ€๋ฒˆํ˜ธ" testOnBorrow="true"

		maxActive="500" maxIdle="100"

		url="jdbc:mariadb://localhost:3306/table์ด๋ฆ„" />

</Context>

์ด๋ ‡๊ฒŒ context.xml๋ฅผ ๋”ฐ๋กœ ์ž‘์„ฑํ•˜๋ฉด, ์ €๋ฒˆ ํฌ์ŠคํŒ…์ฒ˜๋Ÿผ db์—ฐ๊ฒฐํ•˜๋Š” ์ฝ”๋“œ๋Š” ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค. META-INF ํด๋”๋Š” ์™ธ๋ถ€์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— context.xml์„ ์“ด๋‹ค๋ฉด ๊ฐœ์ธ์ •๋ณด๋ฅผ ์™ธ๋ถ€์— ๋…ธ์ถœํ•˜์ง€ ์•Š์•„ ๋ณด์•ˆ์— ์œ ๋ฆฌ ํ•˜๋‹ค.

๐Ÿงฉ JDBC์™€ ๋น„๊ต

๐Ÿ“ JDBC


package pack5db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DbTest1 {
	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;
	public DbTest1() {
		try {
			Class.forName("org.mariadb.jdbc.Driver");
		} catch (Exception e) {
			System.out.println("๋กœ๋”ฉ ์‹คํŒจ :" +e);
			System.exit(0);
		}
		
		try {
			conn = DriverManager.getConnection("jdbc:mariadb://127.0.0.1:3306/test","root","seoho123");
		} catch (Exception e) {
			System.out.println("์—ฐ๊ฒฐ ์‹คํŒจ : "+e);
			System.exit(0);
		}

๐Ÿ“ Connection Pool & JNDI

// import๋ฌธ ์ƒ๋žต
public class ConnPooling {
	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;
	private DataSource ds;
	
	public ConnPooling() {
		try {
			Context context = new InitialContext();
			ds = (DataSource)context.lookup("java:comp/env/jdbc_maria"); 
		} catch (Exception e) {
			System.out.println("db ์—ฐ๊ฒฐ ์‹คํŒจ : "+e.getMessage());
		}
	}

java:comp/env๋Š” ๊ณ ์ •์œผ๋กœ ์“ฐ๊ณ  ๋’ค์— ์“ด jdbc_maria๋Š” context.xml์—์„œ name์œผ๋กœ ์“ด ๊ฒƒ์„ ์ฐธ๊ณ ํ•˜๋ฉด ๋œ๋‹ค. JDBC์— ๋น„ํ•ด ๊ฐœ์ธ์ •๋ณด๋ฅผ ์™ธ๋ถ€์— ๋…ธ์ถœ์‹œํ‚ค์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ณด์•ˆ์— ์œ ๋ฆฌํ•˜๋‹ค.

๐Ÿงฉ ๋ฉ”์„œ๋“œ

์†์„ฑ ์ด๋ฆ„์„ค๋ช…
initialSizeBasicDataSourceํด๋ž˜์Šค ์ƒ์„ฑํ›„ ์ตœ์ดˆ๋กœ getConnection() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ปค๋„ฅ์…˜ ํ’€์— ์ฑ„์›Œ ๋„ฃ์„ ์ปค๋„ฅ์…˜ ๊ฐœ์ˆ˜
maxActive๋™์‹œ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์ปค๋„ฅ์…˜ ๊ฐœ์ˆ˜ (๊ธฐ๋ณธ ๊ฐ’ : 8)
maxldle์ปค๋„ฅ์…˜ ํ’€์— ๋ฐ˜๋‚ฉํ•  ๋•Œ ์ตœ๋Œ€๋กœ ์œ ์ง€๋  ์ˆ˜ ์žˆ๋Š” ์ปค๋„ฅ์…˜ ๊ฐœ์ˆ˜ (๊ธฐ๋ณธ ๊ฐ’ : 8)
minIdle์ตœ์†Œํ•œ์œผ๋กœ ์œ ์ง€ํ•  ์ปค๋„ฅ์…˜ ๊ฐœ์ˆ˜ (๊ธฐ๋ณธ ๊ฐ’ : 0)

์ด๋ฏธ์ง€ ์ถœ์ฒ˜ : https://linked2ev.github.io/spring/2019/08/14/Spring-3-%EC%BB%A4%EB%84%A5%EC%85%98-%ED%92%80%EC%9D%B4%EB%9E%80/
dbcp : https://zzang9ha.tistory.com/376

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