(java)数据库连接池
2009-01-01 14:24
113 查看
************************配置文件****************************
<?xml version="1.0" encoding="gb2312"?>
<driver xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<userdriver name="华工达梦数据库系统">
<driverclass>dm.jdbc.driver.DmDriver</driverclass>
<url>jdbc:dm://localhost:12345/guest</url>
<username>SYSDBA</username>
<password>123456</password>
<maxconnection>9</maxconnection>
<minconnection>5</minconnection>
</userdriver>
<!-- this program copyright by mengdejun -->
</driver>
************************************************************
Connectionpool.java
author:孟德军
************************************************************
package com.mdj.connectionpool;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Connectionpool {
private String driverclass = "com.mysql.jdbc.Driver";// 数据库驱动类
private String username = "root";// 数据库用户名
private String password = "123456";// 数据库密码
private String url = "jdbc:mysql://localhost:3306/guest";// url
private int maxconnection = 10;// 连接池的最大容量
private int minconnection = 5;// 连接池的初始容量
private ArrayList pool = null;// 用于放连接池的容器
private Connection con = null;//
private static String path = "";
private static Connectionpool instance = null;
// 将构造方法私有法,禁止外部通过new语句创建实例.private修饰的构造方法不能被实例化,而只能通过一个方法来返回他的实例.
private Connectionpool() {
init();
addConnection();
}
private void init() {
readconfig();
pool = new ArrayList(maxconnection);
}
// 创建一个外部实例,通过getinstance方法创建实例.
public synchronized static Connectionpool getinstance() {
if (instance == null) {
instance = new Connectionpool();
}
return instance;
}
// 获取连接池中的连接.
public synchronized Connection getconnection() {
if (pool.size() > 0) {
con = (Connection) pool.get(0);
pool.remove(0);
return con;
} else {
return null;
}
}
public synchronized void releaseconnection(Connection con) {
pool.add(con);
}
public synchronized void closeconnectionpool(Connection con) {
for (int i = 0; i < pool.size(); i++) {
try {
((Connection) pool.get(i)).close();
pool.remove(i);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 向连接池中添加初始连接.
private void addConnection() {
for (int i = 0; i < minconnection; i++) {
try {
Class.forName(driverclass);
con = DriverManager.getConnection(url, username, password);
pool.add(con);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 读取配置文件.
private void readconfig() {
path=System.getProperty("user.dir")+ "//sysconfig.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(path);
NodeList nodelist = document.getElementsByTagName("userdriver");
for (int i = 0; i < nodelist.getLength(); i++) {
Element element = (Element) nodelist.item(i);
driverclass = element.getElementsByTagName("driverclass").item(
0).getFirstChild().getNodeValue();
url = element.getElementsByTagName("url").item(0)
.getFirstChild().getNodeValue();
username = element.getElementsByTagName("username").item(0)
.getFirstChild().getNodeValue();
password = element.getElementsByTagName("password").item(0)
.getFirstChild().getNodeValue();
maxconnection = Integer
.parseInt(element.getElementsByTagName("maxconnection")
.item(0).getFirstChild().getNodeValue());
minconnection = Integer
.parseInt(element.getElementsByTagName("minconnection")
.item(0).getFirstChild().getNodeValue());
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
************************************************************
ConnectionpoolTest.java
author:孟德军
************************************************************
package com.mdj.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mdj.pool.ConnectionPool;
public class Test {
public static void main(String[] args) {
ConnectionPool pool;
Connection con;
long start = System.currentTimeMillis();
String sql="select * from userinfo";
for (int i = 0; i < 100; i++) {
pool = ConnectionPool.getinstance();
con = pool.getconnection();
try {
Statement statement = con.createStatement();
ResultSet result = statement
.executeQuery(sql);
while (result.next()) {
}
result.close();
statement.close();
pool.release(con);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}System.out.println("连接池连接");
System.out.println(System.currentTimeMillis() - start+"ms");
long start1=System.currentTimeMillis();
for(int i=0;i<100;i++){
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con1=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
Statement stm1=con1.createStatement();
ResultSet result=stm1.executeQuery(sql);
while(result.next()){
}
result.close();
stm1.close();
con1.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e){
e.printStackTrace();
}
}
System.out.println("普通连接");
System.out.println(System.currentTimeMillis()-start1+"ms");
}
}
当连接池连接和普通连接执行一百次时,结果如下:
连接池连接:1723ms
普通连接:19041ms
*******************************************************************************************
效果很明显,这仅仅是我写的一个连接池,诸多因素没有考虑,你可以到网上下载连接池产品.poolman等等.
其实国产数据库系统还是蛮好的,支持国产!
<?xml version="1.0" encoding="gb2312"?>
<driver xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<userdriver name="华工达梦数据库系统">
<driverclass>dm.jdbc.driver.DmDriver</driverclass>
<url>jdbc:dm://localhost:12345/guest</url>
<username>SYSDBA</username>
<password>123456</password>
<maxconnection>9</maxconnection>
<minconnection>5</minconnection>
</userdriver>
<!-- this program copyright by mengdejun -->
</driver>
************************************************************
Connectionpool.java
author:孟德军
************************************************************
package com.mdj.connectionpool;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Connectionpool {
private String driverclass = "com.mysql.jdbc.Driver";// 数据库驱动类
private String username = "root";// 数据库用户名
private String password = "123456";// 数据库密码
private String url = "jdbc:mysql://localhost:3306/guest";// url
private int maxconnection = 10;// 连接池的最大容量
private int minconnection = 5;// 连接池的初始容量
private ArrayList pool = null;// 用于放连接池的容器
private Connection con = null;//
private static String path = "";
private static Connectionpool instance = null;
// 将构造方法私有法,禁止外部通过new语句创建实例.private修饰的构造方法不能被实例化,而只能通过一个方法来返回他的实例.
private Connectionpool() {
init();
addConnection();
}
private void init() {
readconfig();
pool = new ArrayList(maxconnection);
}
// 创建一个外部实例,通过getinstance方法创建实例.
public synchronized static Connectionpool getinstance() {
if (instance == null) {
instance = new Connectionpool();
}
return instance;
}
// 获取连接池中的连接.
public synchronized Connection getconnection() {
if (pool.size() > 0) {
con = (Connection) pool.get(0);
pool.remove(0);
return con;
} else {
return null;
}
}
public synchronized void releaseconnection(Connection con) {
pool.add(con);
}
public synchronized void closeconnectionpool(Connection con) {
for (int i = 0; i < pool.size(); i++) {
try {
((Connection) pool.get(i)).close();
pool.remove(i);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 向连接池中添加初始连接.
private void addConnection() {
for (int i = 0; i < minconnection; i++) {
try {
Class.forName(driverclass);
con = DriverManager.getConnection(url, username, password);
pool.add(con);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 读取配置文件.
private void readconfig() {
path=System.getProperty("user.dir")+ "//sysconfig.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(path);
NodeList nodelist = document.getElementsByTagName("userdriver");
for (int i = 0; i < nodelist.getLength(); i++) {
Element element = (Element) nodelist.item(i);
driverclass = element.getElementsByTagName("driverclass").item(
0).getFirstChild().getNodeValue();
url = element.getElementsByTagName("url").item(0)
.getFirstChild().getNodeValue();
username = element.getElementsByTagName("username").item(0)
.getFirstChild().getNodeValue();
password = element.getElementsByTagName("password").item(0)
.getFirstChild().getNodeValue();
maxconnection = Integer
.parseInt(element.getElementsByTagName("maxconnection")
.item(0).getFirstChild().getNodeValue());
minconnection = Integer
.parseInt(element.getElementsByTagName("minconnection")
.item(0).getFirstChild().getNodeValue());
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
************************************************************
ConnectionpoolTest.java
author:孟德军
************************************************************
package com.mdj.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mdj.pool.ConnectionPool;
public class Test {
public static void main(String[] args) {
ConnectionPool pool;
Connection con;
long start = System.currentTimeMillis();
String sql="select * from userinfo";
for (int i = 0; i < 100; i++) {
pool = ConnectionPool.getinstance();
con = pool.getconnection();
try {
Statement statement = con.createStatement();
ResultSet result = statement
.executeQuery(sql);
while (result.next()) {
}
result.close();
statement.close();
pool.release(con);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}System.out.println("连接池连接");
System.out.println(System.currentTimeMillis() - start+"ms");
long start1=System.currentTimeMillis();
for(int i=0;i<100;i++){
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con1=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
Statement stm1=con1.createStatement();
ResultSet result=stm1.executeQuery(sql);
while(result.next()){
}
result.close();
stm1.close();
con1.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e){
e.printStackTrace();
}
}
System.out.println("普通连接");
System.out.println(System.currentTimeMillis()-start1+"ms");
}
}
当连接池连接和普通连接执行一百次时,结果如下:
连接池连接:1723ms
普通连接:19041ms
*******************************************************************************************
效果很明显,这仅仅是我写的一个连接池,诸多因素没有考虑,你可以到网上下载连接池产品.poolman等等.
其实国产数据库系统还是蛮好的,支持国产!
相关文章推荐
- 使用网上流传的一个数据库连接池在Proxy.newProxyInstance处引起 java.lang.ClassCastException 问题的解决方法
- Java jdbc数据库连接池总结
- java_web学习第十一天(jdbc数据库驱动------数据库连接池)
- 数据库连接池java实现小结
- C3P0数据库连接池【JAVA】-oracle数据库为例
- 使用 JAVA 中的动态代理实现数据库连接池
- java配置dbcp连接池(数据库连接池)示例
- Java 数据库连接池
- Java语言链接数据库连接池配置的两种技巧
- java 数据库连接池配置方式
- Java中的BoneCP数据库连接池
- 数据库连接池java实现小结
- java:自定义数据库连接池 推荐
- 使用 JAVA 中的动态代理实现数据库连接池
- 使用JAVA中的动态代理实现数据库连接池
- 转 java Tomcat数据库连接池
- Java jdbc数据库连接池
- Java实现数据库连接池例子
- 使用JAVA中的动态代理实现数据库连接池
- Java中的BoneCP数据库连接池用法