在 Spring MVC 中设置连接池

发布于 2024-09-15 20:40:27 字数 926 浏览 2 评论 0 原文

如何在 Spring MVC 中设置连接池?我正在开发一个由 Spring MVC 2.5 和 jQuery 提供支持的 Intranet 网站。这是我第一次尝试网络开发。

我不确定,但是,我只在我的 spring 配置文件中使用它,并且我在 Spring MVC 逐步教程中看到了这一点,

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
 </bean>

 <bean id="propertyConfigurer"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>classpath:jdbc.properties</value>
   </list>
  </property>
 </bean>

这在开发过程中看起来很好,并且连接速度很快,但我不确定这是否仍然适用,如果许多用户同时连接。

我怎样才能实现这个目标?我读到这不是最佳连接数据源。

How can I setup connection pooling in Spring MVC? I am working on an intranet website powered by Spring MVC 2.5 and jQuery. This is my first attempt at web development.

I am not sure but, I am only using this in my spring configuration file and I saw this in the Spring MVC step By Step tutorial

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
 </bean>

 <bean id="propertyConfigurer"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>classpath:jdbc.properties</value>
   </list>
  </property>
 </bean>

This looks good during development and connection speed is fast but I am not sure if this will still holds true if many users are concurrently connected.

How can I achieve this? I have read that this is not an optimal connection datasource.

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

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

发布评论

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

评论(3

羁客 2024-09-22 20:40:27

您可能需要查看 c3p0,它提供了一些强大的配置和优化功能。

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="..." />
    <property name="jdbcUrl" value="..." />
    <property name="user" value="..." />
    <property name="password" value="..." />
</bean>

You might want to look at c3p0, which has some powerful configuration and optimization available.

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="..." />
    <property name="jdbcUrl" value="..." />
    <property name="user" value="..." />
    <property name="password" value="..." />
</bean>
相思故 2024-09-22 20:40:27

您当前的设置是正确的,为了使用基本连接池,您需要做的就是使用连接池库提供的 DataSource 实现,在您的情况下 Apache DBCP。请参阅这篇文章,了解其他替代方案 C3P0 的一些链接成为其中之一。

请注意,当您实际使用 DataSource bean 时,您将注入将其包装在 SimpleJdbcTemplate 或使用 DataSourceUtils 获取连接 - 请参阅 Spring JDBC 文档

Your current setup is correct, all you need to do in order to use basic connection pooling is use a DataSource implementation provided by a connection pooling library, in your case Apache DBCP. See this post for a few links to other alternatives, C3P0 being one of them.

Note that when you actually use the DataSource bean you're injecting wrap it in a SimpleJdbcTemplate or use DataSourceUtils to obtain a Connection - see Spring JDBC Documentation

少钕鈤記 2024-09-22 20:40:27
For connection Pooling

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />

//Add this two more parameters
   <property name="**initialSize**" value="20" />
   <property name="**maxActive**" value="30" />


 </bean>

connection pool will create 20 database connection as initialSize is 20 and goes up to 30 Database connection if required as maxActive is 30.
For connection Pooling

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />

//Add this two more parameters
   <property name="**initialSize**" value="20" />
   <property name="**maxActive**" value="30" />


 </bean>

connection pool will create 20 database connection as initialSize is 20 and goes up to 30 Database connection if required as maxActive is 30.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文