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

Commit

Permalink
[ch05] 5.13. Used BasicDataSource.
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 1c860a3 commit 8e1934b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@
<version>1.1.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-pool/commons-pool -->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>

</dependencies>

</project>
20 changes: 11 additions & 9 deletions src/main/java/ContextLoaderListener.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import Lesson05.DBConnectionPool;
import Lesson05.MemberDao;
import org.apache.commons.dbcp.BasicDataSource;

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

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

private DBConnectionPool dbConnectionPool;
private BasicDataSource dataSource;

@Override
public void contextInitialized(ServletContextEvent event) {

try {
ServletContext sc = event.getServletContext();

dbConnectionPool = new DBConnectionPool(
sc.getInitParameter("driver"),
sc.getInitParameter("url"),
sc.getInitParameter("username"),
sc.getInitParameter("password")
);
dataSource = new BasicDataSource();

dataSource.setDriverClassName(sc.getInitParameter("driver"));
dataSource.setUrl(sc.getInitParameter("url"));
dataSource.setUsername(sc.getInitParameter("username"));
dataSource.setPassword(sc.getInitParameter("password"));

MemberDao memberDao = new MemberDao();
memberDao.setDbConnectionPool(dbConnectionPool);
memberDao.setDataSource(dataSource);

sc.setAttribute("memberDao", memberDao);
}
Expand All @@ -41,6 +43,6 @@ public void contextInitialized(ServletContextEvent event) {
@Override
public void contextDestroyed(ServletContextEvent event) {

dbConnectionPool.closeAll();
try { if (dataSource != null) dataSource.close(); } catch (SQLException e) {}
}
}
32 changes: 17 additions & 15 deletions src/main/java/Lesson05/MemberDao.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package Lesson05;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -9,10 +10,11 @@
*/
public class MemberDao {

private DBConnectionPool dbConnectionPool;
DataSource dataSource;

public void setDbConnectionPool(DBConnectionPool dbConnectionPool) {
this.dbConnectionPool = dbConnectionPool;
public void setDataSource(DataSource dataSource) {

this.dataSource = dataSource;
}

public List<Member> selectList() throws Exception {
Expand All @@ -23,7 +25,7 @@ public List<Member> selectList() throws Exception {

try {

connection = dbConnectionPool.getConnection();
connection = dataSource.getConnection();

stmt = connection.createStatement();
rs = stmt.executeQuery(
Expand Down Expand Up @@ -52,7 +54,7 @@ public List<Member> selectList() throws Exception {
try { if (rs != null) rs.close(); } catch (Exception e) {}
try { if (stmt != null) stmt.close(); } catch (Exception e) {}

if (connection != null) dbConnectionPool.returnConnection(connection);
try { if (connection != null) connection.close(); } catch (Exception e) {}
}
}

Expand All @@ -63,7 +65,7 @@ public int insert(Member member) throws Exception {

try {

connection = dbConnectionPool.getConnection();
connection = dataSource.getConnection();

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

Expand All @@ -92,7 +94,7 @@ public int delete(int no) throws Exception {

try {

connection = dbConnectionPool.getConnection();
connection = dataSource.getConnection();

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

Expand All @@ -120,7 +122,7 @@ public Member selectOne(int no) throws Exception {

try {

connection = dbConnectionPool.getConnection();
connection = dataSource.getConnection();
stmt = connection.prepareStatement(
"select mno, mname, email, cre_date" +
" from members" +
Expand All @@ -147,7 +149,7 @@ 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);
try { if (connection != null) connection.close(); } catch (Exception e) {}
}
}

Expand All @@ -158,7 +160,7 @@ public int update(Member member) throws Exception {

try {

connection = dbConnectionPool.getConnection();
connection = dataSource.getConnection();

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

Expand All @@ -188,7 +190,7 @@ public Member exist(String email, String password) throws Exception {

try {

connection = dbConnectionPool.getConnection();
connection = dataSource.getConnection();
stmt = connection.prepareStatement(
" select mno, mname, email, cre_date" +
" from members" +
Expand Down Expand Up @@ -219,7 +221,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);
try { if (connection != null) connection.close(); } catch (Exception e) {}
}
}
}

0 comments on commit 8e1934b

Please sign in to comment.