ITEEDU

Hibernate Gossip: 数据库连结

Hibernate的配置文件中有一部份是在设定数据库连结,例如使用XML档案进行配置:
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- 显示实际操作数据库时的SQL -->
<property name="show_sql">true</property>
<!-- SQL方言,这边设定的是MySQL -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- JDBC驱动程序 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- JDBC URL -->
<property name="connection.url">jdbc:mysql://localhost/demo</property>
<!-- 数据库使用者 -->
<property name="connection.username">caterpillar</property>
<!-- 数据库密码 -->
<property name="connection.password">123456</property>
<!-- Hibernate 预设的Connection pool -->
<property name="connection.pool_size">2</property>

<!-- 对象与数据库表格映像文件 -->
<mapping resource="onlyfun/caterpillar/User.hbm.xml"/>

</session-factory>

</hibernate-configuration>
其中设定的connection.pool_size是Hibernate预设的连接池设定,通常只用于开发阶段测试之用。如果使用properties档案的话则如下:
hibernate.properties
hibernate.show_sql = true 
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost/demo
hibernate.connection.username = caterpillar
hibernate.connection.password = 123456
hibernate.connection.pool_size = 2
Hibernate在数据库连接池的使用上是可选的,您可以使用C3P0连接池,例如XML的配置方式如下:
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 显示实际操作数据库时的SQL -->
<property name="show_sql">true</property>
<!-- SQL方言,这边设定的是MySQL -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- JDBC驱动程序 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- JDBC URL -->
<property name="connection.url">jdbc:mysql://localhost/demo</property>
<!-- 数据库使用者 -->
<property name="connection.username">caterpillar</property>
<!-- 数据库密码 -->
<property name="connection.password">123456</property>
<!-- C3P0 连接池设定 -->
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>
<!-- 对象与数据库表格映像文件 -->
<mapping resource="onlyfun/caterpillar/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>

记得您的Classpath中必须包括c3p0-*.jar,属性文件hibernate.properties的配置范例如下:
hibernate.properties
hibernate.show_sql = true 
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost/demo
hibernate.connection.username = caterpillar
hibernate.connection.password = 123456
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
您也可以使用Proxool或DBCP连接池,只要在配置文件中配置hibernate.proxool.*或hibernate.dbcp.*等相关选 项,这可以在hibernate的etc目录中找hibernate.properties中的配置例子来参考,当然要记得在Classpath中加入相 关的jar档案。

如果您使用Tomcat的话,您也可以透过它提供的DBCP连接池来取得连接,您可以先参考 使用 DBCP 的文章来设定Tomcat的DBCP连接池。

设定好容器提供的DBCP连接池之后,您只要在配置文件中加入connection.datasource属性,例如在hibernate.cfg.xml中加入:
 <property  name="connection.datasource">java:comp/env/jdbc/dbname</property>
如果是在hibernate.properties中的话,则加入:
 hibernate.connection.datasource = java:comp/env/jdbc/dbname