Hibernate数据源 运行环境:Eclipse 3.0.2+MyEclipse 3.8.3+Tomcat5.0.28+MS SQL Server2000+ MS JDBC
一、 在Tomcat5.0.28中配置数据源,并保证配置成功
二、 在Hibernate中配置数据源
在hibernate.cfg.xml文件中,配置如下
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<hibernate-configuration>
<session-factory>
<!-- properties -->
<property name="connection.datasource">java:comp/env/jdbc/northwind</property>
<property name="show_sql">true</property>
<property name="dialect">
net.sf.hibernate.dialect.SQLServerDialect
</property>
<!--
<property name="dialect">
net.sf.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://10.0.0.168:1433;DatabaseName=northwind
</property>
<property name="connection.username">sa</property>
<property name="connection.password">jckjdkmcj</property>
<property name="hibernate.connection.pool.size">10</property>
<property name="hibernate.show_sql">true</property>
<property name="jdbc.fetch_size">50</property>
<property name="jdbc.batch_size">25</property>
<property name="jdbc.use_scrollable_resultset">false</property>
-->
<!--
<property name="hibernate.dialect">
net.sf.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.datasource">
java:comp/env/jdbc/northwind
</property>
<property name="show_sql">true</property>
-->
<!-- mapping files -->
<mapping resource="zy/pro/wd/dao/Shippers.hbm.xml" />
</session-factory>
</hibernate-configuration>
在此文件中,我使用了两种方法来实现到数据库的连接,一种是使用了JDBC的方法,另一种是使用了数据源的方法。
当时我在测试的时候出了一点问题:当时我配置好数据源后,启动Tomcat,我以为数据源没问题了,其实数据源就是没问题,是我的程序有问题。我在一个类中写了一个SessionFactory类,然后写了一个测试类,但总是抛异常。后来我在jsp文件中测试,一下子就成功了。
现在我终于明白了,原来,数据源一定要在Web工程的框架中使用,而不能在应用程序中使用。
其实,那是因为这个数据源是在Tomcat服务器中做的配置,而我们知道,Tomcat仅仅可以做Servlet,JSP和WEB的容器,而不能做Application的服务器,也就是说,Tomcat不能提供中间件的功能。
我的SessionFactory类如下:
package zy.pro.wd.util;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html}.
*/
public class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();
/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();
/** The single instance of hibernate SessionFactory */
private static net.sf.hibernate.SessionFactory sessionFactory;
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* Default constructor.
*/
private HibernateSessionFactory() {
}
}
我的测试类如下:
/*
* Created on 2005-7-29
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package zy.pro.wd.test;
import zy.pro.wd.util.*;
import net.sf.hibernate.*;
import junit.framework.TestCase;
/**
* @author zhangyi
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class HibernateSessionFactoryTest extends TestCase {
public static void main(String[] args) {
junit.swingui.TestRunner.run(HibernateSessionFactoryTest.class);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}
public void testCurrentSession() {
Session sessio