栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

手写数据库连接池,让抽象工厂不再抽象

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

手写数据库连接池,让抽象工厂不再抽象

接下来创建Java产品族,Java视频JavaVideo类的代码如下。

public class JavaVideo implements IVideo {

public void record() {

System.out.println(“录制Java视频”);

}

}

扩展产品等级Java课堂笔记JavaNote类。

public class JavaNote implements INote {

public void edit() {

System.out.println(“编写Java笔记”);

}

}

创建Java产品族的具体工厂JavaCourseFactory。

public class JavaCourseFactory extends CourseFactory {

public INote createNote() {

super.init();

return new JavaNote();

}

public IVideo createVideo() {

super.init();

return new JavaVideo();

}

}

随后创建Python产品族,Python视频PythonVideo类的代码如下。

public class PythonVideo implements IVideo {

public void record() {

System.out.println(“录制Python视频”);

}

}

扩展产品等级Python课堂笔记PythonNote类。

public class PythonNote implements INote {

public void edit() {

System.out.println(“编写Python笔记”);

}

}

创建Python产品族的具体工厂PythonCourseFactory。

public class PythonCourseFactory implements CourseFactory {

public INote createNote() {

return new PythonNote();

}

public IVideo createVideo() {

return new PythonVideo();

}

}

最后来看客户端调用代码。

public static void main(String[] args) {

JavaCourseFactory factory = new JavaCourseFactory();

factory.createNote().edit();

factory.createVideo().record();

}

上面代码完整地描述了Java课程和Python课程两个产品族,也描述了视频和笔记两个产品等级。抽象工厂非常完美、清晰地描述了这样一层复杂的关系。但是,不知道大家有没有发现,如果再继续扩展 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 产品等级,将源码Source也加入课程中,则代码从抽象工厂到具体工厂要全部调整,这显然不符合开闭原则。

[](()4 使用抽象工厂模式重构数据库连接池


还是演示课堂开始的JDBC操作案例,我们每次操作都需要重新创建数据库连接。其实每次创建都非常耗费性能,消耗业务调用时间。我们使用抽象工厂模式,将数据库连接预先创建好,放到容器中缓存着,当业务调用时就只需现取现用。我们来看代码。

Pool抽象类的代码如下。

public abstract class Pool {

public String propertiesName = “connection-INF.properties”;

private static Pool instance = null; //定义唯一实例

protected int maxConnect = 100; //最大连接数

protected int normalConnect = 10; //保持连接数

protected String driverName = null; //驱动字符串

protected Driver driver = null; //驱动变量

protected Pool() {

try

{

init();

loadDrivers(driverName);

}catch(Exception e)

{

e.printStackTrace();

}

}

private void init() throws IOException {

InputStream is = Pool.class.getResourceAsStream(propertiesName);

Properties p = new Properties();

p.load(is);

this.driverName = p.getProperty(“driverName”);

this.maxConnect = Integer.parseInt(p.getProperty(“maxConnect”));

this.normalConnect = Integer.parseInt(p.getProperty(“normalConnect”));

}

protected void loadDrivers(String dri) {

String driverClassName = dri;

try {

driver = (Driver) Class.forName(driverClassName).newInstance();

DriverManager.registerDriver(driver);

System.out.println(“成功注册JDBC驱动程序” + driverClassName);

} catch (Exception e) {

System.out.println(“无法注册JDBC驱动程序:” + driverClassName + “,错误:” + e);

}

}

public abstract void createPool();

public static synchronized Pool getInstance() throws IOException,

InstantiationException, IllegalAccessException,

ClassNotFoundException {

if (instance == null) {

instance = (Pool) Class.forName(“org.e_book.sqlhelp.Pool”).newInstance();

}

return instance;

}

public abstract Connection getConnection();

public abstract Connection getConnection(long time);

public abstract void freeConnection(Connection con);

public abstract int getnum();

public abstract int getnumActive();

protected synchronized void release() {

//撤销驱动

try {

DriverManager.deregisterDriver(driver);

System.out.println("撤销JDBC驱动程序 " + driver.getClass().getName());

} catch (SQLException e) {

System.out

.println(“无法撤销JDBC驱动程序的注册:” + driver.getClass().getName());

}

}

}

DBConnectionPool数据库连接池的代码如下。

public final class DBConnectionPool extends Pool {

private int checkedOut; //正在使用的连接数

private Vector freeConnections = new Vector();

//存放产生的连接对象容器

private String passWord = null; //密码

private String url = null; //连接字符串

private String userName = null; //用户名

private static int num = 0; //空闲连接数

private static int numActive = 0; //当前可用的连接数

private static DBConnectionPool pool = null; //连接池实例变量

public static synchronized DBConnectionPool getInstance()

{

if(pool == null)

{

pool = new DBConnectionPool();

}

return pool;

}

private DBConnectionPool() {

try

{

init();

for (int i = 0; i < normalConnect; i++) { //初始normalConn个连接

Connection c = newConnection();

if (c != null) {

freeConnections.addElement©; //往容器中添加一个连接对象

num++; //记录总连接数

}

}

}catch(Exception e)

{

e.printStackTrace();

}

}

private void init() throws IOException

{

InputStream is = DBConnectionPool.class.getResourceAsStream(propertiesName);

Properties p = new Properties();

p.load(is);

this.userName = p.getProperty(“userName”);

this.passWord = p.getProperty(“passWord”);

this.driverName = p.getProperty(“driverName”);

this.url = p.getProperty(“url”);

this.driverName = p.getProperty(“driverName”);

this.maxConnect = Integer.parseInt(p.getProperty(“maxConnect”));

this.normalConnect = Integer.parseInt(p.getProperty(“normalConnect”));

}

public synchronized void freeConnection(Connection con) {

freeConnections.addElement(con);

num++;

checkedOut–;

numActive–;

notifyAll(); //解锁

}

private Connection newConnection() {

Connection con = null;

try {

if (userName == null) { //用户、密码都为空

con = DriverManager.getConnection(url);

} else {

con = DriverManager.getConnection(url, userName, passWord);

}

System.out.println(“连接池创建一个新的连接”);

} catch (SQLException e) {

System.out.println(“无法创建这个URL的连接” + url);

return null;

}

return con;

}

public int getnum() {

return num;

}

public int getnumActive() {

return numActive;

}

public synchronized Connection getConnection() {

Connection con = null;

if (freeConnections.size() > 0) { //还有空闲的连接

num–;

con = (Connection) freeConnections.firstElement();

freeConnections.removeElementAt(0);

try {

if (con.isClosed()) {

System.out.println(“从连接池删除一个无效连接”);

con = getConnection();

}

} catch (SQLException e) {

System.out.println(“从连接池删除一个无效连接”);

con = getConnection();

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/860772.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号