很多读者朋友都觉得连接池很神秘,本例将采用Proxool连接池完成一个简单的例子。大家可以到http://proxool.sourceforge.net下载连接池包,下载完成后把proxool-0.9.0RC3.jar(位于Lib下).。
新建一个web或java工程,为工程设置Hibernate环境(将所相关的jar包添加到工程之中)建立一个proxool.xml文件,将这个文档置于src目录之中:
<?xml version="1.0" encoding="UTF-8"?>
<!-- the proxool configuration can be embedded within your own application's.
Anything outside the "proxool" tag is ignored. -->
<something-else-entirely>
<proxool>
<alias>mysql</alias>
<driver-url>jdbc:mysql://localhost:3306/tdjob</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root" />
<property name="password" value="***" />
</driver-properties>
<house-keeping-sleep-time>60000</house-keeping-sleep-time>
<proxool.simultaneous-build-throttle>
100
</proxool.simultaneous-build-throttle>
<prototype-count>2</prototype-count>
<maximum-connection-count>100</maximum-connection-count>
<minimum-connection-count>10</minimum-connection-count>
</proxool>
</something-else-entirely>
现在来看下配置的意思:
<alias>配置连接池的别名;
<driver-url>和写JDBC连接数据库时的URL一样。
<driver-class>和写JDBC连接数据库时的Driver一样。
<driver-properies>就是连接数据库时的用户名和密码。
<house-keeping-sleep-time>proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁。
<prototype-count>最少保持的空闲连接数
<maximum-connection-count>最大连接数。
<minimum-connection-count>最小连接数。
以下是在Hibernate配置文件中对连接池的配置:
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- proxool连接池加载的类-->
<property name="hibernate.connection.provider_class">
org.hibernate.connection.ProxoolConnectionProvider
</property>
<!--连接池文件的地址-->
<property name="hibernate.proxool.xml">proxool.xml</property>
<!--连接池的别名,即配置连接池时起的别名-->
<property name="hibernate.proxool.pool_alias">mysql</property>
<!--是否将运行期生成的SQL输出到日志以供调试-->
<property name="show_sql">true</property>
<mapping resource="com/xmh/domain/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
新建一个实体文件:
package com.xmh.domain;
/**
* Admins generated by MyEclipse Persistence Tools
*/
public class User implements java.io.Serializable {
// Fields
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
……
}
创建一个映射文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.xmh.domain.User" table="td_user">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<property name="age" type="java.lang.Integer">
<column name="age" length="30" />
</property>
</class>
</hibernate-mapping>
编写测试文件:
package com.xmh.domain;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import com.xmh.factory.HibernateSessionFactory;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Logger log = Logger.getLogger(Teest.class);
Session session = HibernateSessionFactory.getSession();
String sqlstr = "from User u where u.id>:id";
try {
Query query = session.createQuery(sqlstr);
query.setInteger("id", 1);
List list = query.list();
log.info("the list's size are: "+list.size());
} catch (Exception e) {
log.error("is error!!!!");
log.error(e.getMessage());
}
}
}