lesson09_JSP_ConnectionPool.zip
0.01MB
ConnectionPool이란?
데이터베이스에 계속적으로 연결 되었다 끊김을 반복할 시 , 리소스를 많이 사용하여 성능이 저하 됩니다.
이를 해결하기 위해 ConnectionPool이라는 개념이 도입되었습니다.
1. ConnectionPool을 위해서 처음 서버가 구동 될 시, WAS(Tomcat) 서버에 DB 정보를 등록해야 합니다.
Eclipse 내의 Context.xml 에 Resource 를 등록합니다.
경로 : {Tomcat Directory}\conf\context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--><!-- The contents of this file will be loaded for each web application --><Context>
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<Resource
auth="Container"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:orcl"
username="HR"
password="12341234"
name="jdbc/Oracle11g"
type="javax.sql.DataSource"
maxActive="8" <!-- Thread Pool 을 총 9개를 사용 합니다. -->
maxWait="10000" <!-- 만약 Pool이 부족하다면 10초간 기다려 추가로 만들어 줍니다 -->
/>
</Context>
2. 기존에는 데이터베이스 정보를 사용할때마다 넣어 주었지만, Tomcat 구동시, 정보가 들어 있기 때문에, context.lookup을 통하여
불러옵니다.
package lesson09_JSP_ConnectionPool;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
@WebServlet("/DeleteCountries")
public class DeleteCountries extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
PreparedStatement pstmt = null;
Connection con = null;
DataSource dataSource;
try {
Context context = new InitialContext();
dataSource = (DataSource) context.lookup("java:/comp/env/jdbc/Oracle11g");
con = dataSource.getConnection();
String sql = "DELETE FROM COUNTRIES WHERE COUNTRY_ID = ?";
pstmt= con.prepareStatement(sql);
pstmt.setString(1, "KR");
int result = pstmt.executeUpdate();
if (result==1) {
out.print("DELETE SUCCESS");
}else {
out.print("DELETE FAIL");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
3. Driver Loading , Connection 부분이 변경 되었습니다.
PreparedStatement 는 SQL을 먼저 선언 후 , ? 안에 순서대로 값을 넣어줍니다.
'Web Programming > JSP' 카테고리의 다른 글
10. JSP EL & JSTL (0) | 2020.03.24 |
---|---|
9. JSP DTO , DAO (0) | 2020.03.24 |
7. JSP DB Connect (0) | 2020.03.23 |
6. JSP Cookie & Session (0) | 2020.03.21 |
5. JSP 데이터 공유 (0) | 2020.03.21 |