im writing a coupon system project and I have a problem with my connectionpool. I made it singleton and it should create 10 connections and print "Creating connection #num" 10 times when i call its instance for the first time. My DBDAO classes are singletons and have private instance of that connectionpool; When i try to test my DBDAO classes and call my DBDAO instance, the console print "Creating connection #num" 20 times for some reason.. would be glad for some help!

The ConnectionPool

package com.app.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Stack;

import com.app.db.DBManager;

public class ConnectionPool {
  private Stack<Connection> connections = new Stack<Connection>();

  private static ConnectionPool instance = null;// = new ConnectionPool();

  private ConnectionPool() {
    for (int i = 1; i <= 10; i++) {
        System.out.println("Creating connection #" + i);
        try {
            Connection conn = DriverManager.getConnection(DBManager.getUrl(), DBManager.getUser(),
                    DBManager.getPass());
            connections.push(conn);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }
}

public static ConnectionPool getInstance() {
    if (instance == null) {
        synchronized (ConnectionPool.class) {
            if (instance == null) {
                instance = new ConnectionPool();
            }
        }
    }
    return instance;
}

The DBDAO

package com.app.dbdao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.app.beans.Company;
import com.app.dao.CompaniesDAO;
import com.app.utils.ConnectionPool;

public class CompaniesDBDAO implements CompaniesDAO {

private static CompaniesDBDAO instance = null;
private ConnectionPool connectionPool = ConnectionPool.getInstance();

private static final String QUERY_GET_ALL = "SELECT * FROM `jb project`.companies;";
private static final String QUERY_INSERT = "INSERT INTO `jb project`.`companies` (`name`, `email`, `password`) VALUES (?, ?, ?);";
private static final String QUERY_DELETE = "DELETE FROM `jb project`.`companies` WHERE (`id` = ?);";
private static final String QUERY_UPDATE = "UPDATE `jb project`.`companies` SET `name` = ?, `email` = ?, `password` = ? WHERE (`id` = ?);";
private static final String QUERY_GET_ONE = "SELECT * FROM `jb project`.companies WHERE (`id` = ?);";

private CompaniesDBDAO() {

}

public static CompaniesDBDAO getInstance() {
    if (instance == null) {
        synchronized (CompaniesDBDAO.class) {
            if (instance == null) {
                instance = new CompaniesDBDAO();
            }
        }
    }
    return instance;
}

The Testing

package com.app.clr.dbdao;

import java.sql.Date;

import com.app.beans.Category;
import com.app.beans.Company;
import com.app.beans.Coupon;
import com.app.beans.Customer;
import com.app.dao.CompaniesDAO;
import com.app.dao.CouponsDAO;
import com.app.dao.CustomersDAO;
import com.app.db.DBManager;
import com.app.dbdao.CompaniesDBDAO;
import com.app.dbdao.CouponsDBDAO;
import com.app.dbdao.CustomersDBDAO;
import com.app.utils.ArtUtil;
import com.app.utils.ConnectionPool;

public class DBDAOTesting {

public static void main(String[] args) {
    DBManager.dropCreateTables();
    System.out.println("Tables were Created!");
    System.out.println("==========================================");
    CompaniesDBDAOTesting();
    CustomersDBDAOTesting();
    CouponsDBDAOTesting();

}

    public static void CompaniesDBDAOTesting() {
    ArtUtil.companiesDBDAOTesting();
    CompaniesDAO cDBDAO = CompaniesDBDAO.getInstance();
    ...}

The Console from the test with the DBManager line

Creating connection #1
Creating connection #2
Creating connection #3
Creating connection #4
Creating connection #5
Creating connection #6
Creating connection #7
Creating connection #8
Creating connection #9
Creating connection #10
Tables were Created!
==========================================
 _____                                   _          __________________  ___  
_____ 
/  __ \                                 (_)         |  _  \ ___ \  _  \/ _ \|  
_  |
| /  \/ ___  _ __ ___  _ __   __ _ _ __  _  ___  ___| | | | |_/ / | | / /_\ \ 
| | |
| |    / _ \| '_ ` _ \| '_ \ / _` | '_ \| |/ _ \/ __| | | | ___ \ | | |  _  | 
| | |
| \__/\ (_) | | | | | | |_) | (_| | | | | |  __/\__ \ |/ /| |_/ / |/ /| | | \ 
\_/ /
 \____/\___/|_| |_| |_| .__/ \__,_|_| |_|_|\___||___/___/ \____/|___/ \_| 
|_/\___/ 
                      | |                                                          
                      |_|                                                          
             _____         _   _                                               
            |_   _|       | | (_)                                              
              | | ___  ___| |_ _ _ __   __ _                                   
              | |/ _ \/ __| __| | '_ \ / _` |                                  
              | |  __/\__ \ |_| | | | | (_| |                                  
              \_/\___||___/\__|_|_| |_|\__, |                                  
                                        __/ |                                  
                                       |___/                   

The Console from the test without the DBManager line

Tables were Created!
==========================================
 _____                                   _          __________________  ___  
_____ 
/  __ \                                 (_)         |  _  \ ___ \  _  \/ _ \|  
_  |
| /  \/ ___  _ __ ___  _ __   __ _ _ __  _  ___  ___| | | | |_/ / | | / /_\ \ 
| | |
| |    / _ \| '_ ` _ \| '_ \ / _` | '_ \| |/ _ \/ __| | | | ___ \ | | |  _  | 
| | |
| \__/\ (_) | | | | | | |_) | (_| | | | | |  __/\__ \ |/ /| |_/ / |/ /| | | \ 
\_/ /
 \____/\___/|_| |_| |_| .__/ \__,_|_| |_|_|\___||___/___/ \____/|___/ \_| 
|_/\___/ 
                      | |                                                          
                      |_|                                                          
             _____         _   _                                               
            |_   _|       | | (_)                                              
              | | ___  ___| |_ _ _ __   __ _                                   
              | |/ _ \/ __| __| | '_ \ / _` |                                  
              | |  __/\__ \ |_| | | | | (_| |                                  
              \_/\___||___/\__|_|_| |_|\__, |                                  
                                        __/ |                                  
                                       |___/                                   
Creating connection #1
Creating connection #1
Creating connection #2
Creating connection #3
Creating connection #4
Creating connection #5
Creating connection #6
Creating connection #7
Creating connection #8
Creating connection #9
Creating connection #10
Creating connection #2
Creating connection #3
Creating connection #4
Creating connection #5
Creating connection #6
Creating connection #7
Creating connection #8
Creating connection #9
Creating connection #10
0

There are 0 best solutions below