2012-11-20 100 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