JDBC连接池问题

发布于 2024-09-15 04:01:38 字数 1347 浏览 5 评论 0原文

我可能会说,我有一个很大的问题。

我正在开发 Java Web 应用程序,它使用 springs BasicDataSource 来设置数据库连接。我在本地测试该应用程序,它工作得很好......但是,当应用程序在线时,与数据库的连接在某些时候卡住了。我当时正在调查连接池,我发现在每个新的 HTTP 请求中,我执行了一些查询,就会创建新的池。据我所知,引入池是为了可重用,而不是每次涉及新的数据库访问时都创建池。还是我错了?

这是我的 spring 数据源配置:

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="url"/>
        <property name="username" value="username"/>
        <property name="password" value="password"/>
        <property name="defaultAutoCommit" value="true"/>
 <property name="defaultTransactionIsolation" value="1"/>
 <property name="initialSize" value="0"/>
 <property name="maxActive" value="20"/>
 <property name="minIdle" value="0"/>
</bean>

比我配置的:

<bean id="EventDao" class="my.managament.database.class">
    <property name="dataSource" ref="dataSource"/>
</bean>

以及处理发送到应用程序的所有 HTTP 请求的 mainPageController

<bean id="mainController" class="my.management.main.controller.class">

在应用程序的其余部分中,我使用 gedDatabase() 获取数据库连接,并通过 JDBCTemplate 进行选择。

我哪里出错了?

谢谢

I have, I might say, a quite a big issue.

I'm working on Java web application which use springs BasicDataSource to setup DB connection. I was testing the application locally and it works just fine... but, when application is online, connection to DB in some point just stuck. I was than investigating regarding connection pooling, and I figure it out that on each new HTTP request, where I have some of the queries executed, new pool is created. As I know, pooling is introduced to be reusable, and not created the each time when new DB access is involved. Or I'm wrong?

Here is my spring datasource config:

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="url"/>
        <property name="username" value="username"/>
        <property name="password" value="password"/>
        <property name="defaultAutoCommit" value="true"/>
 <property name="defaultTransactionIsolation" value="1"/>
 <property name="initialSize" value="0"/>
 <property name="maxActive" value="20"/>
 <property name="minIdle" value="0"/>
</bean>

Than I have configured:

<bean id="EventDao" class="my.managament.database.class">
    <property name="dataSource" ref="dataSource"/>
</bean>

And mainPageController which handles all HTTP requests sent to application

<bean id="mainController" class="my.management.main.controller.class">

In the rest of application, I use gedDatabase() to acquire DB connection, and do select through JDBCTemplate.

Where am I getting wrong?

Thanks

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

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

发布评论

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

评论(2

缱绻入梦 2024-09-22 04:01:38

您希望通过连接池使用 dao 和 jdbcTemplate 和 dataSource。我猜最接近您的设置的正确方法是拥有一个包含 JdbcTemplate 字段的 dao 和一个使用 dataSource bean 创建的 JdbcTemplate bean。它看起来像:

public class MyDAO {
  private JdbcTemplate jdbcTemplate;

  // your dao methods using jdbcTemplate here
}

其中 jdbcTemplate 来自这样的 bean:

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <constructor-arg ref="dataSource">
</bean>

您永远不需要直接从 dataSource(在您的情况下是基于 apache dbcp 的连接池)获取连接。 JdbcTemplate 将在需要时自行获取连接。我不确定“gedDatabase”是什么,但听起来您试图自己建立连接,但可能忘记关闭它。这将导致池很快耗尽连接。处理 20 个请求后,后续请求将在尝试从池中获取连接时被卡住。

我也不明白为什么以及如何看到多个池。您有一个连接池,最多可容纳 20 个连接。所有的 bean 都被创建为单例,这是默认的 spring 范围。

You want to use dao and jdbcTemplate and dataSource through a connection pool. My guess the closest correct approach to your setup is having a dao which has JdbcTemplate field and a JdbcTemplate bean created with your dataSource bean. It would look like:

public class MyDAO {
  private JdbcTemplate jdbcTemplate;

  // your dao methods using jdbcTemplate here
}

where jdbcTemplate comes from a bean like:

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <constructor-arg ref="dataSource">
</bean>

You should never need to obtain a connection from dataSource (which is apache dbcp based connection pool in your case) directly. JdbcTemplate will get a connection itself when needed. I'm not sure what "gedDatabase" is but it sounds like you tried to get connection yourself and possibly forgot to close it. This would result in pool quickly running out of connections. After handling 20 requests, the subsequent requests would be stuck on attempt to get connection from the pool.

Also I don't understand why and how you see multiple pools. You have a single connection pool which can hold up to 20 connections. All your beans are created as singletons which is the default spring scope.

梦与时光遇 2024-09-22 04:01:38

您的 EventDao 的生命周期是多少?您正在将 DataSource 注入到 dataSource 属性中。我怀疑您正在创建多个 EventDao bean,并且每次创建一个 bean 时都会有一个新的数据源。我认为我们需要进一步了解代码才能正确回答您的问题。

我的两分钱:
就我而言,通过 XML 编写代码和连接事物是一种可怕的反模式。

What is the life time of your EventDao ? You are injecting your DataSource into the dataSource property. I suspect that you are creating multiple EventDao beans and each time you create one you have a new DataSource. I think we would need to have further understanding of the code to be able to answer your question properly.

My two cents:
As far as I am concerned having code and wiring things through XML is a terrible anti-pattern.

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