This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dongho Sim <dhsim86@gmail.com>
- Loading branch information
Showing
4 changed files
with
91 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters