java

JSP - ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ(5) DB Connection Pool(CP)

dev_summer 2020. 11. 9. 15:31

 

p.443 DB Connection Pool(CP) : datasource

 

1. ํ†ฐ์บฃ ์‚ฌ์ดํŠธ๋กœ ์ด๋™ (http://tomcat.apache.org/)

2. ๋ฒ„์ „์— ๋งž๊ฒŒ ํด๋ฆญ

ํ†ฐ์บฃ ํŽ˜์ด์ง€ ์™ผ์ชฝ ๋ฐ”

3. JDBC Datasouce๋กœ ์ด๋™

 

 

4. MySQL DBCP ์˜ˆ์ œ ๋ณด๊ธฐ

 

 

5. JDBC MySQL Connector ๋‹ค์šด๋กœ๋“œ

 

https://www.mysql.com/products/connector/๋กœ ์ด๋™

 

6. MySQL๋ฒ„์ „์œผ๋กœ ๋‹ค์šด๋กœ๋“œ, ์••์ถ• ํ’€๊ณ  jarํŒŒ์ผ lib์— ๋„ฃ๊ธฐ, ๋”ฐ๋กœ ๋นŒ๋“œํŒจ์Šค ์•ˆํ•ด๋„๋จ

 

 

 

 

 

 

7. ์†Œ์Šค์ฝ”๋“œ ์ž‘์„ฑ

http://tomcat.apache.org/tomcat-8.5-doc/jndi-datasource-examples-howto.html#MySQL_DBCP_2_Example

http://tomcat.apache.org/tomcat-8.5-doc/jndi-datasource-examples-howto.html#MySQL_DBCP_2_Example

์œ„ ์‚ฌ์ดํŠธ๋ฅผ ํ™•์ธ ํ•˜์—ฌ ์ฐจ๋ก€๋Œ€๋กœ ๊ตฌ์„ฑ

 

 

 

(1) META-INF/context.xml

 

 <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxTotal="100" maxIdle="30" maxWaitMillis="10000"
               username="jsp" password="1234" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/javatest??serverTimezone=Asia/Seoul
               "/>

 

 

(2) WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee                       http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

  <description>MySQL Test App</description>
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

 

 

(3) DBUtil

package com.cos.board.db;

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

public class DButil {
	public static Connection getConnection() {
		try {

			Context initContext = new InitialContext();
			Context envContext = (Context) initContext.lookup("java:/comp/env");
			DataSource ds = (DataSource) envContext.lookup("jdbc/TestDB");
			Connection conn = ds.getConnection();
			System.out.println("DB Success");
			return conn;
		} catch (Exception e) {
			System.out.println("DB Fail");
			e.printStackTrace();
		}
		return null;
	}
}

 

 

 

 

(3) ์ฃผ์˜ ํ•  ์ 

 

- web.xml ์—์„œ ver="3.1"๊ณผ ๋‚ด ํ”„๋กœ์ ํŠธ์˜ ํ†ฐ์ผ“๋ฒ„์ „๊ณผ ๋งž์ถฐ์•ผํ•œ๋‹ค.

 

๋‚ด ํ”„๋กœ์ ํŠธ์˜ ํ†ฐ์บฃ ๋ฒ„์ „ ๋ณด๋Š” ๋ฐฉ๋ฒ•

 

 

 

 

 

- context.xml ์˜ ์„œ๋ฒ„์‹œ๊ฐ„์„ค์ •์„ ํ•ด์„œ ์—ฐ๊ฒฐํ•ด์•ผํ•œ๋‹ค.

mysql ๋ฒ„์ „์ด 8.0 ๋ฒ„์ „ ๋ถ€ํ„ฐ๋Š”severTimezone์„ ์ž๋™์œผ๋กœ ์žก์ง€๋ชปํ•˜๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด timezone์ถ”๊ฐ€ํ•ด์•ผํ•จ

 

context.xml์ˆ˜์ •

 

private String url = "jdbc:mysql://localhost:3306/calendar";

->

private String url = "jdbc:mysql://localhost:3306/calendar?serverTimezone=Asia/Seoul";

โ€‹

 

 

 

 

 

 

 

========================================================== ์ฐธ๊ณ ์‚ฌํ•ญ

- ๋””ํ”Œ๋ฆฌ์ผ€์ดํŠธ ์˜ค๋ฅ˜๋ฉ”์‹œ์ง€๊ฐ€ ๋œจ๋Š” ๊ฒฝ์šฐ CJ ๊ฒฝ๋กœ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ์•„ ํ•จ.

ํ˜„์žฌ ๊ฒฝ๋กœ์˜ ๋“œ๋ผ์ด๋ธŒํŒŒ์ผ์€ ๊ณง ์ง€์› ์ข…๋ฃŒ ๋  ์˜ˆ์ • CJ๋กœ ๊ฒฝ๋กœ๋ฅผ ๋ฐ”๊ฟ€๊ฒƒ์„ ๊ถŒ์žฅํ•จ.

context.xml์˜ driverClassName="com.mysql.cj.jdbc.Driver"์„ ๋ณ€๊ฒฝํ•ด์ฃผ์–ด์•ผํ•จ.

 

- taglib