c3p0的使用

32 篇文章 0 订阅

一: 导c3p0的包

二:导数据库连接包 ,比如我使用的mysql

三:在类路径底线创建文件

     jdbc.properties

    文件内容为:

 

driverClass=org.gjt.mm.mysql.Driver
jdbcUrl=jdbc:mysql://localhost:3306/my_test?useUnicode=true&characterEncoding=UTF-8
user=root
password=123456
#<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
initialPoolSize=3
#<!--连接池中保留的最小连接数。-->
minPoolSize=1
#<!--连接池中保留的最大连接数。Default: 15 -->
maxPoolSize=15
#<!--最大空闲时间,600秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
maxIdleTime=200
#<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
acquireIncrement=2
#<!--每120秒检查所有连接池中的空闲连接。Default: 0 -->
idleConnectionTestPeriod=120

maxConnectionAge=400

 

其他的具体配置看我另一边文章吧

书写工具类:C3p0Source

 

public final class C3p0Source {
 private C3p0Source() {
 }

 private static ComboPooledDataSource dataSource;
 static {
  try {
   dataSource = new ComboPooledDataSource();
   Properties properties = new Properties();
   InputStream is = C3p0Source.class.getClassLoader()
     .getResourceAsStream("jdbc.properties");
   properties.load(is);
//   dataSource.setProperties(properties);
   dataSource.setUser(properties.getProperty("user"));
   dataSource.setPassword(properties.getProperty("password"));
   dataSource.setJdbcUrl(properties.getProperty("jdbcUrl"));
   dataSource.setDriverClass(properties.getProperty("driverClass"));
   
   dataSource.setInitialPoolSize(Integer.parseInt(properties.getProperty("initialPoolSize")));
   dataSource.setMinPoolSize(Integer.parseInt(properties.getProperty("minPoolSize")));
   dataSource.setMaxPoolSize(Integer.parseInt(properties.getProperty("maxPoolSize")));
   dataSource.setMaxIdleTime(Integer.parseInt(properties.getProperty("maxIdleTime")));
   dataSource.setIdleConnectionTestPeriod(Integer.parseInt(properties.getProperty("idleConnectionTestPeriod")));
   dataSource.setMaxConnectionAge(Integer.parseInt(properties.getProperty("maxConnectionAge")));
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public static Connection getConnection() {
  try {
   return dataSource.getConnection();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return null;
 }

 public static void realseSource(ResultSet rs, Statement st, Connection conn)
   throws SQLException {
  try {
   if (rs != null)
    rs.close();
  } finally {
   try {
    if (st != null)
     st.close();
   } finally {
    if (conn != null)
     conn.close();
   }
  }
 }
}

 

我这一句dataSource.setProperties(properties); 注释掉了,我感觉他提供这个方法应该就是直接获取配置文件的,不过一直测试不通过,不知道什么原因,如果哪个高手知道,还请指点一下,非常感谢。

 

测试junit

public class C3p0SourceTest {
 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
 }
 
 @Test
 public void getConnection(){
  for(int y = 1;y<1000;y++){
   for(int i = 1;i<10;i++){
    Connection connection = C3p0Source.getConnection();
    System.out.println(i+"-----"+connection);
    try {
     C3p0Source.realseSource(null, null, connection);
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
  }
 }
}

 

基本就这样,我没仔细检查,应该有不是很正确的地方,还请指教。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值