Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch05] 5.12, Used DBConnectionPool.
Browse files Browse the repository at this point in the history
Signed-off-by: Dongho Sim <dhsim86@gmail.com>
  • Loading branch information
dhsim86 committed Mar 25, 2017
1 parent 0f6202d commit 1c860a3
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 16 deletions.
16 changes: 6 additions & 10 deletions src/main/java/ContextLoaderListener.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import Lesson05.DBConnectionPool;
import Lesson05.MemberDao;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.sql.Connection;
import java.sql.DriverManager;

/**
* Created by Dongho on 2017. 3. 25..
Expand All @@ -14,23 +13,23 @@
@WebListener
public class ContextLoaderListener implements ServletContextListener {

private Connection conn;
private DBConnectionPool dbConnectionPool;

@Override
public void contextInitialized(ServletContextEvent event) {

try {
ServletContext sc = event.getServletContext();

Class.forName(sc.getInitParameter("driver"));
conn = DriverManager.getConnection(
dbConnectionPool = new DBConnectionPool(
sc.getInitParameter("driver"),
sc.getInitParameter("url"),
sc.getInitParameter("username"),
sc.getInitParameter("password")
);

MemberDao memberDao = new MemberDao();
memberDao.setConnection(conn);
memberDao.setDbConnectionPool(dbConnectionPool);

sc.setAttribute("memberDao", memberDao);
}
Expand All @@ -41,10 +40,7 @@ public void contextInitialized(ServletContextEvent event) {

@Override
public void contextDestroyed(ServletContextEvent event) {
try {
conn.close();
} catch (Exception e) {

}
dbConnectionPool.closeAll();
}
}
55 changes: 55 additions & 0 deletions src/main/java/Lesson05/DBConnectionPool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package Lesson05;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;

/**
* Created by Dongho on 2017. 3. 25..
*/
public class DBConnectionPool {

private String url;
private String username;
private String password;

List<Connection> connectionList = new ArrayList<>();

public DBConnectionPool(String driver, String url, String username, String password)
throws Exception {

this.url = url;
this.username = username;
this.password = password;

Class.forName(driver);
}

public Connection getConnection() throws Exception {

if (connectionList.size() > 0) {

Connection conn = connectionList.get(0);

if (conn.isValid(10)) {
return conn;
}
}

return DriverManager.getConnection(url, username, password);
}

public void returnConnection(Connection conn) throws Exception {

connectionList.add(conn);
}

public void closeAll() {

for (Connection conn : connectionList) {

try { conn.close(); } catch (Exception e) {}
}
}
}
5 changes: 2 additions & 3 deletions src/main/java/Lesson05/LoginServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ protected void doPost(

ServletContext sc = this.getServletContext();

MemberDao memberdao = new MemberDao();
memberdao.setConnection((Connection)sc.getAttribute("conn"));
MemberDao memberDao = (MemberDao)sc.getAttribute("memberDao");

Member member = memberdao.exist(
Member member = memberDao.exist(
request.getParameter("email"),
request.getParameter("password")
);
Expand Down
31 changes: 28 additions & 3 deletions src/main/java/Lesson05/MemberDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@
*/
public class MemberDao {

Connection connection;
private DBConnectionPool dbConnectionPool;

public void setConnection(Connection connection) {
this.connection = connection;
public void setDbConnectionPool(DBConnectionPool dbConnectionPool) {
this.dbConnectionPool = dbConnectionPool;
}

public List<Member> selectList() throws Exception {

Connection connection = null;
Statement stmt = null;
ResultSet rs = null;

try {

connection = dbConnectionPool.getConnection();

stmt = connection.createStatement();
rs = stmt.executeQuery(
"select mno, mname, email, cre_date" +
Expand All @@ -47,14 +51,20 @@ public List<Member> selectList() throws Exception {
finally {
try { if (rs != null) rs.close(); } catch (Exception e) {}
try { if (stmt != null) stmt.close(); } catch (Exception e) {}

if (connection != null) dbConnectionPool.returnConnection(connection);
}
}

public int insert(Member member) throws Exception {

Connection connection = null;
PreparedStatement stmt = null;

try {

connection = dbConnectionPool.getConnection();

stmt = connection.prepareStatement(
"insert into members(email, pwd, mname, cre_date, mod_date)" +
" values(?, ?, ?, now(), now())"
Expand All @@ -71,15 +81,19 @@ public int insert(Member member) throws Exception {
}
finally {
try { if (stmt != null) stmt.close(); } catch (Exception e) {}
if (connection != null) dbConnectionPool.returnConnection(connection);
}
}

public int delete(int no) throws Exception {

Connection connection = null;
PreparedStatement stmt = null;

try {

connection = dbConnectionPool.getConnection();

stmt = connection.prepareStatement(
"delete from members" +
" where mno = ?"
Expand All @@ -94,16 +108,19 @@ public int delete(int no) throws Exception {
}
finally {
try { if (stmt != null) stmt.close(); } catch (Exception e) {}
if (connection != null) dbConnectionPool.returnConnection(connection);
}
}

public Member selectOne(int no) throws Exception {

Connection connection = null;
PreparedStatement stmt = null;
ResultSet rs = null;

try {

connection = dbConnectionPool.getConnection();
stmt = connection.prepareStatement(
"select mno, mname, email, cre_date" +
" from members" +
Expand All @@ -130,15 +147,19 @@ public Member selectOne(int no) throws Exception {
finally {
try { if (rs != null) rs.close(); } catch (Exception e) {}
try { if (stmt != null) stmt.close(); } catch (Exception e) {}
if (connection != null) dbConnectionPool.returnConnection(connection);
}
}

public int update(Member member) throws Exception {

Connection connection = null;
PreparedStatement stmt = null;

try {

connection = dbConnectionPool.getConnection();

stmt = connection.prepareStatement(
"update members set email = ?, mname = ?, mod_date = now()" +
" where mno = ?"
Expand All @@ -155,16 +176,19 @@ public int update(Member member) throws Exception {
}
finally {
try { if (stmt != null) stmt.close(); } catch (Exception e) {}
if (connection != null) dbConnectionPool.returnConnection(connection);
}
}

public Member exist(String email, String password) throws Exception {

Connection connection = null;
PreparedStatement stmt = null;
ResultSet rs = null;

try {

connection = dbConnectionPool.getConnection();
stmt = connection.prepareStatement(
" select mno, mname, email, cre_date" +
" from members" +
Expand Down Expand Up @@ -195,6 +219,7 @@ public Member exist(String email, String password) throws Exception {
finally {
try { if (rs != null) rs.close(); } catch (Exception e) {}
try { if (stmt != null) stmt.close(); } catch (Exception e) {}
if (connection != null) dbConnectionPool.returnConnection(connection);
}
}
}

0 comments on commit 1c860a3

Please sign in to comment.