Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package connectionPool;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.HashMap;
- public class ConnectionPool {
- private static int maxConnectionCount = 3;
- private ArrayList<Connection> connectionPool;
- private HashMap<Thread, Connection> connectionInUse;
- private String url = "jdbc:oracle:thin:@localhost:1521:xe";
- private String account = "user08";
- private String password = "1111";
- static{
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- public ConnectionPool(){}
- public void openDB(int aConnectionCount){
- connectionPool = new ArrayList<>();
- connectionInUse = new HashMap<>();
- addConnection(aConnectionCount);
- }
- public void closeDB(){
- if (connectionInUse != null) {
- for (Connection conn : connectionInUse.values()) { //JDK1.5 for-each
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- connectionInUse.clear();
- connectionInUse = null;
- }
- if (connectionPool != null) {
- for (Connection conn : connectionPool) { //JDK1.5 for-each
- try {
- conn.close();
- System.out.println("Connection in pool closed.");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- connectionPool.clear();
- connectionPool = null;
- }
- }
- public synchronized Connection getConnection(){
- Connection con = null;
- int totalConnections = connectionPool.size() + connectionInUse.size();
- // dynamically create Connection object until it reaches its maximum limit
- if (connectionPool.size() == 0 && totalConnections < maxConnectionCount){
- addConnection(1);
- System.out.println("create new Connection.");
- }else if (connectionPool.size() == 0 && !(totalConnections < maxConnectionCount)){
- System.out.println("reach the maximum number of Connections. Can't create Connection anymore.");
- }
- // if no connection available now, tell the current Thread to wait
- if (connectionPool.size() == 0){
- System.out.println("This Thread is waiting**************************************************");
- try {
- wait(); // look for notifyAll() - to see when will this code keeps going down.
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("This Thread gets Connection.**************************************************");
- }
- // if connection available, give one to the current Thread
- if (connectionPool.size() > 0){
- con = connectionPool.remove(connectionPool.size()-1);
- connectionInUse.put(Thread.currentThread(), con);
- }
- return con;
- }
- public synchronized void returnConnection(Connection con){
- connectionPool.add(con);
- connectionInUse.remove(Thread.currentThread());
- //notifyAll();
- notify(); //一次叫一個Thread起來就好,不然會出事,因為若同時兩個Thread起來一起往下跑,會只有一個人拿到Connection,那另一個人就會NullPointerException
- }
- private synchronized void addConnection(int aConnectionCount){
- try {
- for(int i = 0; i < aConnectionCount; i++){
- Connection con = DriverManager.getConnection(url, account, password);
- connectionPool.add(con);
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public int size(){
- return this.connectionPool.size();
- }
- }
Add Comment
Please, Sign In to add comment