简体   繁体   English

与OracleDataSource的多个Oracle DB连接

[英]Multiple Oracle DB connections with OracleDataSource

This code is pseudo code 这段代码是伪代码

Trying to execute the method executeAllTheQueries(). 尝试执行executeAllTheQueries()方法。 But am seeing number of db connections are equal to the size of ArrayList<String> queries . 但我看到数据库连接数等于ArrayList<String> queries的大小。 Ideally it should be only once. 理想情况下它应该只有一次。 what's going wrong here? 这里出了什么问题?

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

    }

Looks like You create connection in every call to doSomething. 看起来你在每次调用doSomething时都会创建连接。 And you call doSomething for every query. 你为每个查询调用doSomething。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用oracle.jdbc.pool.OracleDataSource时如何获得空闲连接和活动连接的数量? - How do I get the number of free connections and active connections when using oracle.jdbc.pool.OracleDataSource? 运行 1 个 spring 引导应用程序时创建了多个 ORACLE 数据库连接 - Multiple ORACLE DB connections created while running 1 spring boot app 具有主数据库/备份数据库的单个JDBC OracleDataSource / HikariCP - Single JDBC OracleDataSource/HikariCP with primary/backup DB 指定的Oracle URL无效:OracleDataSource.makeURL - Invalid Oracle URL specified: OracleDataSource.makeURL 使用Java管理与数据库的多个连接 - Managing multiple connections to a DB in Java 指定了无效的Oracle URL:OracleDataSource.makeURL(WAS 8.5) - invalid Oracle URL specified: OracleDataSource.makeURL (WAS 8.5) java.lang.NoClassDefFoundError: oracle/jdbc/pool/OracleDataSource - 新的 - java.lang.NoClassDefFoundError: oracle/jdbc/pool/OracleDataSource - New One 出现错误指定了无效的Oracle URL:OracleDataSource.makeURL - getting error Invalid Oracle URL specified: OracleDataSource.makeURL oracle.jdbc.pool.OracleDataSource为每个新连接运行一个命令 - oracle.jdbc.pool.OracleDataSource run a command for every new connection 在oracle.jdbc.pool.OracleDataSource上设置池属性 - Setting Pooling Property on oracle.jdbc.pool.OracleDataSource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM