可以在这里下载 http://proxool.sourceforge.net/
在 hibernate 中,通过简单配置,就可以使用 proxool。
先看看 proxool 的一个配置实例(本文使用 mysql, 读者可以根据需要改为其它数据库,比如 oracle 之类的)
文件名为 proxool-snake.xml
snake
大概解析一下:
alias:自定义的一个名称,通过此名称与 hibernate 关联;
driver-url, driver-class:数据库连接串和 driver 的信息;
maximum-connection-count, minimum-connection-count:最大、最小连接数;
现在看看 hibernate 的配置文件
文件名为 hibernate.cfg.xml
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
大概解析一下:
hibernate.proxool.pool_alias:即 proxool 中设置的 alias;
hibernate.proxool.existing_pool:此值设为 false,当 hibernate 开始被调用时,就会初始化 proxool,进行数据库连接等操作,这样最省事;
hibernate.proxool.xml:proxool 配置文件的名字,此例中是 proxool-snake.xml
配置文件编辑完成后,需要放到 CLASSPATH 路径中,迟些我会另外再写文章介绍通过 build.xml 来实现。
接下来我介绍一种从网上学习的,使用 hibernate 的方法。就是通过 HibernateUtil 来取得 管理和取得 session。以下是代码
HibernateUtil.java
package mixed.snake.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil
{
private static Log log = LogFactory.getLog(HibernateUtil.class);
public static final ThreadLocal
private static final SessionFactory sessionFactory;
static
{
try
{
sessionFactory = new Configuration().configure()
.buildSessionFactory();
}
catch (HibernateException e)
{
log.error("catch exception when configure hibernate: ", e);
throw new RuntimeException("Configuration problem: "
+ e.getMessage(), e);
}
}
public static Session currentSession() throws HibernateException
{
Session session = local.get();
if (session == null)
{
session = sessionFactory.openSession();
local.set(session);
}
return session;
}
public static void closeSession() throws HibernateException
{
Session session = local.get();
local.set(null);
if (session != null)
{
session.close();
}
}
java.lang.classnotfoundexception: com.microsoft.jdbc.sqlserver.sqlserver
sql servier的驱动包没有找着,检查是否添加sql servier的驱动包!
-----------------------------------------------------------------------------------------
你的配置是这样写的!
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.sqlserverdriver
</property>
而错误信息是这样的(注意粗体字标识)
java.lang.classnotfoundexception: com.microsoft.jdbc.sqlserver.sqlserver
检查一下你否有拼写错误!
像这种错误,一般都是驱动包没有导入或者拼写出现错误!
如果还有问题,建议下载最新版本的sql server驱动包!