zoukankan      html  css  js  c++  java
  • 一个小应用的dbcp和c3p0配置实例

    以下是一个小应用的数据库连接池配置,包括DBCP和C3P0的配制方法

    因为是小应用,完全不涉及访问压力,所以配置上采取尽量节约数据库资源的方式

    具体要求如下:
    初始化连接数为0
    连接不够,需要新创建时,每次创建1个
    尽快回收空闲连接
    需要开启prepareStatement缓存

    具体用代码来说明

     1 package com.yazuo.util;
     2 
     3 import com.mchange.v2.c3p0.ComboPooledDataSource;
     4 import org.apache.commons.dbcp.BasicDataSource;
     5 import org.slf4j.Logger;
     6 import org.slf4j.LoggerFactory;
     7 import org.springframework.jdbc.core.JdbcTemplate;
     8 
     9 import java.util.Map;
    10 
    11 /**
    12  * Created by IntelliJ IDEA.
    13  * User: Luo
    14  * Date: 13-6-19
    15  * Time: 下午3:33
    16  */
    17 public class JdbcTemplateFactory {
    18     static Logger log = LoggerFactory.getLogger(JdbcTemplateFactory.class);
    19 
    20     private static JdbcTemplate jdbcTemplate;
    21 
    22 
    23     /**
    24      * 获取单例的jdbcTemplate,基于c3p0
    25      *
    26      * @return
    27      */
    28     public static JdbcTemplate getJdbcTemplate() {
    29         if (jdbcTemplate == null) {
    30             synchronized (JdbcTemplateFactory.class) {
    31                 if (jdbcTemplate == null) {
    32                     try {
    33                         //读取配置文件
    34                         Map<String, String> jdbcProps = PropertiesUtil.getPropertiesMap("jdbc.properties");
    35                         //创建连接池
    36                         ComboPooledDataSource dataSource = new ComboPooledDataSource();
    37                         dataSource.setJdbcUrl(jdbcProps.get("jdbc.url"));
    38                         dataSource.setUser(jdbcProps.get("jdbc.username"));
    39                         dataSource.setPassword(jdbcProps.get("jdbc.password"));
    40                         //默认初始化连接为3,设置为0
    41                         dataSource.setInitialPoolSize(0);
    42                         //默认每次创建连接数为3,设置为1
    43                         dataSource.setAcquireIncrement(1);
    44                         //默认最小连接数是3,设置为0
    45                         dataSource.setMinPoolSize(0);
    46                         //默认最长空闲时间为0,即不会回收空闲连接,设置为10秒
    47                         dataSource.setMaxIdleTime(10);
    48                         //默认为不开启 prepareStatement 缓存,设置为最大缓存5个
    49                         dataSource.setMaxStatements(5);
    50                         jdbcTemplate = new JdbcTemplate(dataSource);
    51                     } catch (Exception e) {
    52                         throw new IllegalStateException("数据库连接创建失败", e);
    53                     }
    54                 }
    55             }
    56         }
    57         return jdbcTemplate;
    58     }
    59 
    60     /**
    61      * 获取单例的jdbcTemplate,基于dbcp
    62      *
    63      * @return
    64      */
    65     public static JdbcTemplate getJdbcTemplateByDbcp() {
    66         if (jdbcTemplate == null) {
    67             synchronized (JdbcTemplateFactory.class) {
    68                 if (jdbcTemplate == null) {
    69                     try {
    70                         //读取配置文件
    71                         Map<String, String> jdbcProps = PropertiesUtil.getPropertiesMap("jdbc.properties");
    72                         //创建连接池
    73                         BasicDataSource dataSource = new BasicDataSource();
    74                         dataSource.setUrl(jdbcProps.get("jdbc.url"));
    75                         dataSource.setUsername(jdbcProps.get("jdbc.username"));
    76                         dataSource.setPassword(jdbcProps.get("jdbc.password"));
    77                         dataSource.setInitialSize(3);
    78 
    79                         //默认为不起动回收器,设置为30秒执行一次回收器
    80                         dataSource.setTimeBetweenEvictionRunsMillis(30 * 1000);
    81                         //默认为30分钟不使用的连接被认为空闲,设置为10秒钟
    82                         dataSource.setMinEvictableIdleTimeMillis(10 * 1000);
    83                         //开启 prepareStatement 缓存
    84                         dataSource.setPoolPreparedStatements(true);
    85                         jdbcTemplate = new JdbcTemplate(dataSource);
    86                     } catch (Exception e) {
    87                         throw new IllegalStateException("数据库连接创建失败", e);
    88                     }
    89                 }
    90             }
    91         }
    92         return jdbcTemplate;
    93     }
    94 
    95 }
  • 相关阅读:
    Java实现LeetCode_0041_FirstMissingPositive
    Java实现 Leetcode 169 求众数
    Java实现 LeetCode 137 只出现一次的数字
    Java实现 LeetCode 137 只出现一次的数字
    Java实现 Leetcode 169 求众数
    TimeUnit用法
    第六章:任务执行——Java并发编程实战
    Java实现的并发任务处理实例
    java三大框架实现任务调度——IRemindService
    如何在一个div中使其子div居中
  • 原文地址:https://www.cnblogs.com/baibaluo/p/3186260.html
Copyright © 2011-2022 走看看