java c3po_17、C3PO纯java使用

2、代码使用 package com.study;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.beans.PropertyVetoException;

import java.lang.reflect.Field;

import java.sql.*;

/**

* C3PO 纯java使用,不需要借助其他框架

* @CreateTime 2017/11/3 9:16

*/

public class C3P0Manager {

private static final Logger LOGGER = LoggerFactory.getLogger(C3P0Manager.class);

private static C3P0Manager inStance;

private ComboPooledDataSource dataSource;

private C3P0Manager(){}

private C3P0Manager(String driverClass, String jdbcUrl, String user, String passwd, int poolSize) {

dataSource = new ComboPooledDataSource();

try {

dataSource.setDriverClass(driverClass);

} catch (PropertyVetoException e) {

LOGGER.error("C3PO配置驱动类时,报错:{}", e);

}

dataSource.setJdbcUrl(jdbcUrl);

dataSource.setUser(user);

dataSource.setPassword(passwd);

dataSource.setAcquireIncrement(5);

dataSource.setInitialPoolSize(poolSize);

dataSource.setMaxPoolSize(poolSize + 10);

}

/**

* 单例模式

* [@param](https://my.oschina.net/u/2303379) driverClass

* [@param](https://my.oschina.net/u/2303379) jdbcUrl

* [@param](https://my.oschina.net/u/2303379) user

* [@param](https://my.oschina.net/u/2303379) passwd

* @param poolSize

* @return

*/

public static C3P0Manager getInstance(String driverClass, String jdbcUrl, String user, String passwd, int poolSize) {//完成初始化

if (inStance == null) {

synchronized ("getC3P0Manager") {

if (inStance == null) {

inStance = new C3P0Manager(driverClass, jdbcUrl, user, passwd, poolSize);

}

}

}

return inStance;

}

/**

* 同步获取一个连接

* @return

*/

public synchronized Connection getConnection() {

try {

return dataSource.getConnection();

} catch (SQLException e) {

LOGGER.error("C3PO获取connection报错:{}", e);

}

return null;

}

/**

* 同步返回一个连接

* @param connection

* @return

*/

public synchronized boolean returnConnection(Connection connection) {

try {

connection.close();

} catch (SQLException e) {

LOGGER.error("C3PO关闭connection报错:{}", e);

}

return true;

}

/**

* 释放Statement 和 ResultSet

* @param st

* @param rs

*/

public void closeStatement(Statement st, ResultSet rs) {

try {

rs.close();

st.close();

} catch (Exception e) {

LOGGER.error("C3PO关闭Statement 或者 resultSet报错:{}", e);

}

}

public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {

String driverClass = "com.mysql.jdbc.Driver";

String jdbcUrl = "jdbc:mysql://192.168.50.11:3306/caiwutong?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true";

String userName = "root";

String passwd = "123456";

int poolSize = 10;

C3P0Manager instance = C3P0Manager.getInstance(driverClass, jdbcUrl, userName, passwd, poolSize);

Connection connection = null;

// 1、循环获取connection,拿一个放回去一个,判断是否是同一个 ==》反复都是使用前几个相同的

for (int i = 0; i < 10000; i++) {

connection = instance.getConnection();

Field inner = connection.getClass().getDeclaredField("inner");

inner.setAccessible(true);

System.out.println(inner.get(connection));

instance.returnConnection(connection); //只需要connection.close(),c3p0就会把它回收,而不是关闭

}

// 2、循环获取connection,没有放回去,判断是否是同一个 ==》每次都不一样,同时达到20个上限时会被阻塞

for (int i = 0; i < 10000; i++) {

connection = instance.getConnection();

Field inner = connection.getClass().getDeclaredField("inner");

inner.setAccessible(true);

System.out.println(inner.get(connection));

}

connection = instance.getConnection();

try {

PreparedStatement preparedStatement = connection.prepareStatement("select * from user_info");

ResultSet resultSet = preparedStatement.executeQuery();

while (resultSet.next()) {

System.out.println(resultSet.getString("id"));

}

//关闭连接statement he resultSet , 同时将connection放回到连接池中

instance.closeStatement(preparedStatement, resultSet);

instance.returnConnection(connection);

} catch (SQLException e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值