2

我在我的应用程序中使用数据库池(数据库池)。我的 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 以及如何调试实际问题。

编辑:

我的应用程序运行良好,但 throws 有时会突然开始抛出此错误。

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

4

1 回答 1

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();
}

}
于 2012-11-20T07:28:07.817 回答