2012-11-20 99 views
2

我在我的应用程序中使用数据库池(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方法从连接池类。 有一个在我的日志打印以及调试日志,打印以下行:

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

我无法理解返回空的原因是,我怎么能调试的实际问题。

编辑:

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

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

+0

请问您可以发布完整的错误stacktrace? –

+0

启动时是否有错误? – Santosh

+0

@Santosh:请参阅我的编辑部分的问题。该应用程序通常运行良好,但突然抛出此错误。 – Ankit

回答

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

} 
+0

我的应用程序已经在生产环境中运行。我不想改变我的连接池方式,除非我发现任何无法解决的问题,而我认为这些问题并不存在。 – Ankit