2017-10-20 100 views
0

我想了解連接pool.For例子的概念連接,我在META-INF下面的XML文件與我的數據庫設置靜態類來獲得從連接池

<Resource name="jdbc/appname" 
       auth="Container" 
       type="javax.sql.DataSource" 
       maxActive="100" 
       maxIdle="30" 
       maxWait="10000" 
       minIdle="10" 
       username="postgres" 
       password="123" 
       driverClassName="org.postgresql.Driver" 
       url="jdbc:postgresql://localhost:5432/Lab4"/> 

要使用我使用連接池在folliwng類

public class DataBaseConnection { 

     private static DataSource dataSource; 

     static { 
      try { 
       InitialContext initContext = new InitialContext(); 
       dataSource = (DataSource) initContext.lookup("java:comp/env/jdbc/appname"); 
      } catch (NamingException ex) { 
       throw new RuntimeException(ex); 
      } 
     } 

     public static Connection getConnection() { 
      try { 
       return dataSource.getConnection(); 
      } catch (SQLException ex) { 
       throw new RuntimeException(ex); 
      } 
     } 

這是使用

public class UserQueries{ 
public User selectUserByLoginAndPassword(final String login, final String password) { 
     try(Connection connection=DataBaseConnection.getConnection()) 
     try (PreparedStatement st = connection.prepareStatement(SELECT_QUERY)) { 
      st.setString(1, login); 
      st.setString(2, password); 
      ResultSet result = st.executeQuery(); 
      while (result.next()) { 
       final User user = User.newBuilder() 
         .setId(result.getInt("id")) 
         .setAge(result.getInt("age")) 
         .setName(result.getString("name")) 
         .setPassword(result.getString("password")) 
         .setLogin(login) 
         .build(); 
       return user; 

      } 
     } catch (SQLException ex) { 
      throw new RuntimeException(ex); 
     } 
     throw new NullPointerException("Nu such user in db"); 
    } 

    } 

的問題是,如果我莫迪FY的DatabaseConnection以下列方式,

public class DataBaseConnection { 

     private DataSource dataSource; 

     public DataBaseConnection() 
      try { 
       InitialContext initContext = new InitialContext(); 
       dataSource = (DataSource) initContext.lookup("java:comp/env/jdbc/appname"); 
      } catch (NamingException ex) { 
       throw new RuntimeException(ex);     
     } 

     public Connection getConnection() { 
      try { 
       return dataSource.getConnection(); 
      } catch (SQLException ex) { 
       throw new RuntimeException(ex); 
      } 
     } 

,我將建立在每個使用DB conections類新的DatabaseConnection對象,這是否意味着每個類(例如UserQueries)將創建中隔離池連接如使用?

public class UserQueries{ 
    private DataBaseConnection dbCon = new DataBaseConnection(); 
public User selectUserByLoginAndPassword(final String login, final String password) { 
     try(Connection connection = dbCon.getConnection()) 
     try (PreparedStatement st = connection.prepareStatement(SELECT_QUERY)) { 
      st.setString(1, login); 
      st.setString(2, password); 
      ResultSet result = st.executeQuery(); 
      while (result.next()) { 
       final User user = User.newBuilder() 
         .setId(result.getInt("id")) 
         .setAge(result.getInt("age")) 
         .setName(result.getString("name")) 
         .setPassword(result.getString("password")) 
         .setLogin(login) 
         .build(); 
       return user; 

      } 
     } catch (SQLException ex) { 
      throw new RuntimeException(ex); 
     } 
     throw new NullPointerException("Nu such user in db"); 
    } 

    } 

回答

0

每個JNDI查找返回的DataSource的新實例。每個DataSource實例都維護它自己的連接池。

見下:,

public Connection getConnection() throws SQLException { 
    if (pool == null) 
     return createPool().getConnection(); 
    return pool.getConnection(); 
} 

tomcat的實施Datasource.getConnection()這將創建connectionPool

private synchronized ConnectionPool pCreatePool() throws SQLException { 
    if (pool != null) { 
     return pool; 
    } else { 
     pool = new ConnectionPool(poolProperties); 
     return pool; 
    } 
} 

綜上所述,

  • DataSource實例返回每個JNDI查找。
  • 新的連接池將用於DataSource
+0

每一個實例被創建這是否意味着使用我的applcation將使用只有一個連接池靜態數據源保證? –

+0

更好地說:使用一個'DataSource'實例是最好的方法。 「...我的applcation將使用唯一的連接池?」 _--是的 – wiseOne