Oracle getConnection 慢

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5503063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 23:12:20  来源:igfitidea点击:

Oracle getConnection slow

javaoraclejdbcdatasource

提问by bwawok

In a Java project, I am using an ojdbc6 jar

在 Java 项目中,我使用的是 ojdbc6 jar

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.1.0</version>
        <scope>compile</scope>
    </dependenc>

The first time for a day I run, dataSource.getConnection() is fast. Second time is usually okay. The next few times take around 45 seconds. After that, it takes multiple minutes. Once I have the FIRST connection of a given application run, any new connections I get are very very fast. This delay is only getting the FIRST connection for a given run.

我第一次跑步,dataSource.getConnection() 很快。第二次一般没问题。接下来的几次大约需要 45 秒。之后,需要几分钟的时间。一旦我运行了给定应用程序的第一个连接,我获得的任何新连接都非常快。此延迟仅获得给定运行的第一个连接。

What is making getting my first connection so slow?

是什么让我的第一个连接如此缓慢?

I am watching netstat and don't see any connection hanging after a successful run. Have tried several different connection pools (DBCP, C3PO) with no luck. Debugging through source code, the delay is 100% on the line of org.springframework.jdbc.datasource.DataSourceUtils:

我正在观看 netstat 并且在成功运行后没有看到任何连接挂起。尝试了几种不同的连接池(DBCP、C3PO),但都没有成功。通过源码调试,在org.springframework.jdbc.datasource.DataSourceUtils一行上延迟是100% :

Connection con = dataSource.getConnection();

Any ideas?

有任何想法吗?

Edited For More Details

已编辑以获取更多详细信息

1) I am using a connection pool (DBCP or C3PO) which saves connections for future use. When I talk about getting a new connection, I mean while the first connection is in use.. I need to go to the DB and get a NEW connection. Of course I can return and get my same connection from the connection pool over and over again. But getting a second at the same time is also fast.

1) 我正在使用一个连接池(DBCP 或 C3PO)来保存连接以备将来使用。当我谈论获得新连接时,我的意思是当第一个连接正在使用时..我需要去数据库并获得一个新连接。当然,我可以一次又一次地返回并从连接池中获取相同的连接。但同时获得一秒钟也很快。

2) I do not know how many connections my DB lets me be logged in with. Any idea where this property is in oracle?

2)我不知道我的数据库允许我登录多少个连接。知道这个属性在 oracle 中的什么位置吗?

回答by bwawok

Was due to java using /dev/random instead of /dev/urandom to make a ssh connection with the database.... switching to /dev/urandom fixed this

是由于 java 使用 /dev/random 而不是 /dev/urandom 与数据库建立 ssh 连接......切换到 /dev/urandom 解决了这个问题

回答by rfeak

2 questions.

2个问题。

First, after you get the first connection, what are you doing with it? Are you closing it correctly (INCLUDING all resources you opened with it, like Statement and ResultSet)?

首先,在您获得第一个连接后,您用它做什么?您是否正确关闭了它(包括使用它打开的所有资源,例如 Statement 和 ResultSet)?

Second, how many connections does your DB allow you for the credentials you logged in with?

其次,您的数据库允许您登录时使用的凭据有多少个连接?

The reason I ask these questions is that the time delays may be the amount of time it takes for the DB to clean up after you (because you may not have done it right) and free up a connection to give you. Much of this could just be timeouts before it forcefully releases resources.

我问这些问题的原因是时间延迟可能是数据库在您之后清理(因为您可能没有做对)并释放连接给您所需的时间。其中大部分可能只是在强制释放资源之前超时。

回答by Ray

Are you exceeding your max active connections in your connection pool and have to wait for your program to release old connections?

您是否超过了连接池中的最大活动连接数,并且必须等待您的程序释放旧连接?