2011-10-12 154 views
0

我正在開發一個J2EE應用程序,它將接收來自移動應用程序的請求並執行一些數據庫操作以向用戶顯示數據。Tomcat連接池方法

因此數據庫連接池是對我,我重視我的J2EE application.It我META-INF文件夾聲明context.xml如下所示

<Context debug="0" 
    reloadable="true" crossContext="false" privileged="true" cookies="true" > 

    <Resource name="jdbc/servicedb" 
       auth="Container" 
       type="javax.sql.DataSource" 
       driverClassName="oracle.jdbc.OracleDriver" 
       username="XXXXXX" 
       password="XXXXXX" 
       url="jdbc:oracle:thin:@XXXXXXXXXXX:XXXX:XXXXXXX" 
       initialSize="10" 
       maxActive="100" 
       maxIdle="50" 
       minIdle="10" 
       suspectTimeout="60" 
       timeBetweenEvictionRunsMillis="30000" 
       minEvictableIdleTimeMillis="60000"/> 
</Context> 

這是從我的連接的方法類返回一個連接

public static Connection getConnection() 
    { 
     Connection dbConnection = null; 

     //loading from the dao.properties file 
     DAOProperties properties = new DAOProperties("com.jndi"); 
     String url = properties.getProperty(PROPERTY_URL, true); 
     String driverClassName = properties.getProperty(PROPERTY_DRIVER, false); 
     String password = properties.getProperty(PROPERTY_PASSWORD, false); 
     String username = properties.getProperty(PROPERTY_USERNAME, password != null); 

    // If driver is specified, then load it to let it register itself with DriverManager. 
     if(driverClassName !=null){ 
      try 
      { 
       Class.forName(driverClassName); 
       dbConnection = DriverManager.getConnection(url, username, password); 

      }catch(ClassNotFoundException e){ 
       // Could not find the database driver 
       e.printStackTrace(); 
       System.out.println("database driver error"); 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
     } 
    // Else assume URL as DataSource URL and lookup it in the JNDI. 
     else{ 
      Context initContext; 
      DataSource ds; 
      try { 
       initContext = new InitialContext(); 
       Context envContext = (Context)initContext.lookup(JNDI_ROOT); 
       if(envContext!=null){ 
        ds = (DataSource)envContext.lookup(url); 
        if(ds!=null){ 
         try { 
          dbConnection = ds.getConnection(); 
         } catch (SQLException e) { 
          e.printStackTrace(); 
         } 
        } 
       } 
      } catch (NamingException e) { 
       e.printStackTrace(); 
      } 
     } 
     return dbConnection; 
    } 

DAOProperties是裝載屬性file.I一個包裝類把它從here

我將結束這樣的

public static void close(Connection connection) { 
     if (connection != null) { 
      try { 
       connection.close(); 
      } catch (SQLException e) { 
       System.err.println("Closing Connection failed: " + e.getMessage()); 
       e.printStackTrace(); 
      } 
     } 
    } 

連接,最後我現在用的連接polulate這樣

public loadlist(){ 
     Connection dbConnection = null; 
     try{ 
      dbConnection = Connection.getConnection(); 
     if(dbConnection !=null){ 

      System.out.println("Connected to database"); 
      LOGGER.debug("Successfully connected to database"); 
     else{ 
       System.out.println("No Connection Exists"); 
       LOGGER.debug("unable to connect to database"); 
     } 
     }catch (SQLException e) { 
      // Could not connect to the database 
      e.printStackTrace(); 
      System.out.println("database connection failed"); 
      LOGGER.debug("database connection failed due to SQLException"); 
     }finally{ 
      Connection.close(dbConnection); 
      LOGGER.debug("connection closed"); 
     } 
     return result; 
    } 

剛纔我已經通過this在連接池了一個清單。

我有疑問我是否可以關閉我的連接或將它返回給池?

我也想知道如何返回連接池?

請幫忙。

回答

2

假設你的代碼將在getConnection()方法其他路徑(即,它JNDI查找,而不是使用的DriverManager的),你的連接池的使用是正確的。

您不能選擇關閉連接並將其返回到池中。如果您從池中獲得連接,則close()將返回到池(而不是關閉它)。沒有其他選擇。您只能關閉自己創建的連接。

+0

換句話說:處理來自池的'Connection'就像處理非池化連接一樣:只要在完成使用時調用'close()'。 –

+0

@Codo-謝謝你的回覆。你說的是tomcat只會管理連接。只要看看'close'方法[here](http://balusc.blogspot.com/2008/07/dao -tutorial-data-layer.html#HowAboutConnectionPooling) – Sreeram

+0

@Sreeram:在你的評論中鏈接的'close()'方法是一個說明連接池是如何實現的,而不是你應該如何使用它或者你應該如何實現你的DAO類。 Tomcat管理何時創建連接,何時重新使用連接,何時將連接返回到池以及何時關閉連接。 – Codo

0

我不明白你爲什麼要編寫自己的連接池。

我建議使用這種方法並使用由Tomcat管理的JNDI數據源和由其他人編寫的池。 Apache有一個很好的DBCP - 爲什麼寫自己的?

+0

@ duffymo-感謝您的回覆。我現在只遵循JNDI查找方法。我懷疑是關於''close'方法返回連接池。 – Sreeram