数据连接池
来源:互联网 发布:假人秒进群qq数据 编辑:程序博客网 时间:2024/05/18 19:37
DataSourse
数据连接池的目的,意义
数据连接池的基本实现
1、 实现途径:
编写JdbcPool implements java.sql.DataSource类
l 静态初始化块
static{
try {
InputStream in = DBManager.class.getClassLoader().getResourceAsStream("db.properties");
Properties prop = new Properties();
prop.load(in);
driver = prop.getProperty("driver");
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
Class.forName(driver);
list = new LinkedList<Connection>();
for(int i=0;i<10;i++){
Connection conn = DriverManager.getConnection(url,user,password);
MyConnection myconn = new MyConnection(conn,list);
System.out.println("连接池中增加了一个连接对象:"+conn);
list.add(myconn);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
l getConnection()
@Override
public Connection getConnection() throws SQLException {
if(list.size()>0){
Connection conn = list.remove();
System.out.println("用户取走了一个连接"+conn);
return conn;
}
return null;
}
l release()
public void release(Connection conn) {
list.add(conn);
System.out.println("用户还回了一个连接:"+conn);
}
装饰模式解决方案
用包装设计模式对connnction的close方法进行增强
1.写一个类实现与被增强对象相同的接口
2.在类中定义一个变量记住被增强对象
3.在类中定义一个构造函数,接收被增强对象
4.覆盖想增强的方法
5.对于不想增强的方法,直接调用目标对象(被增强对象)的方法
1) 动态代理技术实现
public Connection getConnection() throws SQLException {
if(list.size()>0){
final Connection conn = list.removeFirst(); //mysql
System.out.println("用户从池中拿走了:" + conn);
System.out.println("池的大小为" + list.size());
//myconnection preparedstatement commit close
return (Connection)Proxy.newProxyInstance(JdbcPool.class.getClassLoader(), conn.getClass().getInterfaces(), new InvocationHandler(){
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if(!method.getName().equalsIgnoreCase("close")){
return method.invoke(conn, args);
}
System.out.println(conn + "被还到池中了");
list.add(conn);
System.out.println("池的大小为" + list.size());
return null;
}
});
/* MyConnection myconn = new MyConnection(conn,list);
return myconn; // conn = pool.getConnection(); conn.preparedstatment conn
*/ }else{
throw new RuntimeException("对不起,数据库忙,请等会再来!!");
}
}
DBCP
DBCP(DataBase connection pool),数据库连接池。是 apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件。单独使用dbcp需要3个包:
common-dbcp.jar,
common-pool.jar
,common-collections.jar
操作步骤
1、赋值jar包及dbconfig.properties文件
2、修改配置文件
3、 修改DBManager文件
a) 静态初始化块,加载配置文件
b) DataSource ds =
BasicDataSourceFactory.createDataSource(prop);
c) 修改getConnection()
public static Connection getConnection() throws SQLException{
return ds.getConnection();
}
3、在数据库操作中任然使用DBManager的getConnection和release方法即可
C3P0
1.复制jar文件
c3p0-0.9.2-pre1.jar
mchange-commons-0.2.jar
1、 创建DBManager_c3p0
ds = new ComboPooledDataSource();
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
ds.setUser("root");
ds.setPassword("admin");
ds.setInitialPoolSize(20);
ds.setMaxPoolSize(40);
ds.setMinPoolSize(10);
2、 测试代码
解决c3p0配置问题
1、 在src目录下创建c3p0-config.xml文件,在c3p0文档中找到配置文件案例代码复制到xml中,格式化代码。
2、 修改xml文件,添加driverClass, jdbcUrl, user, password四个属性如下
<c3p0-config>
<default-config>
<property name="automaticTestTable">con_test</property>
<property name="checkoutTimeout">30000</property>
<property name="idleConnectionTestPeriod">30</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>
<named-config name="mysql">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password">admin</property>
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">5</property>
<property name="maxPoolSize">30</property>
</named-config>
</c3p0-config>
3、 修改DBManager_c3p0
try{
ds = new ComboPooledDataSource("mysql");
}catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
3、 测试代码
c3p0与dbcp区别
dbcp没有自动的去回收空闲连接的功能
c3po有自动回收空闲连接功能
- Tomat 连接数据连接池
- 配置数据连接池
- weblogic数据连接池
- Jndi数据连接池
- 数据连接池配置
- 数据连接池
- 数据连接池
- 数据连接池配置文件
- 数据连接池
- 数据连接池
- 数据连接池
- 数据连接池+图解
- 数据连接池
- proxool数据连接池
- Java数据连接池
- 自定义数据连接池
- 数据连接池JNDI
- 数据连接池
- Delphi 中自定义异常及异常处理的一般方法
- linux文件系统的目录结构
- 汇编语言实验1-3
- 微博兴起时,博客这个大家伙就放弃了
- eclipse 下环境变量设置
- 数据连接池
- eclipse导入工程出现 Unbound classpath variable Error .
- thinking in java(VIII)--containers,exceptions,I/O,RTTI
- 减少冗余计算的优化对比
- WIN7意想不到的强大功能
- 项目心得
- 如何进行java系统优化
- OpenVPN关于push-peer-info的实现
- C#之P/Invoke