Hibernate数据源
运行环境Eclipse +MyEclipse +Tomcat+MS SQL Server+ MS JDBC
一在Tomcat中配置数据源并保证配置成功
二在Hibernate中配置数据源
在hibernatecfgxml文件中配置如下
<?xml version= encoding=UTF?>
<!DOCTYPE hibernateconfiguration PUBLIC
//Hibernate/Hibernate Configuration DTD //EN
configurationdtd>
<! DO NOT EDIT: This is a generated file that is synchronized >
<! by MyEclipse Hibernate tool integration >
<hibernateconfiguration>
<sessionfactory>
<! properties >
<property name=connectiondatasource>java:comp/env/jdbc/northwind</property>
<property name=show_sql>true</property>
<property name=dialect>
netsfhibernatedialectSQLServerDialect
</property>
<!
<property name=dialect>
netsfhibernatedialectSQLServerDialect
</property>
<property name=connectiondriver_class>
commicrosoftjdbcsqlserverSQLServerDriver
</property>
<property name=connectionurl>
jdbc:microsoft:sqlserver://:;DatabaseName=northwind
</property>
<property name=connectionusername>sa</property>
<property name=connectionpassword>jckjdkmcj</property>
<property name=nnectionpoolsize></property>
<property name=hibernateshow_sql>true</property>
<property name=jdbcfetch_size></property>
<property name=jdbcbatch_size></property>
<property name=jdbcuse_scrollable_resultset>false</property>
>
<!
<property name=hibernatedialect>
netsfhibernatedialectSQLServerDialect
</property>
<property name=connectiondatasource>
java:comp/env/jdbc/northwind
</property>
<property name=show_sql>true</property>
>
<! mapping files >
<mapping resource=zy/pro/wd/dao/Shippershbmxml />
</sessionfactory>
</hibernateconfiguration>
在此文件中我使用了两种方法来实现到数据库的连接一种是使用了JDBC的方法另一种是使用了数据源的方法
当时我在测试的时候出了一点问题当时我配置好数据源后启动Tomcat我以为数据源没问题了其实数据源就是没问题是我的程序有问题我在一个类中写了一个SessionFactory类然后写了一个测试类但总是抛异常后来我在jsp文件中测试一下子就成功了
现在我终于明白了原来数据源一定要在Web工程的框架中使用而不能在应用程序中使用
其实那是因为这个数据源是在Tomcat服务器中做的配置而我们知道Tomcat仅仅可以做ServletJSP和WEB的容器而不能做Application的服务器也就是说Tomcat不能提供中间件的功能
我的SessionFactory类如下
package zyprowdutil;
import netsfhibernateHibernateException;
import netsfhibernateSession;
import netsfhibernatecfgConfiguration;
/**
* Configures and provides access to Hibernate sessions tied to the
* current thread of executionFollows the Thread Local Session
* pattern see {@link }
*/
public class HibernateSessionFactory {
/**
* Location of hibernatecfgxml 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 = /nfxml
* CONFIG_FILE_LOCATION = /com/foo/bar/nfxml</code>
*/
private static String CONFIG_FILE_LOCATION = /hibernatecfgxml;
/** 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 netsfhibernateSessionFactory sessionFactory;
/**
* Returns the ThreadLocal Session instanceLazy initialize
* the <code>SessionFactory</code> if needed
*
*@return Session
*@throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocalget();
if (session == null) {
if (sessionFactory == null) {
try {
nfigure(CONFIG_FILE_LOCATION);
sessionFactory = cfgbuildSessionFactory();
}
catch (Exception e) {
Systemerrprintln(%%%% Error Creating SessionFactory %%%%);
eprintStackTrace();
}
}
session = sessionFactoryopenSession();
threadLocalset(session);
}
return session;
}
/**
*Close the single hibernate session instance
*
*@throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocalget();
threadLocalset(null);
if (session != null) {
sessionclose();
}
}
/**
* Default constructor
*/
private HibernateSessionFactory() {
}
}
我的测试类如下
/*
* Created on
*
* TODO To change the template for this generated file go to
* Window Preferences Java Code Style Code Templates
*/
package zyprowdtest;
import zyprowdutil*;
import netsfhibernate*;
import junitframeworkTestCase;
/**
* @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) {
junitswinguiTestRunnerrun(HibernateSessionFactoryTestclass);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
supersetUp();
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
supertearDown();
}
public void testCurrentSession() {
Sessionsessio