2016-06-28 3 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을 호출합니다.