我应该为Spring 3.1.0,Hibernate 4.0.1.Final和MySQL 5.1使用哪些池数据源? [英] What pooled data source should I use for Spring 3.1.0, Hibernate 4.0.1.Final, and MySQL 5.1?

查看:61
本文介绍了我应该为Spring 3.1.0,Hibernate 4.0.1.Final和MySQL 5.1使用哪些池数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring 3.1.0.RELEASE,Hibernate 4.0.1.Final和MySQL 5.1。什么是我应该使用的合并数据源?

 < bean id =dataSourceclass = org.springframework.jdbc.datasource.DriverManagerDataSource> 
< property name =driverClassName>
<值> com.mysql.jdbc.Driver< /值>
< / property>
< property name =url>
< value> jdbc:mysql:// localhost:3306 / myproj< / value>
< / property>
< property name =username>
<值> myproj< /值>
< / property>
< property name =password>
<值>密码< /值>
< / property>
< / bean>

但这不是合并的数据源,每次调用都创建JDBC连接。我曾经拥有这个Hibernate配置(hibernate.cfg.xml)...

pre $ < hibernate-configuration>
< session-factory>
< property name =hibernate.connection.driver_class> com.mysql.jdbc.Driver< / property>
< property name =hibernate.connection.url> jdbc:mysql:// localhost:3306 / myproj< / property>
< property name =hibernate.connection.username> myproj< / property>
< property name =hibernate.connection.password>密码< / property>
< property name =hibernate.connection.pool_size> 10< / property>
< property name =show_sql> true< / property>
< property name =dialect> org.hibernate.dialect.MySQLDialect< / property>
...

但是由于Spring 3.1.0中的一个bug,在配置我的会话工厂bean(我试图这样做 - 从Spring应用程序上下文文件中摘录...)时,使用hibernate.cfg.xml文件...

 < bean class =org.springframework.orm.hibernate4.LocalSessionFactoryBeanid =sessionFactory> 
< property name =configLocation>
< value> classpath:hibernate.cfg.xml< / value>
< / property>


解决方案

您可以使用Apache DBCP,替换如下所示:

< bean id =dataSource
class =org.apache.commons.dbcp.BasicDataSourcedestroy-method =close>
< property name =driverClassNamevalue =$ {jdbc.driverClassName}/>
< property name =urlvalue =$ {jdbc.url}/>
< property name =usernamevalue =$ {jdbc.username}/>
< property name =passwordvalue =$ {jdbc.password}/>
< property name =maxActivevalue =10/>
< property name =minIdlevalue =5/>
<! - SELECT 1是一个简单的查询,它在MySQL中返回1行 - >
< property name =validationQueryvalue =SELECT 1/>
< / bean>

需要注意的几点




  • 您可以配置最大连接数。

  • 您可以配置最少空闲连接数。

  • 执行以验证连接是否仍然有效。


更多选项可用于配置验证时间。


I'm using Spring 3.1.0.RELEASE, Hibernate 4.0.1.Final, and MySQL 5.1. What is the pooled data source I should be using? I'm currently using (snippet from application context file) ...

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
        <value>jdbc:mysql://localhost:3306/myproj</value>
    </property>
    <property name="username">
        <value>myproj</value>
    </property>
    <property name="password">
        <value>password</value>
    </property>
</bean>

but this isn't a pooled data source, creating JDBC connections on each call. I used to have this Hibernate config (hibernate.cfg.xml) ...

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myproj</property>
    <property name="hibernate.connection.username">myproj</property>
    <property name="hibernate.connection.password">password</property>
    <property name="hibernate.connection.pool_size">10</property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    ...

but because of a bug in Spring 3.1.0, I can't use a hibernate.cfg.xml file when configuring my session factory bean (which I tried to do like this -- snippet from the spring application context file ...)

<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory">
    <property name="configLocation"> 
        <value>classpath:hibernate.cfg.xml</value> 
    </property>

解决方案

You can use Apache DBCP, which should be a drop in replacement something like this:

<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}"/>
    <property name="maxActive" value="10"/> 
    <property name="minIdle" value="5"/> 
    <!-- SELECT 1 is a simple query that returns 1 row in MySQL -->
    <property name="validationQuery" value="SELECT 1"/> 
</bean>

a few things to note

  • you can configure max number of connections.
  • you can configure min number of idle connections.
  • a query that will get executed to validate the connection is still valid.

Further options exist to configure when validation happens.

这篇关于我应该为Spring 3.1.0,Hibernate 4.0.1.Final和MySQL 5.1使用哪些池数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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