编写一个基本的连接池来实现连接的复用&一些工程细节上的优化
package it.cast.jdbc; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList; public class MyDataSource { private static String url = "jdbc:mysql://localhost:3306/jdbc";
private static String username = "root";
private static String password = "123"; private static int initCount = 5;
private static int maxCount = 10;
private int currentCount = 0; private LinkedList<Connection> connectionPool = new LinkedList<Connection>(); public MyDataSource() {
try {
for (int i = 0; i < initCount; i++) { this.connectionPool.addLast(this.createConnection()); this.currentCount++;
}
} catch (SQLException e) {
throw new ExceptionInInitializerError(e);
} } public Connection getConnection() throws SQLException {
synchronized (connectionPool) {
if (this.connectionPool.size() > 0) {
return this.connectionPool.removeFirst();
}
if (this.currentCount < maxCount) {
this.currentCount++;
return this.createConnection();
}
throw new SQLException("NO Connection!!");
} } public void free(Connection conn) {
this.connectionPool.addLast(conn);
} private Connection createConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
}
MyDataSource
package it.cast.jdbc; import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class jdbcUtils { private static String url = "jdbc:mysql://localhost:3306/jdbc?generateSimpleParameterMetadata=true";
private static String user = "root";
private static String password = "123"; private static MyDataSource myDataSource = null; private jdbcUtils() { } static {
try {
Class.forName("com.mysql.jdbc.Driver");
myDataSource =new MyDataSource();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} public static Connection getConnection() throws SQLException {
return myDataSource.getConnection();
} public static void free(ResultSet rs, Statement st, Connection conn) { try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally { try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally { try {
if (conn != null)
//conn.close();
myDataSource.free(conn);
} catch (Exception e) {
e.printStackTrace();
}
} } } public static void free(ResultSet rs, PreparedStatement ps, Connection conn) { try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally { try {
if (ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
} finally { try {
if (conn != null)
//conn.close();
myDataSource.free(conn);
} catch (Exception e) {
e.printStackTrace();
}
} } }
public static void free(ResultSet rs, CallableStatement cs, Connection conn) { try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally { try {
if (cs != null)
cs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally { try {
if (conn != null)
//conn.close();
myDataSource.free(conn);
} catch (Exception e) {
e.printStackTrace();
}
} } }
}
jdbcUtils
package it.cast.jdbc; import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class Base { static Connection conn = null; public static void main(String[] args) throws SQLException, ClassNotFoundException {
for (int i = 0; i < 20; i++) {
Connection conn = jdbcUtils.getConnection();
System.out.println(conn);
//jdbcUtils.free(null, null, conn);
}
} static void test() throws SQLException, ClassNotFoundException { // 2.建立连接
conn = jdbcUtils.getConnection(); // 3.创建语句
Statement st = conn.createStatement(); // 4.执行语句
ResultSet rs = st.executeQuery("select * from user"); // 5.处理结果
while (rs.next()) {
System.out.println(rs.getObject(1) + "\t" + rs.getObject(2) + "\t"
+ rs.getObject(3)+"\t" + rs.getObject(4));
} //6.释放资源
jdbcUtils.free(rs, st, conn);
} }
Base
编写一个基本的连接池来实现连接的复用&一些工程细节上的优化的更多相关文章
- 关于 Mybatis的原生连接池 和 DBCP 连接池
一 遇到的问题: 项目用的play框架,数据库DB2, 持久化框架是Mybatis, 连接池用的是Mybatis原生的,遇到的问题是:有时候抛出如下异常: play.api.UnexpectedEx ...
- Redisclient连接方式Hiredis简单封装使用,连接池、屏蔽连接细节
工作须要对Hiredis进行了简单封装,实现功能: 1.API进行统一,对外仅仅提供一个接口. 2.屏蔽上层应用对连接的细节处理: 3.底层採用队列的方式保持连接池,保存连接会话. 4.重连时採用时间 ...
- JDBC秒变C3P0连接池——再加连接解耦
从JDBC连接到C3P0数据库连接池 在Java开发中,使用JDBC操作数据库的四个步骤如下: ①加载数据库驱动程序(Class.forName("数据库驱动类");) ② ...
- Swoole 中使用 PDO 连接池、Redis 连接池、Mysqli 连接池
连接池使用说明 所有连接池的实现均基于 ConnectionPool 原始连接池: 连接池的底层原理是基于 Channel 的自动调度: 开发者需要自己保证归还的连接是可重用的: 若连接不可重用,需要 ...
- JDBC连接池(三)DBCP连接池
JDBC连接池(三)DBCP连接池 在前面的随笔中提到 了 1.JDBC自定义连接池 2. C3P0连接池 今天将介绍DBCP连接池 第一步要导入jar包 (注意:mysql和mysql 驱动 ...
- DBCP连接池与c3p0连接池
1. DBCP连接池
- 采用DBCP连接池技术管理连接
DBCP的使用步骤步骤一:导包,使用第三方的道具,必须导入相应的jar包. 一般需要导入两个jar包: -commons-dbcp-1.x.jar包 -commons-pool-1.x.x.jar包 ...
- JDBC连接池一 自定义连接池
package com.mozq.jdbc; import java.io.IOException; import java.io.InputStream; import java.sql.Conne ...
- getSessionFactory().openSession()导致druid连接池中的连接都占用满但无法回收
该问题产生的现象 页面刷新几次后,就卡住,线上就得需要重新部署(还好是测试环境,不是真正生产环境) 过程及原因 查看日志线程池满了 Caused by: org.springframework.jdb ...
随机推荐
- MongoDB高可用集群配置的方案
>>高可用集群的解决方案 高可用性即HA(High Availability)指的是通过尽量缩短因日常维护操作(计划)和突发的系统崩溃(非计划)所导致的停机时间,以提高系统和应用的可用性. ...
- 多对多关系<EntityFramework6.0>
无负载建立多对多关联的模型 原文中是Modeling a Many-to-Many Relationship with No Payload,虽然这么翻译也有点不准确,但是可以说明其目的,如下图所示, ...
- C#运算符号
double x=5.1e3;// 5.1乘以10 的3次方. x就是 5100 //注 : 5.1e+3=5.1e3=5.1e03=5.1e+03 double y=5.1e-3;// 5.1乘以 ...
- 【学习篇:他山之石,把玉攻】JavaScript Date() 对象 及 格式化
Date 对象用于处理日期和时间. 创建 Date 对象的语法: var myDate=new Date() Date 对象会自动把当前日期和时间保存为其初始值. 参数形式有以下5种: new ...
- Visual Studio (VS IDE) 你必须知道的功能和技巧 - 【.Net必知系列】
前言 本文主要阐述一些Visual Studio开发下需要知道的少部分且比较实用的功能,也是很多人忽略的部分.一些不常用而且冷门的功能不在本文范围,当然本文的尾巴[.Net必知系列]纯属意淫,如有雷同 ...
- URL
URL的格式:protocol :// hostname[:port] / path / [;parameters][?query]#fragment URL出现了有+,空格,/,?,%,#,& ...
- css3选择器
原网站 cnblogs.com/tianshang/p/5982513.html通配符选择器 通配选择器的作用就是对页面上所有的元素都生效, 页面上的所有标签都会展示出通配符选择器设定的样式. 这样的 ...
- html学习第三天—— 第11章 盒子模型 div
盒模型--边框(一) 盒子模型的边框就是围绕着内容及补白的线,这条线你可以设置它的粗细.样式和颜色(边框三个属性). 如下面代码为div来设置边框粗细为2px.样式为实心的.颜色为红色的边框: div ...
- PDA手持机 移动开单进销存系统 现场出打印凭据和扫码 新的亮点
传统车销模式弊端:1.手写开单,效率低,动作慢2.现场手写开单明细不能打印,产品明细不规范3.电脑办公人员及车销人员对车上的库存情况掌握不清楚,销售人员对每种产品销售价格不清楚4.老板对员工工作的管控 ...
- MyEclipse创建Maven工程
先要在MyEclipse中对Maven进行设置: