我正在写一个优惠券系统项目,但我的连接池有问题。我使它成为单例,当我第一次调用它的实例时,它应该创建 10 个连接并打印“创建连接 #num”10 次。我的 DBDAO 类是单例的,并且有该连接池的私有实例;当我尝试测试我的 DBDAO 类并调用我的 DBDAO 实例时,控制台出于某种原因打印了 20 次“正在创建连接 #num”.. 很高兴得到一些帮助!
连接池
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;
}
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;
}
测试
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();
...}
带有 DBManager 行的测试控制台
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!
==========================================
_____ _ __________________ ___
_____
/ __ \ (_) | _ \ ___ \ _ \/ _ \|
_ |
| / \/ ___ _ __ ___ _ __ __ _ _ __ _ ___ ___| | | | |_/ / | | / /_\ \
| | |
| | / _ \| '_ ` _ \| '_ \ / _` | '_ \| |/ _ \/ __| | | | ___ \ | | | _ |
| | |
| \__/\ (_) | | | | | | |_) | (_| | | | | | __/\__ \ |/ /| |_/ / |/ /| | | \
\_/ /
\____/\___/|_| |_| |_| .__/ \__,_|_| |_|_|\___||___/___/ \____/|___/ \_|
|_/\___/
| |
|_|
_____ _ _
|_ _| | | (_)
| | ___ ___| |_ _ _ __ __ _
| |/ _ \/ __| __| | '_ \ / _` |
| | __/\__ \ |_| | | | | (_| |
\_/\___||___/\__|_|_| |_|\__, |
__/ |
|___/
没有 DBManager 行的测试控制台
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