`connection.close()`不会在服务器端关闭连接。

huangapple go评论86阅读模式
英文:

connection.close() does not close the connection at server side

问题

I am doing some data validations on Oracle DB. There are 10 different types of validations I have to do. Each set of validation contains hundreds of test cases. To save time, I am running the validations in parallel on 10 different threads. Each thread opens a database connection, execute the test cases and then close the connection - and do the same for the next test case. The problem is that I can have a maximum of 8 concurrent Oracle sessions. It is currently not possible to increase the number of sessions. To handle this scenario, I am using Semaphore to limit the number of concurrent Oracle sessions to 8.

public class DatabaseConnection {
    Semaphore counter = new Semaphore(8);
    public Connection createConnection() {
        ...
        ...
        counter.acquire();
        Connection connection = DriverManager.getConnection(....
        ...
    }

    public closeConnection(Connection con) {
        ...
        con.close();
        counter.release();
    }
}

Even though I am closing the connections properly after executing each test, I am getting a maximum number of sessions exceeded exception. I suspect that even though I close the connections, they are not properly closed at the server-side. What is the solution for this?

英文:

I am doing some data validations on Oracle DB. There are 10 different types of validations I have to do. Each set of validation contains hundreds of test cases. To save time, I am running the validations in parallel on 10 different threads. Each thread opens a database connection, execute the test cases and then close the connection - and do the same for the next test case. The problem is that I can have maximum of 8 concurrent oracle sessions. It is currently not possible to increase the number of sessions. To handle this scenario, I am using Semaphore to limit the number of concurrent oracle sessions to 8

public class DatabaseConnection{
 Semaphore counter = new Semaphore(8);
 public Connection createConnection(){
   ...
   ...
   counter.acquire();
   Connection connection = DriverManager.getConnection(....
   ...
}

public closeConnection(Connection con){
  ...
  con.close();
  counter.release();
}
}

Eventhough I am closing the connections properly after executing each test, I am getting maximum number of sessions exceeded exception. I suspects eventhough I close the connections, it is not properly closed at server side. What is the solution for this?

答案1

得分: 1

我正在同时在10个不同的线程上运行验证。

我想知道你是如何运行这些并行测试的?是否有可能最终会得到10个DatabaseConnection类的实例?如果是的话,你也会得到10个计数每次为8的信号量。在这种情况下,最大连接数将达到80...你可以避免这个问题,将信号量声明为静态变量。

静态信号量示例:

static Semaphore counter = new Semaphore(8);
英文:

> I am running the validations in parallel on 10 different threads.

I wonder how are you running the parallel tests? Is it possible that you will end up with 10 instances of the DatabaseConnection class? If yes then you will also get 10 semaphores counting 8 times each. In this case the maximum number of connections will be 80... You can avoid it declaring the semaphore as static.

static Semaphore counter = new Semaphore(8);

huangapple
  • 本文由 发表于 2023年5月22日 12:40:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定