【问题标题】:hibernate connection pool休眠连接池
【发布时间】:2011-01-05 06:26:30
【问题描述】:

它说,我似乎无法休眠以使用 c3p0 进行连接池

12:30:35,038  INFO DriverManagerConnectionProvider:64 - Using Hibernate built-in connection pool (not for production use!)
12:30:35,038  INFO DriverManagerConnectionProvider:65 - Hibernate connection pool size: 20

休眠配置:

<hibernate-configuration>
  <session-factory>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/xxx?autoReconnect=true</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">xxxx</property>
      <property name="show_sql">false</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <property name="hibernate.c3p0.min_size">5</property>
      <property name="hibernate.c3p0.max_size">200</property>
      <property name="hibernate.c3p0.max_statements">200</property>
      <property name="current_session_context_class">thread</property>
  </session-factory>
</hibernate-configuration>

【问题讨论】:

    标签: hibernate connection-pooling c3p0


    【解决方案1】:
    <!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
    
            <!-- datasource config -->
            <property name="connection.url">jdbc:mysql://localhost:3306/db?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    
            <property name="connection.username">user</property>
            <property name="connection.password">pass</property>
    
            <!-- c3p0 config http://www.hibernate.org/214.html -->
            <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>        
            <property name="hibernate.c3p0.acquire_increment">1</property>
            <property name="hibernate.c3p0.idle_test_period">60</property>
            <property name="hibernate.c3p0.min_size">1</property>
            <property name="hibernate.c3p0.max_size">2</property>
            <property name="hibernate.c3p0.max_statements">50</property>
            <property name="hibernate.c3p0.timeout">0</property>
            <property name="hibernate.c3p0.acquireRetryAttempts">1</property>
            <property name="hibernate.c3p0.acquireRetryDelay">250</property>
    
            <property name="hibernate.show_sql">true</property>
            <property name="hibernate.use_sql_comments">true</property>
    
            <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
            <property name="hibernate.current_session_context_class">thread</property>
    
            ...
    
        </session-factory>
    </hibernate-configuration>
    

    【讨论】:

    • 选择合适的connection.provider_class很重要。
    • org.hibernate.connection.C3P0 已被弃用,取而代之的是 org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider
    • 感谢@lserni 提及,过去两年我一直没有更新hibernate。
    • @lserni 猜这是休眠 4 的变化?无论如何,还有一些其他注意事项:您不必“必须”指定 provider_class,只要您指定“一些”c3p0 设置,它似乎就会使用它,尽管最好明确地调用它。
    • 我认为idle_test_period的值不能超过timeout。
    【解决方案2】:

    解决这些 DTD(文档类型定义)映射问题的最佳方法是

    1. 获取hibernate jar 文件并解压缩/jar 并获取这两个文件 - hibernate-configuration dtd 文件和 hibernate-mapping dtd 文件。

    2. 从这两个文件中复制 DTD 声明并在您的 分别是cfg.xml和hbm.xml。

    【讨论】:

    • 这与题中的问题无关。
    猜你喜欢
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 2013-12-29
    相关资源
    最近更新 更多