2016-06-28 14 views
1

このコードは擬似コードOracleDataSourceとの複数のOracle DB接続

executeAllTheQueries()メソッドの実行を試みています。しかし、db接続の数がArrayList<String> queriesのサイズに等しいことが分かります。理想的には一度だけであるべきです。ここで何がうまくいかないの?

 

    public class Database { 
     Connection conneciton = null; 
     protected OracleDataSource ds; 

     public Database(String connectString, String user, String password) throws SQLException { 

      ds = new OracleDataSource(); 
      ds.setURL(connectString); 
      ds.setUser(user); 
      ds.setPassword(password); 
     } 

     //Method to open the connection if there isn’t one 
     public Connection createConnection() throws SQLException { 
     this.connection = (connection == null) ? ds.getConnection() : this.connection; 
     } 

     //Method to close the db connection 
     protected void closeConnection() throws SQLException { 
     if (connection != null) { 
      connection.close(); 
      this.connection = null; 
     } 
     } 

     //Method to do something 
     public String doSomething(String query) { 
      createConnection(); 
      //Doing something 
     } 


    //Class to execute all the queries 
    public class ExecuteQueries { 
     private ArrayList queries; 
     Database db; 

     public ExecuteQueries(ArrayList queries, Database db) { 
      this.queries = queries ; 
      this.db = db; 
     } 

     public ArrayList executeAllTheQueries() { 
      for (String query: this.queries) { 
       db.doSomething(query); 
      } 
     } 

    } 


答えて

0

doSomethingを呼び出すたびに接続を作成するように見えます。そして、すべてのクエリに対してdoSomethingを呼び出します。

関連する問題