DB池问题

时间:2012-11-20 06:29:11

标签: java database-connection connection-pooling

我在我的应用程序中使用数据库池(DB Pool)。我的DAO代码如下:

static {
        try {
            PropertyUtil propertyUtil = new PropertyUtil();
            propertyUtil.getBundle(Constants.DB_PROPERTIES);
            String dburl = propertyUtil.getProperty("dburl");
            String dbuserName = propertyUtil.getProperty("dbuserName");
            String dbpassword = propertyUtil.getProperty("dbpassword");
            String dbclass = propertyUtil.getProperty("dbclass");
            String dbpoolName = propertyUtil.getProperty("dbpoolName");
            int dbminPool = Integer.parseInt(propertyUtil
                    .getProperty("dbminPool"));
            int dbmaxPool = Integer.parseInt(propertyUtil
                    .getProperty("dbmaxPool"));
            int dbmaxSize = Integer.parseInt(propertyUtil
                    .getProperty("dbmaxSize"));
            Class.forName(dbclass).newInstance();
            moPool = new ConnectionPool(dbpoolName, dbminPool, dbmaxPool,
                    dbmaxSize, dburl, dbuserName, dbpassword);
            moLogWrapper.info("Connection pool size: -"+Integer.valueOf(moPool.getSize()));
        } catch (ApplicationException aoAppEx) {
            moLogWrapper
                    .error(aoAppEx.getMessage(), aoAppEx.fillInStackTrace());
            new ApplicationException(aoAppEx.getMessage(),
                    aoAppEx.fillInStackTrace());
        } catch (IllegalAccessException aoIllEx) {
            moLogWrapper
                    .error(aoIllEx.getMessage(), aoIllEx.fillInStackTrace());
            new ApplicationException(aoIllEx.getMessage(),
                    aoIllEx.fillInStackTrace());
        } catch (ClassNotFoundException aoCnfEx) {
            moLogWrapper
                    .error(aoCnfEx.getMessage(), aoCnfEx.fillInStackTrace());
            new ApplicationException(aoCnfEx.getMessage(),
                    aoCnfEx.fillInStackTrace());
        } catch (InstantiationException aoIEx) {
            moLogWrapper.error(aoIEx.getMessage(), aoIEx.fillInStackTrace());
            new ApplicationException(aoIEx.getMessage(),
                    aoIEx.fillInStackTrace());
        }

    }

我的openConnection()方法是:

public void openConnection() throws ApplicationException {
        moLogWrapper.info("inside openConnection method");
        try {
            loCon = moPool.getConnection();
            // moLogWrapper.info(moPool.getSize());
        } catch (SQLException aoSqlEx) {
            moLogWrapper
                    .error(aoSqlEx.getMessage(), aoSqlEx.fillInStackTrace());
            if (null != loCon) {
                loCon = null;
            }
                throw new ApplicationException(1002, aoSqlEx);
        } catch (Exception aoEx) {
            moLogWrapper.error(aoEx.fillInStackTrace());
            throw new ApplicationException(aoEx.getMessage(),
                    aoEx.fillInStackTrace());
        }
        moLogWrapper.info("exiting openConnection method");
    }

问题是我在连接池类的.openConnection方法中获取null。 我的日志中还打印了一个DEBUG日志,该日志打印在以下行中:

[snaq.db.ConnectionPool.sp] sp: Checkout - 10/10 (HitRate=40.186916%) - null returned

我无法理解为什么返回null以及如何调试实际问题。

修改

我的应用程序运行正常,但抛出开始突然抛出此错误。

我使用postgres作为我的数据库。

1 个答案:

答案 0 :(得分:0)

为什么不使用javax.sql.DataSource来获取连接。

import javax.sql.DataSource;

public class JdbcConnection {

private static DataSource dataSource;

static{
   //make DB connection here with datasource
}

public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}

}