2017-10-20 9 views
0

接続プールから接続を取得するために私は私のDB設定でMETA-INFに次のxmlファイルを持っている私が使用して接続プールを使用するには静的クラスは、私は例pool.For接続の概念を理解したい

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

    } 

です以下の方法で年度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); 
      } 
     } 

と私はそれが(例えばUserQueries用)各クラスが使用するセパレートたっプールを作成することを意味し、デシベルconectionsを使用し、各クラスに新しいDataBaseConnectionオブジェクトを作成しますか?

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

以下Datasource.getConnection()のTomcatの実装を参照してください。そして、これはconnectionPoolが作成されます。

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

  • DataSourceインスタンスは、すべてのJNDIルックアップのために返されます。
  • 新しい接続プールは、それが私のapplcationが唯一つの接続プールを使用する静的なデータソースの保証を使用していることを意味していDataSource
+0

のすべてのインスタンスのために作成されますか? –

+0

より良いput:単一の 'DataSource'インスタンスを使うことはお勧めです。 "...私のapplcationは唯一の接続プールを使用しますか?" _ - はい。_ – wiseOne

関連する問題