1.需要知道的:
- DBCP在hibernate3中以及不再被支持了,由于作者提出过bug,后续版本没有加入对其的支持。
- 推荐使用proxool,负面新闻最少的连接池
- 下面的图显示了,Hibernate 数据源的四种,分别是C3p0、JDBC、JNDI、Proxool,下面分别做解析
C3p0:
1 <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property> 2 <property name="hibernate.connection.password">root</property> 3 <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property> 4 <property name="hibernate.connection.username">root</property> 5 <property name="hibernate.default_catalog">hibernate</property> 6 <property name="hibernate.default_schema">testdb</property> 7 <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 8 <property name="hibernate.show_sql">true</property> 9 <property name="hibernate.format_sql">true</property> 10 <property name="hibernate.hbm2ddl.auto">create</property> 11 <property name="connection.useUnicode">true</property> 12 <property name="connection.characterEncoding">UTF-8</property> 13 <!-- C3P0连接池配置 --> 14 <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property> 15 <property name="hibernate.c3p0.max_size">20</property> 16 <property name="hibernate.c3p0.min_size">5</property> 17 <property name="hibernate.c3p0.timeout">120</property> 18 <property name="hibernate.c3p0.max_statements">100</property> 19 <property name="hibernate.c3p0.idle_test_period">120</property> 20 <property name="hibernate.c3p0.acquire_increment">2</property>
JDBC:
在没有做任何配置的情况下,默认就是JDBC 由Hibernate 自发完成对数据库连接的创建
1 <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property> 2 <property name="hibernate.connection.password">root</property> 3 <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property> 4 <property name="hibernate.connection.username">root</property> 5 <property name="hibernate.default_catalog">hibernate</property> 6 <property name="hibernate.default_schema">testdb</property> 7 <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 8 <property name="hibernate.show_sql">true</property> 9 <property name="hibernate.format_sql">true</property> 10 <property name="hibernate.hbm2ddl.auto">create</property> 11 <property name="connection.useUnicode">true</property> 12 <property name="connection.characterEncoding">UTF-8</property>
JNDI:
JNDI是Java 命名和目录接口(Java Naming and Directory Interface,JNDI)的简称.从一开始就一直是 JavaEE 企业的核心技术之一。在JMS,JMail,JDBC,EJB等技术中,就大量应用的这种技术。
-
修改conf/server.xml文件,在GlobalNamingResources节点中添加
1 <Resource name="jdbc/bookshop" auth="Container" 2 scope="jdbc/bookshop" 3 type="javax.sql.DataSource" 4 driverClassName="com.mysql.jdbc.Driver" 5 url="jdbc:mysql://192.168.1.2:3306/bookshop" 6 username="root" 7 password="lu123" 8 maxActive="50" 9 maxIdle="30" 10 maxWait="10000" />
然后接着再修改conf/context.xml文件,在Context节点中添加引用,最后还需要重启一下tomcat服务
1 <ResourceLink 2 global="jdbc/bookshop" 3 name="jdbc/bookshop" 4 auth="Container" 5 type="javax.sql.DataSource"/>
然后再hibernate.cfg.xml配置文件中加入JNDI的配置即可
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <!--<property name="connection.url">jdbc:mysql://192.168.1.2:3306/bookshop</property>--> 8 <!--<property name="connection.driver_class">com.mysql.jdbc.Driver</property>--> 9 <!--<property name="connection.username">root</property>--> 10 <!--<property name="connection.password">lu123</property>--> 11 12 <!-- JNDI --> 13 <property name="connection.datasource">java:comp/env/jdbc/bookshop</property> 14 15 <property name="hbm2ddl.auto">update</property> 16 <property name="show_sql">true</property> 17 <property name="format_sql">true</property> 18 19 <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 20 21 <property name="hibernate.current_session_context_class">thread</property> 22 23 24 <mapping resource="Users.hbm.xml"/> 25 26 </session-factory> 27 </hibernate-configuration>
Proxool(推荐):
主配置文件:
1 <!-- proxool代理连接器 在hibernate.cfg.xml 加入 --> 2 <property name="hibernate.connection.provider_class">org.hibernate.proxool.internal.ProxoolConnectionProvider</property> 3 <property name="hibernate.proxool.pool_alias">DBPool</property> 4 <property name="hibernate.proxool.xml">proxoolconf.xml</property>
次要配置文件:
1 <!-- 新建 proxoolconf.xml --> 2 <?xml version="1.0" encoding="UTF-8"?> 3 <!-- the proxool configuration can be embedded within your own application's. 4 Anything outside the "proxool" tag is ignored. --> 5 <something-else-entirely> 6 <proxool> 7 <!--连接池的别名 --> 8 <alias>DBPool</alias> 9 <!-- proxool只能管理由自己产生的连接 --> 10 <driver-url> 11 jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF8 12 </driver-url> 13 <!-- JDBC驱动程序 --> 14 <driver-class>com.mysql.jdbc.Driver</driver-class> 15 <driver-properties> 16 <property name="user" value="root" /> 17 <property name="password" value="root" /> 18 </driver-properties> 19 <!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回 收,超时的销毁 --> 20 <house-keeping-sleep-time>90000</house-keeping-sleep-time> 21 <!-- 指因未有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的 用户连接就不会被接受 --> 22 <maximum-new-connections>20</maximum-new-connections> 23 <!-- 最少保持的空闲连接数 --> 24 <prototype-count>5</prototype-count> 25 <!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的 等待请求数由maximum-new-connections决定 --> 26 <maximum-connection-count>100</maximum-connection-count> 27 <!-- 最小连接数 --> 28 <minimum-connection-count>10</minimum-connection-count> 29 </proxool> 30 </something-else-entirely> 31 32 ```