Java JDBC 连接和 Oracle

发布于 2024-09-03 22:09:39 字数 1942 浏览 6 评论 0原文

我有一个场景,问题如下:

应用程序服务器有两个到数据库的连接池。 AB

A 指向 -> 数据库A ->有 128 个连接

A 具有存储过程,可通过 DB 链接 访问驻留在 DatabaseB 中的表

B< /code> 指向 -> 数据库B ->有 36 个连接

现在假设 Java 代码通过使用连接池 A 调用 DatabaseA 中的存储过程。此存储过程正在通过数据库链接从DatabaseB获取数据

问题:

基于此场景,如果我们在前端收到连接关闭错误。是否可以这样说:即使 java 从池 A (128) 调用 SP(在 DatabaseA 中),但由于 SP 从 DatabaseB 获取数据,因此数据量较少连接数 (36)。

基本上我想知道数据何时通过这样的数据库链接传送...它是否会取消分配给池 B 的 36 个连接指向 DatabaseB?

完全例外 我得到的确切异常是: --- Cause: java.sql.SQLException: Closed Connection

一些堆栈跟踪:

由以下原因引起:java.sql.SQLException: 关闭连接于 com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:185) 在 com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForList(GeneralStatement.java:123) 在 com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:614) 在 com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:588) 在 com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:118) 在 org.springframework.orm.ibatis.SqlMapClientTemplate$3.doInSqlMapClient(SqlMapClientTemplate.java:268) 在 org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:193) 在 org.springframework.orm.ibatis.SqlMapClientTemplate.executeWithListResult(SqlMapClientTemplate.java:219) 在 org.springframework.orm.ibatis.SqlMapClientTemplate.queryForList(SqlMapClientTemplate.java:266)

另外,我正在使用 iBatis ...所以没有 try..catch..finally

I have a scenario and the question follows

Application server has two connections pools to DB. A and B

A points to -> DatabaseA -> has 128 connections

A has Stored Procedures which access tables residing in DatabaseB over the DB link

B points to -> DatabaseB -> has 36 connections

Now lets say that Java code calls Stored Proc in DatabaseA by using connection pool A. This stored proc is getting data over the DB link from DatabaseB

Question:

Based on this scenario if we get connection closed errors on the front end. Is it viable to say that even though java is calling the SP (in DatabaseA) from pool A (128) but since the SP is bringing data from DatabaseB it has less amount of connections (36).

Basically I want to know when the data is brought over the DB link like this...does it take away from 36 connections assigned to pool B pointint to DatabaseB?

Exact Exception
Exact exception I get is: --- Cause: java.sql.SQLException: Closed Connection

Some Stack trace:

Caused by: java.sql.SQLException:
Closed Connection at
com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:185)
at
com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForList(GeneralStatement.java:123)
at
com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:614)
at
com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:588)
at
com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:118)
at
org.springframework.orm.ibatis.SqlMapClientTemplate$3.doInSqlMapClient(SqlMapClientTemplate.java:268)
at
org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:193)
at
org.springframework.orm.ibatis.SqlMapClientTemplate.executeWithListResult(SqlMapClientTemplate.java:219)
at
org.springframework.orm.ibatis.SqlMapClientTemplate.queryForList(SqlMapClientTemplate.java:266)

Also, I am using iBatis ...so don't have try..catch..finally blocks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒解孤独 2024-09-10 22:09:39

存储过程正在数据库中运行;当它与其他数据库建立连接时,它会建立直接连接,并且不会通过应用程序服务器的池。事实上,它可以与链接到 A 的任何数据库建立连接,无论应用服务器是否维护该数据库的连接池。

The stored procedure is running in the database; when it makes the connection to the other database it makes a direct connection and doesn't go through the app server's pool. In fact, it could make a connection to any database that is linked to A, regardless whether or not there's a connection pool to that database maintained by the app server.

此异常表示资源泄漏,即 JDBC 代码未正确关闭finally 块中的连接(以确保即使在发生异常时也能关闭)或者连接在多个线程之间共享。如果两个线程共享池中的同一个连接,并且其中一个线程关闭了该连接,则当另一个线程使用该连接时,就会发生此异常。

应该编写 JDBC 代码,以便在同一方法块中获取和关闭连接(以及语句和结果集)(以相反的顺序)。例如,

Connection connection = null;
// ...
try {
     connection = database.getConnection();
     // ...
} finally {
     // ...
     if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
}

另一个可能的原因是池保持连接空闲时间太长,并且在释放之前没有测试/验证它们。这可以在一个不错的连接池中进行配置。请参阅其文档。

This exception indicates a resource leak, i.e. the JDBC code is not properly closing connections in the finally block (to ensure that it's closed even in case of an exception) or the connection is been shared among multiple threads. If two threads share the same connection from the pool and one thread closes it, then this exception will occur when the other thread uses the connection.

The JDBC code should be written so that connections (and statements and resultsets) are acquired and closed (in reversed order) in the very same method block. E.g.

Connection connection = null;
// ...
try {
     connection = database.getConnection();
     // ...
} finally {
     // ...
     if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
}

Another possible cause is that the pool is holding connections too long idle and not testing/verifying them before releasing. This is configureable in a decent connection pool. Consult its documentation.

空‖城人不在 2024-09-10 22:09:39

“基本上我想知道数据何时通过这样的数据库链接传送...它是否会减少分配给池 B 的 36 个连接指向 DatabaseB?”

不会。无论连接池如何,数据库服务器都会与其他数据库服务器建立不同的连接。

我必须忍受防火墙在一段时间不活动后会切断连接,所以我经常看到这个错误。查看 dbms_session.close_database_link,因为数据库链接连接通常会在会话期间保留(并且由于您有一个连接池,该会话可能会停留很长时间)。

"Basically I want to know when the data is brought over the DB link like this...does it take away from 36 connections assigned to pool B pointint to DatabaseB?"

No. The database server will make a distinct connection to the other database server irrespective of any connection pool.

I have to suffer a firewall that cuts of connections after a period of inactivity so I see this error quite a lot. Look into dbms_session.close_database_link, since the database link connection would generally remain for the duration of the session (and since you have a connection pool, that session probably sits around for a very long time).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文