关于 java 连接池的使用 一个pc 软件,java 写的,访问mysql 数据库,速度很慢。请问怎么解决? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 什么样的软件,b/s or c/s?你问题太泛了 package com.itcast.proxy.pool;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.LinkedList;public class DataSource2 { private static LinkedList<Connection> connectionPool=new LinkedList<Connection>(); private static String url = "jdbc:mysql://localhost:3306/jdbc"; private static String user = "root"; private static String password = "root"; private static String driverName = "com.mysql.jdbc.Driver"; private static int initSize=2; private static int maxSize=5; int currentSize=0; static{ try { Class.forName(driverName); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public DataSource2(){ for(int i=0;i< initSize ;i++){ connectionPool.addLast(createConnection()); currentSize++; } } public Connection getConnection() throws Exception{ synchronized (connectionPool) { if(connectionPool.size() > 0 ){ return connectionPool.removeFirst(); } if(currentSize < maxSize){ currentSize++; return createConnection(); } throw new Exception("达到最大连接限制"); } } public void replaceConnection(Connection conn){ if(conn!=null) connectionPool.addLast(conn); } private Connection createConnection(){ try { Connection conn=DriverManager.getConnection(url, user, password); MyConnectionHandler mHandler=new MyConnectionHandler(this); return mHandler.bind(conn); } catch (SQLException e) { throw new ExceptionInInitializerError(e); } }} package com.itcast.proxy.pool;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.sql.Connection;public class MyConnectionHandler implements InvocationHandler { private Connection realConn; private Connection warpedConn; private DataSource2 datasource; private int maxCount = 5; private int currentSize = 0; public MyConnectionHandler(DataSource2 datasource) { this.datasource=datasource; } //产生一个代理 Connection bind(Connection realConn) { this.realConn=realConn; this.warpedConn = (Connection) Proxy.newProxyInstance(this .getClass().getClassLoader(), new Class[] { Connection.class }, this); return this.warpedConn; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if("close".equals(method.getName())){ currentSize++; if (currentSize <= maxCount) { datasource.replaceConnection(this.warpedConn); System.out.println("回收"); } else { this.realConn.close(); datasource.currentSize--; } } return method.invoke(this.realConn, args); }} 如何区分没有session和session过期 编译失败 与 运行时抛出异常 小菜问题--关于向数据库存储日期时间类型数据 一个简单程序出错了 如何用socket来获取网页的内容(包括网页上的图片)? 关于把HTML文档里面反复出现的相同格式的单元-格内容写入新文件的问题。 在windows中怎样关闭某个端口,不是编程,只是操作 谁帮我解释解释? freebird 领分~~先给50,以后多帮忙,分没问题! 用getClass().getResource("Image.jpg")不能的到"Image.jpg"这张图片怎么办?? sql嵌套查询 问00:00:00时间格式问题
你问题太泛了
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;public class DataSource2 {
private static LinkedList<Connection> connectionPool=new LinkedList<Connection>();
private static String url = "jdbc:mysql://localhost:3306/jdbc";
private static String user = "root";
private static String password = "root";
private static String driverName = "com.mysql.jdbc.Driver";
private static int initSize=2;
private static int maxSize=5;
int currentSize=0;
static{
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public DataSource2(){
for(int i=0;i< initSize ;i++){
connectionPool.addLast(createConnection());
currentSize++;
}
}
public Connection getConnection() throws Exception{
synchronized (connectionPool) {
if(connectionPool.size() > 0 ){
return connectionPool.removeFirst();
}
if(currentSize < maxSize){
currentSize++;
return createConnection();
}
throw new Exception("达到最大连接限制");
}
}
public void replaceConnection(Connection conn){
if(conn!=null)
connectionPool.addLast(conn);
}
private Connection createConnection(){
try {
Connection conn=DriverManager.getConnection(url, user, password);
MyConnectionHandler mHandler=new MyConnectionHandler(this);
return mHandler.bind(conn); } catch (SQLException e) {
throw new ExceptionInInitializerError(e);
}
}
}
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;public class MyConnectionHandler implements InvocationHandler {
private Connection realConn;
private Connection warpedConn;
private DataSource2 datasource;
private int maxCount = 5;
private int currentSize = 0;
public MyConnectionHandler(DataSource2 datasource) {
this.datasource=datasource;
} //产生一个代理
Connection bind(Connection realConn) {
this.realConn=realConn;
this.warpedConn = (Connection) Proxy.newProxyInstance(this
.getClass().getClassLoader(), new Class[] { Connection.class },
this);
return this.warpedConn;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if("close".equals(method.getName())){
currentSize++; if (currentSize <= maxCount) {
datasource.replaceConnection(this.warpedConn);
System.out.println("回收");
} else {
this.realConn.close();
datasource.currentSize--;
}
}
return method.invoke(this.realConn, args);
}}