1、首先在hibernate.cfg.xml的平级目录新建proxoolconf.xml
<?xml version="1.0" encoding="utf-8"?>
<something-else-entirely>
<proxool>
<alias>proxoolPool</alias>
<!--proxool只能管理由自己产生的连接-->
<driver-url>jdbc:mysql://localhost:3306/skycliff</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root"/>
<property name="password" value="sa"/>
</driver-properties>
<!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁-->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<!-- 指因未有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的用户连接就不会被接受-->
<maximum-new-connections>20</maximum-new-connections>
<!-- 最少保持的空闲连接数-->
<prototype-count>5</prototype-count>
<!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定-->
<maximum-connection-count>100</maximum-connection-count>
<!-- 最小连接数-->
<minimum-connection-count>10</minimum-connection-count>
</proxool>
</something-else-entirely>
2、配置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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="proxool.pool_alias">proxoolPool</property>
<property name="proxool.xml">proxoolconf.xml</property>
<property name="connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<mapping resource="skycliff/entitys/Account.hbm.xml" />
</session-factory>
</hibernate-configuration>
好了配置文件就到此结束。下面是具体的hibernate类及配置文件
//Account.java
package skycliff.entitys;
public class Account {
private Integer id;
private String name;
private Integer balance;
private int version;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getBalance() {
return balance;
}
public void setBalance(Integer balance) {
this.balance = balance;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
}
//Account.hbm.xml
<?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" >
<hibernate-mapping>
<class name="skycliff.entitys.Account" table="accounts">
<id name="id" type="integer">
<generator class="identity">
</generator>
</id>
<version name="version" column="VERSION"></version>
<property name="name" type="string" column="NAME"></property>
<property name="balance" type="integer" column="BALANCE"></property>
</class>
</hibernate-mapping>
//BaseDAO.java
package skycliff.dao;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class BaseDAO {
public BaseDAO(){
}
protected Session session;
public Session getSession(){
session=new Configuration().configure().buildSessionFactory().openSession();
return session;
}
}
//AccountsDAO.java
package skycliff.dao;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;
import skycliff.entitys.Account;
public class AccountsDAO extends BaseDAO {
public void registerAccoutn(Account account){
session=getSession();
Transaction tx=session.beginTransaction();
tx.begin();
try {
session.save(account);
tx.commit();
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
public void updateAccount(Account account){
session=getSession();
Transaction tx=session.beginTransaction();
tx.begin();
try{
session.update(account);
tx.commit();
session.close();
}catch(HibernateException e){
e.printStackTrace();
}
}
}