【问题标题】:Issue with DB pool数据库池问题
【发布时间】:2012-11-20 06:29:11
【问题描述】:

我在我的应用程序中使用数据库池 (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 方法中得到空值。 我的日志中还打印了一个 DEBUG 日志,它打印以下行:

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

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

编辑:

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

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

【问题讨论】:

  • 你能发布完整的错误堆栈跟踪吗?
  • 开机有没有错误?
  • @Santosh:请参阅我在问题中的编辑部分。应用程序通常运行良好,但突然开始抛出此错误。
  • @sheldonCooper:我所拥有的堆栈跟踪的其余部分来自我自己的应用程序,这是由于连接为空而导致的空指针异常。
  • @sheldonCooper: moPool 不为空,但 moPool.getConnection() 返回的值为空。

标签: java database-connection connection-pooling


【解决方案1】:

为什么不使用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();
}

}

【讨论】:

  • 我的应用程序已经在生产环境中运行。我不想改变我的连接池方式,除非我发现任何无法解决的问题,我认为不存在。
猜你喜欢
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-13
  • 2016-01-20
  • 1970-01-01
相关资源
最近更新 更多