[英]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.