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
闽公网安备 35060202000074号