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

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

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


<property name="maxIdleTime">1800property>

<property name="idleConnectionTestPeriod">60property>

<property name="acquireRetryAttempts">20property>
但是

最后

发现

并不是这个问题。

错误是:

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.class),uid,key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

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

你可能感兴趣的:(Spring,maven,mysql优化)