关于 org.apache.commons.dbcp.BasicDataSource 的问题 [英] Question regarding org.apache.commons.dbcp.BasicDataSource

查看:41
本文介绍了关于 org.apache.commons.dbcp.BasicDataSource 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我修复了一些与我们使用 BasicDataSource 的方式相关的错误,虽然我了解其中的一部分,但仍有一些问题没有得到解答:)

I fixed some bug related to the way we were using BasicDataSource and though I understand part of it I still have some questions unanswered :)

问题:数据库失败后,应用程序无法自动连接到数据库.

Problem: The application was not able to auto-connect to the database after a db failure.

应用程序正在使用 org.apache.commons.dbcp.BasicDataSource 类 作为到 Oracle 数据库的 JDBC 连接的 TCP 连接池.

Application is using org.apache.commons.dbcp.BasicDataSource class as a TCP-connection pool for a JDBC connection to Oracle db.

修正:经过一番研究,我发现在 BasicDataSource 中 testOnBorrow 和 testOnreturn 没有设置.我提供了验证查询来测试连接.这解决了问题

Fix: After some research I discovered that in BasicDataSource testOnBorrow and testOnreturn were not set. I provided the validation query to test connections. This fixed the problem

池中的最大连接数设置为 1

Max no of connections in pool was set to 1

我的理解:连接池会将连接移交给应用程序.我认为发生的事情是应用程序 MAGICALLY 在数据库崩溃时将坏集合返回到池中.现在,由于池不知道它是否是一个坏连接,它会在下次需要时将相同的连接移交给应用程序,从而导致应用程序无法自动重新连接到数据库.

My Understanding: The connection pool would hand over a connection to the application. What I think was happening was the application MAGICALLY returned the bad collection to the pool when it db crashed . Now since the Pool does not know if it is a bad connection it would hand over the same connection to the application next time it needs it causing the application to not auto-reconnect to db.

现在,在修复之后.. 每当一个错误的连接返回到连接池时,它都会被丢弃并且不会再次使用,因为我上面做了修复.

Now, after the fix.. whenever a bad connection is returned to the connection pool it would be discarded and wont be used again because of the fix I made above.

现在我知道 BasicDataSource 在提供给应用程序之前包装了连接,这样每当应用程序说 con.close ..BasicDataSource 就会知道该连接不再使用..它将负责将连接返回给池或丢弃等.

Now I know that BasicDataSource wraps the connection before giving to the application, such that whenever application says con.close ..BasicDataSource would know that the connection is not used any more.. it will take care of either returning the connection to the pool or discardigg etc.

未回答的问题:然而,我不明白是什么使应用程序神奇在其断开时将连接返回到连接池[请注意,当连接不正常退出时,不会调用 con.close 方法].BasicDataSource 无法知道连接已关闭或有 ?.有人可以指出我的代码吗?

Unanswered Question: However what I do not understand is what makes the application MAGICALLY return the connection to the connection pool when its broken[Note that con.close method is not called when the connection exits un-gracefully]. There is no way of BasicDataSource to know that the connection closed or there is ?. Can someone point me to code for that ?

我的整体理解是修复为什么有效??

I my overall understanding connect of why the fix worked ??

推荐答案

现在,我知道这是一个旧线程,但它在 google 搜索结果中的位置很高,所以我想我可以快速回答一下.有关配置 BasicDataSource 的更多信息,您应该参考 DBCP 配置页面:http://commons.apache.org/proper/commons-dbcp/configuration.html

Now, I know that this is kind of an old thread, but it's high on google search results, so I thought I might give it a quick answer. For more information on configuring the BasicDataSource, you should reference the DBCP Configuration page: http://commons.apache.org/proper/commons-dbcp/configuration.html

回答BasicDataSource 如何知道一个连接何时被放弃并需要返回到连接池?"的未回答"问题.(转述)...

To answer the "unanaswered" question of "How does BasicDataSource know when a connection is abondoned and needs to be returned to the connection pool?" (paraphrased)...

org.apache.commons.dbcp.BasicDataSource 能够通过使用连接的包装类来监控它提供的连接的流量和使用情况.每次调用连接上的方法或从连接创建的任何语句时,实际上是在调用实现接口或使用相同方法扩展基类的包装类(多态万岁!).这些自定义方法允许数据源知道连接是否处于活动状态.

org.apache.commons.dbcp.BasicDataSource is able to monitor traffic and usage on the connections it offers by using a wrapper class for the Connection. Every time you call a method on the connection or any Statements created from the connection, you are actually calling a wrapper class that implements an interface or extends a base class with those same methods (Hurray for Polymorphism!). These custom methods allow the DataSource to know whether or not a Connection is active.

在 BasicDataSource 本身上,有一个名为removeAbandoned"的属性和另一个名为removeAbandonedTimeout"的属性,用于配置将放弃的连接返回到连接池的行为.

On the BasicDataSource itself, there is a property called "removeAbandoned" and another called "removeAbandonedTimeout" that are used to configure this behavior of returning abondonded connections to the connection pool.

"removeAbandoned" 是一个布尔值,指示是否应将放弃的连接返回到池中.默认为假".

"removeAbandoned" is a boolean that indicates whether abandoned connections should be returned to the pool. Defaults to "false".

"removeAbandonedTimeout" 是一个 int,表示在连接被视为放弃之前应该允许经过的不活动秒数.默认值为 300(约 5 分钟).

"removeAbandonedTimeout" is an int, that represents the number of seconds of inactivity that should be allowed to pass before a connection is considered to be abandoned. Default value is 300 (about 5 minutes).

这篇关于关于 org.apache.commons.dbcp.BasicDataSource 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆