SpringJDBC配置C3P0数据库连接池

jopen 8年前

今天在Spring的配置文件里面配置C3P0数据源:

先导入c3p0的jar包

<bean id="dataSource" name="dataSource"          class="com.mchange.v2.c3p0.ComboPooledDataSource">          <!-- 指定连接数据库的驱动 -->          <property name="driverClass" value="com.mysql.jdbc.Driver" />          <!-- 指定连接数据库的URL -->          <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/jeanselam" />          <!-- 指定连接数据库的用户名 -->          <property name="user" value="root" />          <!-- 指定连接数据库的密码 -->          <property name="password" value="root" />          <!-- 指定连接池中保留的最大连接数. Default:15 -->          <property name="maxPoolSize" value="100" />          <!-- 指定连接池中保留的最小连接数 -->          <property name="minPoolSize" value="10" />          <!-- 指定连接池的初始化连接数 取值应在minPoolSize 与 maxPoolSize 之间.Default:3 -->          <property name="initialPoolSize" value="20" />          <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0 -->          <property name="maxIdleTime" value="600" />          <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3 -->          <property name="acquireIncrement" value="5" />          <!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。 但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数.如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0 -->          <property name="maxStatements" value="5" />          <!-- 每60秒检查所有连接池中的空闲连接.Default:0 -->          <property name="idleConnectionTestPeriod" value="60" />      </bean>        <!-- 通用泛型DAO组件 -->      <bean name="abstractDaoImpl" class="com.lin.jllm.dao.impl.AbstractDaoImpl"          abstract="true">          <property name="dataSource" ref="dataSource"></property>      </bean>


运行测试数据库的时候报错:



由于我的OS是linux,所以报错的原因可能是:

原因分析:

Linux于host相关的几个文件如下:

/etc/host.conf

功能:指定主机名查找方法,通常指先查找文件/etc/hosts,找不到时再向DNS服务器请求。

对于大多数用户不用改动此文件内容。

Linux: /etc/host.conf文件内容

order hosts, bind

multi on

/etc/resolv.conf

文件功能:DNS客户机配置文件,设置DNS服务器的IP地址及DNS域名

相关文件:/etc/host.conf

文件格式:

domainname 域名

search 域名

nameserver Primary_DNS_Server_IP_address

nameserver Second_DNS_Server_IP_address

其中domainname和search可同时存在,也可只有一个;nameserver可指定多个

/etc/hosts

#/etc/hosts

#文件格式: IPaddress hostname aliases

#文件功能: 提供主机名到IP地址的对应关系,建议将自己经常使用的主机

# 加入此文件中,也可将没有DNS记录的机器加入到此文件中,

# 这样会方便网络应用

127.0.0.1 localhost localhost.localdomain


解决:编辑/etc/hosts文件,在 127.0.0.1    localhost 后加上控制台里说的所谓的未知的名称和服务"lin-Dell-System-Inspiron-7420"



运行项目,OK


测试,写个action:

package com.lin.jllm.action;    import java.util.List;    import com.lin.jllm.domain.OrderInfo;  import com.lin.jllm.service.intf.IOrderInfoService;  import com.opensymphony.xwork2.ActionSupport;    public class TestAction extends ActionSupport {        private static final long serialVersionUID = 1L;            private IOrderInfoService orderInfoService;            private static volatile int a;              private synchronized static void incr() {          a++;      }        public IOrderInfoService getOrderInfoService() {          return orderInfoService;      }        public void setOrderInfoService(IOrderInfoService orderInfoService) {          this.orderInfoService = orderInfoService;      }        @Override      public String execute() throws Exception      {          /**           * 并发测试           */          int times = 10000;//设置一万个并发访问          long start = System.currentTimeMillis();                    for (int i = 0; i < times; i++) {                            new Thread(new Runnable() {                                    @Override                  public void run() {                                            try {                                                    /**                           * 读取记录                           */                          List<OrderInfo> orderInfos = orderInfoService.getAll();                          for(OrderInfo orderInfo : orderInfos)                          {                              System.out.println(orderInfo.toString());                          }                                                } catch (Exception e) {                          // TODO: handle exception                      }                      finally                      {                          incr();//记录访问次数                      }                  }              }).start();          }                    while (true) {              if (a == times) {                  System.out.println("并发量"+times+"\t完成时间:"                          + (System.currentTimeMillis() - start)+"ms");                  break;              }              Thread.sleep(100);          }                    return SUCCESS;      }    }


并发量10000    完成时间:6272ms

来自: http://my.oschina.net/u/2328736/blog/598569