简单模拟实现数据库连接池
实例1:
package com.bijian.thread; public class DB { //private static final int MAX_COUNT = 10; private static final DB instance = new DB(); private int count = 0; private int maxCount = 0; private DB() { } public static DB getInstance() { return instance; } public synchronized Connection getConnection() { Connection conn = new Connection(); count++; if (count > maxCount) { maxCount = count; System.out.println("(MAX)DB Connection count = " + maxCount); } return conn; } public synchronized void close() { count--; //System.out.println("(Close)DB Connection count = " + count); } }
package com.bijian.thread; public class DBUtil { private static Pool pool = new Pool(); public static Connection getConnection() throws Exception { //return DB.getInstance().getConnection(); Connection conn = null; conn = pool.getConnection(); return conn; } public static void close(Connection conn) { //DB.getInstance().close(); pool.back(conn); } }
package com.bijian.thread; public class Connection { public Connection() { try { Thread.sleep(100); } catch(Exception e) { e.printStackTrace(); } } public void execute(String sql) { try { Thread.sleep((int) Math.random() * 50); } catch(Exception e) { e.printStackTrace(); } } }
package com.bijian.thread; import java.util.ArrayList; import java.util.List; public class Pool { private static final int POOL_SIZE = 10; private List<Connection> connections = new ArrayList<Connection>(); public Pool() { for(int i = 0; i < POOL_SIZE; i++) { connections.add(DB.getInstance().getConnection()); } } public synchronized Connection getConnection() throws Exception { Connection conn = null; if (connections.size() > 0) { conn = connections.get(0); connections.remove(0); } else { wait(400); if (connections.size() > 0) { conn = connections.get(0); connections.remove(0); } else { throw new Exception("Connection Pool is empty!"); } } return conn; } public synchronized void back(Connection conn) { connections.add(conn); notify(); } }
package com.bijian.thread; public class Guest implements Runnable { private String name; public Guest(String name) { this.name = name; } @Override public void run() { Connection conn = null; try { conn = DBUtil.getConnection(); conn.execute(""); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } finally { if (conn != null) { DBUtil.close(conn); } } System.out.println(name + " executed."); } }
package com.bijian.thread; public class Main { private static final int MAX_GUEST = 100; /** * @param args */ public static void main(String[] args) { for (int i = 0; i < MAX_GUEST; i++) { new Thread(new Guest("Guest" + i)).start(); } } }
运行结果:
(MAX)DB Connection count = 1 (MAX)DB Connection count = 2 (MAX)DB Connection count = 3 (MAX)DB Connection count = 4 (MAX)DB Connection count = 5 (MAX)DB Connection count = 6 (MAX)DB Connection count = 7 (MAX)DB Connection count = 8 (MAX)DB Connection count = 9 (MAX)DB Connection count = 10 Guest75 executed. Guest74 executed. Guest73 executed. Guest79 executed. Guest81 executed. Connection Pool is empty! Guest1 executed. java.lang.Exception: Connection Pool is empty!Guest80 executed. Guest0 executed. Guest4 executed. Guest82 executed. Guest77 executed. Guest72 executed. Guest66 executed. Guest89 executed. Guest7 executed. Guest83 executed. Guest69 executed. Guest55 executed. Guest76 executed. Guest99 executed. Guest71 executed. Guest59 executed. Guest43 executed. Guest41 executed. Guest39 executed. Guest92 executed. Guest3 executed. Guest5 executed. Guest87 executed. Guest34 executed. Guest33 executed. Guest35 executed. Guest28 executed. Guest36 executed. at com.bijian.thread.Pool.getConnection(Pool.java:31) at com.bijian.thread.DBUtil.getConnection(DBUtil.java:10) at com.bijian.thread.Guest.run(Guest.java:16) at java.lang.Thread.run(Thread.java:619) Guest98 executed. Guest42 executed. Guest15 executed. Guest16 executed. Guest46 executed. Guest38 executed. Guest40 executed. Guest44 executed. Guest94 executed. Guest45 executed. Guest51 executed. Guest49 executed. Guest63 executed. Guest93 executed. Guest57 executed. Guest85 executed. Guest50 executed. Guest60 executed. Guest56 executed. Guest58 executed. Guest86 executed. Guest68 executed. Guest70 executed. Connection Pool is empty! Connection Pool is empty! java.lang.Exception: Connection Pool is empty! at com.bijian.thread.Pool.getConnection(Pool.java:31) Guest84 executed. at com.bijian.thread.DBUtil.getConnection(DBUtil.java:10) at com.bijian.thread.Guest.run(Guest.java:16) Connection Pool is empty! at java.lang.Thread.run(Thread.java:619) Connection Pool is empty! Guest52 executed. java.lang.Exception: Connection Pool is empty! Connection Pool is empty! Connection Pool is empty! Guest65 executed. at com.bijian.thread.Pool.getConnection(Pool.java:31) at com.bijian.thread.DBUtil.getConnection(DBUtil.java:10) at com.bijian.thread.Guest.run(Guest.java:16) Connection Pool is empty! Guest2 executed. at java.lang.Thread.run(Thread.java:619) Guest6 executed. Guest67 executed. java.lang.Exception: Connection Pool is empty! Guest90 executed. Guest88 executed. at com.bijian.thread.Pool.getConnection(Pool.java:31) at com.bijian.thread.DBUtil.getConnection(DBUtil.java:10) at com.bijian.thread.Guest.run(Guest.java:16) at java.lang.Thread.run(Thread.java:619) Guest64 executed. Guest8 executed. Guest54 executed. Guest78 executed. java.lang.Exception: Connection Pool is empty! at com.bijian.thread.Pool.getConnection(Pool.java:31) Guest13 executed. at com.bijian.thread.DBUtil.getConnection(DBUtil.java:10) at com.bijian.thread.Guest.run(Guest.java:16) at java.lang.Thread.run(Thread.java:619) java.lang.Exception: Connection Pool is empty! Guest12 executed. Guest17 executed. Guest11 executed. Guest10 executed. Guest47 executed. Guest9 executed. Guest48 executed. at com.bijian.thread.Pool.getConnection(Pool.java:31) at com.bijian.thread.DBUtil.getConnection(DBUtil.java:10) Guest95 executed. Guest22 executed. at com.bijian.thread.Guest.run(Guest.java:16) at java.lang.Thread.run(Thread.java:619)Guest18 executed. Guest14 executed. Guest30 executed. Guest96 executed. Guest19 executed. Guest23 executed. Guest31 executed. java.lang.Exception: Connection Pool is empty! at com.bijian.thread.Pool.getConnection(Pool.java:31) at com.bijian.thread.DBUtil.getConnection(DBUtil.java:10) at com.bijian.thread.Guest.run(Guest.java:16) at java.lang.Thread.run(Thread.java:619) java.lang.Exception: Connection Pool is empty! Guest20 executed. Guest27 executed. at com.bijian.thread.Pool.getConnection(Pool.java:31) at com.bijian.thread.DBUtil.getConnection(DBUtil.java:10) Guest24 executed. at com.bijian.thread.Guest.run(Guest.java:16) Guest97 executed. at java.lang.Thread.run(Thread.java:619) Guest25 executed. Guest21 executed. Guest32 executed. Guest29 executed. Guest26 executed. Guest37 executed. Guest62 executed. Guest91 executed. Guest61 executed. Guest53 executed.
通过实例1简单模拟实现了数据库的连接池,但使用者获取连接时,如果连接池中没有连接时,wait 400毫秒,再次获取,如果获取不到直接报错。
修改实例1的Pool.java类(如果连接池中没有连接时,wait 400毫秒,再次获取,如果再获取不到,再wait 400毫秒……直到共wait 2分钟[当然,具体共wait多少分钟,可以具体修改]),如下实例2所示:
实例2:
package com.bijian.thread; import java.util.ArrayList; import java.util.List; public class Pool { private static final int POOL_SIZE = 10; private List<Connection> connections = new ArrayList<Connection>(); public Pool() { for(int i = 0; i < POOL_SIZE; i++) { connections.add(DB.getInstance().getConnection()); } } public synchronized Connection getConnection() throws Exception { Connection conn = null; for(int i=0;i<60*1000*2;i++) { if(connections.size() > 0) { conn = connections.get(0); connections.remove(0); break; }else { wait(400); i+=400; } } if(conn == null) { throw new Exception("Connection Pool is empty!"); } return conn; } public synchronized void back(Connection conn) { connections.add(conn); notify(); } }
运行结果(对于此模拟实例,获取连接时共wait 2分钟,所有的连接应该都能正常获取到连接,不会抛出Connection Pool is empty!的异常):
(MAX)DB Connection count = 1 (MAX)DB Connection count = 2 (MAX)DB Connection count = 3 (MAX)DB Connection count = 4 (MAX)DB Connection count = 5 (MAX)DB Connection count = 6 (MAX)DB Connection count = 7 (MAX)DB Connection count = 8 (MAX)DB Connection count = 9 (MAX)DB Connection count = 10 Guest0 executed. Guest46 executed. Guest1 executed. Guest79 executed. Guest88 executed. Guest3 executed. Guest37 executed. Guest92 executed. Guest32 executed. Guest85 executed. Guest74 executed. Guest55 executed. Guest14 executed. Guest96 executed. Guest52 executed. Guest95 executed. Guest81 executed. Guest27 executed. Guest80 executed. Guest84 executed. Guest25 executed. Guest44 executed. Guest45 executed. Guest51 executed. Guest50 executed. Guest24 executed. Guest63 executed. Guest7 executed. Guest41 executed. Guest23 executed. Guest60 executed. Guest94 executed. Guest58 executed. Guest59 executed. Guest54 executed. Guest72 executed. Guest56 executed. Guest53 executed. Guest73 executed. Guest69 executed. Guest71 executed. Guest89 executed. Guest70 executed. Guest91 executed. Guest65 executed. Guest93 executed. Guest68 executed. Guest67 executed. Guest90 executed. Guest66 executed. Guest64 executed. Guest34 executed. Guest36 executed. Guest33 executed. Guest35 executed. Guest78 executed. Guest83 executed. Guest86 executed. Guest82 executed. Guest28 executed. Guest87 executed. Guest20 executed. Guest19 executed. Guest77 executed. Guest22 executed. Guest29 executed. Guest26 executed. Guest30 executed. Guest2 executed. Guest61 executed. Guest62 executed. Guest10 executed. Guest47 executed. Guest9 executed. Guest8 executed. Guest57 executed. Guest16 executed. Guest17 executed. Guest76 executed. Guest75 executed. Guest6 executed. Guest4 executed. Guest5 executed. Guest43 executed. Guest21 executed. Guest18 executed. Guest42 executed. Guest15 executed. Guest49 executed. Guest13 executed. Guest12 executed. Guest48 executed. Guest11 executed. Guest98 executed. Guest39 executed. Guest40 executed. Guest97 executed. Guest99 executed. Guest31 executed. Guest38 executed.
相关推荐
java练习之模拟数据库连接池,对数据库连接池有个初步的认识!
本示例包含的代码文件`ConnectionPool.cs`、`TestConnectionPool.cs`和`PooledConnection.cs`提供了自定义数据库连接池的实现。 首先,我们来看`ConnectionPool.cs`。这个文件通常会包含一个类,比如`...
用java实现的简易的数据库连接池及其管理 课程设计 编写一关于Microsoft Access数据库连接池及其管理的相关类,要求能从XML配置文件中读取该数据库驱动程序,数据库名,连接池最大连接数,最多等待用户数,查询...
在Java中,数据库连接池通常通过第三方库如Apache的DBCP、C3P0或HikariCP来实现。然而,为了学习目的,我们可以自己模拟一个简单的连接池。这个项目的核心概念包括: 1. **连接创建与管理**:在应用启动时,连接池...
在VC++(Visual C++)环境中,实现数据库连接池可以帮助提高应用的效率,减少系统资源的消耗,并改善整体的并发处理能力。下面将详细讨论VC++实现数据库连接池的相关知识点。 首先,了解数据库连接池的基本概念。...
- 常见的数据库连接池实现有HikariCP、C3P0、Apache DBCP等,它们提供连接复用,减少系统资源消耗,提高系统性能。 - 使用连接池可以显著提升系统效率,尤其是在高并发环境下,减少由于频繁创建和关闭连接导致的...
46.请写一个java程序实现数据库连接池功能? 用一个自己的类实现Connection接口太麻烦! 主要是连接(Connection)的close()方法,从连接池得到的是一个代理对象,过滤Connection的close()方法。你还能找到其他方法...
在本问题中,我们探讨了基于字符串标识和计数的数据库连接池模拟。输入数据的格式为多组数据,每组数据的第一行是一个正整数n,表示接下来会有n个请求。每个请求包含一个字母id(A到Z)和一个操作字符串,可能是...
Java连接池是应用程序管理数据库连接的一种机制,它通过复用已存在的数据库连接,避免了频繁创建和销毁连接带来的性能开销。在Java中,连接池是实现高效数据库操作的重要手段,尤其是在高并发环境下。本篇文章将深入...
DELPHI7编写的ADO连接池是数据库应用开发中的一个重要技术,它主要用于管理和优化数据库连接,提高系统性能并防止可能出现的死锁问题。在多用户同时访问SQL数据库时,如果没有有效的连接管理,可能会出现资源竞争,...
Java-JDBC【源码】数据库连接池技术、常规操作与Durid对比、测试(20-200)不同连接数下,10万记录,1万并发,CPU磁盘占用情况 1.数据库连接池 2.Durid介绍(官方文档) 3.编码,问题,常规并发操作 3.1.JDBCUtil....
本文将深入探讨Delphi环境下实现的数据库连接池性能测试程序,结合提供的源码,帮助开发者理解如何有效地利用数据库连接池提高应用程序的性能。 首先,Delphi是一种强大的对象 Pascal 编程语言,广泛应用于开发...
通过上述操作,我们可以实现在单一物理机器上模拟多服务器环境,同时通过数据库连接池管理数据库连接,提高系统的并发处理能力和资源效率。然而,实际生产环境中,为了提高可用性和容错性,通常会采用多台物理服务器...
此外,数据库连接池的大小也需要根据服务器资源和应用需求进行适当调整。 综上所述,这个Node.js模块提供了完整的MySQL数据库操作解决方案,结合连接池和大量测试数据,不仅方便开发者快速构建数据库相关功能,还为...
本教程将通过一个简单的Java实现来探讨数据库连接池的工作原理和基本功能。 首先,我们需要理解数据库连接池的核心思想。连接池维护了一个预创建的数据库连接集合,当应用程序需要连接时,它不会每次都新建,而是从...
数据库连接池,即Database Connection Pool,是在应用服务器中维护的一组数据库连接。这些连接可以被多个客户端请求共享,避免了每次请求时创建和关闭连接的开销,显著提升了系统效率。在Java中,常见的连接池实现有...
通过以上知识的学习和实践,开发者可以熟练地在Java环境中实现多线程数据互导,高效使用数据库连接池,以及进行多表插入操作,提升系统的并发处理能力和性能。在实际工作中,还需要根据具体需求和场景,灵活运用这些...
数据库连接池的核心实现,它维护一个连接列表,负责连接的分配、回收和管理。包括连接的初始化、最大连接数设置、空闲连接检测等功能。当连接池中的连接被耗尽时,可能会有新的连接按需创建。 6. **Mysql...
数据库连接池通过预先创建并维护一定数量的数据库连接,实现了对数据库连接的统一管理和复用,解决了这一问题。当一个数据库操作完成,连接不立即关闭,而是返回连接池,供后续请求再次使用,从而避免了频繁的连接...