Jam's story
[JSP] 커넥션풀 본문
커넥션 풀(Connection Pool)
JSP요청 + DB연동처리 + Connection 객체 생성,닫기+ 동시 접속자 많다면 성능 떨어진다.
Connection 객체를 미리 풀(pool) 속에 생성해 두고 가져다 사용하고 다시 반환하는 기법
톰캣 (WAS)이 제공하는 DBCP를 사용하는 방법
1번
-> WEB-INF > lib 폴더에 tomcat-dbcp.jar 추가 (jar파일 위치 : 나는 c드라이브 apache-tomcat 속 lib 속에 존재)
2번
커넥션풀 초기화 설정
https://tomcat.apache.org/tomcat-8.5-doc/jndi-datasource-examples-howto.html#Oracle_8i,_9i_&_10g
Apache Tomcat 8 (8.5.81) - JNDI Datasource How-To
JNDI Datasource configuration is covered extensively in the JNDI-Resources-HOWTO. However, feedback from tomcat-user has shown that specifics for individual configurations can be rather tricky. Here then are some example configurations that have been poste
tomcat.apache.org
META_INF > context.xml 추가
(추가에서 other에 들어가서 xml 검색!! 주의-> xml스키마파일아니다.)
-xml은 대소문자 구분한다.
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/myoracle"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:xe"
username="scott"
password="tiger"
maxTotal="20"
maxIdle="10"
maxWaitMillis="-1"/>
</Context>
MaxTotal 커넥션 풀안에 최대 생성할 커넥션 객체의 수
maxIdle 커넥션 풀이 보관할 수 있는 최대 유휴 객체의 수
maxWaitMillis최대 대기 시간 설정 속성 (음수:커넥션 객체를 얻어갈 때까지 무한정 기다리게 함)
blockWhenExhausted 풀이 관리하는 커넥션 객체가 모두 사용중인 상태에 커넥션 객체를 요청할 때true로 설정시 기다리게 하고false로 설정시 NoSuchElementException 예외 발생시키는 속성
web.xml에 추가
<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4번째
com.util.클래스 .java -DBCP 커넥션 객체 반환하는 메소드
ConnectionProvider.java
package com.util;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class ConnectionProvider {
public static Connection getConnection() throws NamingException, SQLException{
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();
return conn;
}
}
ex03.jsp
<%@page import="com.util.ConnectionProvider"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="java.sql.Connection"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="javax.naming.Context"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<title>2022. 6. 22.-오전 10:54:13</title>
</head>
<body>
<h3></h3>
<%
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();
%>
DBconn=<%=conn %>
<%
conn.close(); //커넥션 풀에 반환 (닫는거x)
%>
DBCP conn2=<%=ConnectionProvider.getConnection()%><br> //객체불러오기
</body>
</html>
'JSP' 카테고리의 다른 글
[JSP] EL (0) | 2022.06.22 |
---|---|
[JSP] 세션 (0) | 2022.06.22 |
[JSP] 로그인 화면 (0) | 2022.06.22 |
[JSP] 클라이언트와 대화 - 쿠키 (0) | 2022.06.21 |
[JSP] 게시판 - 목록, 글쓰기 기능 , 페이징 처리 (0) | 2022.06.20 |