点击页面多次后,jdbc操作,抛数据库连接资源不足异常

7 篇文章 0 订阅
7 篇文章 0 订阅

一开始以为是数据库连接池配置的问题。一开始配置

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
   <default-config>
      <property name="jdbcUrl">jdbc:oracle:thin:@wiseuat.wisers.com:1521:uatdb</property>
      <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
      <property name="user">wisers</property>
      <property name="password">uatuat</property>
      <property name="acquireIncrement">5</property>
      <property name="initialPoolSize">20</property>
      <property name="minPoolSize">10</property>
      <property name="maxPoolSize">30</property>
    </default-config>
</c3p0-config>
后来添加了

<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime">1800</property>
<!--60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod">60</property>
<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
<property name="acquireRetryAttempts">20</property>
但是

最后

发现

并不是这个问题。

错误是:

public QueryRunner qr = new QueryRunner(getDataSource());
写在了getUserPref()和getKeyList()方法里面了。每次都会获得新的连接,连接资源不够了

真正的原因是:要把QueryRunner写在数据库方法外面。下面这种方式就对了。

@Repository
public class UserPrefDaoImpl implements UserPrefDao {

    public static ComboPooledDataSource getDataSource() {
        ComboPooledDataSource ds = new ComboPooledDataSource("c3p0-config.xml");
        return ds;
    }

    public QueryRunner qr = new QueryRunner(getDataSource());

    @Override
    public UserPref getUserPref(String uid,String key) {
        UserPref user=null;
        try {
            String sql = "select * from USER_PREF u where u.USERID=? AND u.KEY= ? ";
            user=qr.query(sql,new BeanHandler<UserPref>(UserPref.class),uid,key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public List<String> getKeyList(String uid) {
        List<String> keyList=null;
        try {
            String sql = "select u.key from USER_PREF u where u.USERID=? ";
            keyList= (List<String>) qr.query(sql,new ColumnListHandler("KEY"),uid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return keyList;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值