在hibernate.cfg.xml中配置的数据源只能在web环境下使用,我们可以通过下面的例子来比较.
如果我们在hibernatesessionfactory中加上测试,代码如下:
package sc.pro.forum.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();
}
}
public static void main(string[] args){
system.out.println("sdfsdfsd");
try{
session session=hibernatesessionfactory.currentsession();
if(session==null)
system.out.println("session is null");
else
system.out.println("session is not null");
}catch(exception e){
e.printstacktrace();
}
}
/**
* default constructor.
*/
private hibernatesessionfactory() {
}
}
执行时,程序会给我们如下的错误:
environment - hibernate 2.1.7
environment - hibernate.properties not found
environment - using cglib reflection optimizer
environment - using jdk 1.4 java.sql.timestamp handling
sdfsdfsd
configuration - configuring from resource: /hibernate.cfg.xml
configuration - configuration resource: /hibernate.cfg.xml
dtdentityresolver - trying to locate http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd in classpath under net/sf/hibernate/
dtdentityresolver - found http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd in classpath
configuration - connection.datasource=java:comp/env/jdbc/forum
configuration - show_sql=true
configuration - dialect=net.sf.hibernate.dialect.sqlserverdialect
configuration - null<-org.dom4j.tree.defaultattribute@18088c0 [attribute: name resource value "sc/pro/forum/dao/userinfo.hbm.xml"]
configuration - mapping resource: sc/pro/forum/dao/userinfo.hbm.xml
dtdentityresolver - trying to locate http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd in classpath under net/sf/hibernate/
dtdentityresolver - found http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd in classpath
binder - mapping class: sc.pro.forum.dao.userinfo -> userinfo
binder - mapped property: userid -> userid, type: integer
binder - mapped property: password -> password, type: string
binder - mapped property: nickname -> nickname, type: string
configuration - configured sessionfactory: null
configuration - properties: {java.runtime.name=java(tm) 2 runtime environment, standard edition, sun.boot.library.path=c:/program files/java/j2re1.4.2_03/bin, java.vm.version=1.4.2_03-b02, connection.datasource=java:comp/env/jdbc/forum, java.vm.vendor=sun microsystems inc., java.vendor.url=http://java.sun.com/, path.separator=;, java.vm.name=java hotspot(tm) client vm, file.encoding.pkg=sun.io, user.country=cn, sun.os.patch.level=service pack 4, java.vm.specification.name=java virtual machine specification, user.dir=e:/86vr/forum, java.runtime.version=1.4.2_03-b02, java.awt.graphicsenv=sun.awt.win32graphicsenvironment, java.endorsed.dirs=c:/program files/java/j2re1.4.2_03/lib/endorsed, os.arch=x86, java.io.tmpdir=c:/docume~1/zhangyi/locals~1/temp/, line.separator=
, java.vm.specification.vendor=sun microsystems inc., user.variant=, os.name=windows 2000, sun.java2d.fontpath=, hibernate.connection.datasource=java:comp/env/jdbc/forum, java.library.path=c:/program files/java/j2re1.4.2_03/bin;.;c:/winnt/system32;c:/winnt;c:/winnt/system32;c:/winnt;c:/winnt/system32/wbem;d:/program files/cvsnt;d:/j2sdk1.4/bin;c:/program files/microsoft sql server/80/tools/binn;c:/program files/cvsnt/, java.specification.name=java platform api specification, java.class.version=48.0, java.util.prefs.preferencesfactory=java.util.prefs.windowspreferencesfactory, os.version=5.0, user.home=c:/documents and settings/zhangyi, user.timezone=, java.awt.printerjob=sun.awt.windows.wprinterjob, file.encoding=gbk, java.specification.version=1.4, show_sql=true, user.name=zhangyi, java.class.path=e:/86vr/forum/webroot/web-inf/classes;e:/86vr/forum/webroot/web-inf/lib/ant-1.5.3.jar;e:/86vr/forum/webroot/web-inf/lib/ant-optional-1.5.3.jar;e:/86vr/forum/webroot/web-inf/lib/c3p0-0.8.4.5.jar;e:/86vr/forum/webroot/web-inf/lib/cglib-full-2.0.2.jar;e:/86vr/forum/webroot/web-inf/lib/commons-collections-2.1.1.jar;e:/86vr/forum/webroot/web-inf/lib/commons-dbcp-1.2.1.jar;e:/86vr/forum/webroot/web-inf/lib/commons-lang-1.0.1.jar;e:/86vr/forum/webroot/web-inf/lib/commons-logging-1.0.4.jar;e:/86vr/forum/webroot/web-inf/lib/commons-pool-1.2.jar;e:/86vr/forum/webroot/web-inf/lib/concurrent-1.3.3.jar;e:/86vr/forum/webroot/web-inf/lib/connector.jar;e:/86vr/forum/webroot/web-inf/lib/dom4j-1.4.jar;e:/86vr/forum/webroot/web-inf/lib/ehcache-0.9.jar;e:/86vr/forum/webroot/web-inf/lib/hibernate2.jar;e:/86vr/forum/webroot/web-inf/lib/jaas.jar;e:/86vr/forum/webroot/web-inf/lib/jboss-cache.jar;e:/86vr/forum/webroot/web-inf/lib/jboss-common.jar;e:/86vr/forum/webroot/web-inf/lib/jboss-jmx.jar;e:/86vr/forum/webroot/web-inf/lib/jboss-system.jar;e:/86vr/forum/webroot/web-inf/lib/jcs-1.0-dev.jar;e:/86vr/forum/webroot/web-inf/lib/jdbc2_0-stdext.jar;e:/86vr/forum/webroot/web-inf/lib/jgroups-2.2.7.jar;e:/86vr/forum/webroot/web-inf/lib/jta.jar;e:/86vr/forum/webroot/web-inf/lib/junit-3.8.1.jar;e:/86vr/forum/webroot/web-inf/lib/log4j-1.2.8.jar;e:/86vr/forum/webroot/web-inf/lib/msbase.jar;e:/86vr/forum/webroot/web-inf/lib/mssqlserver.jar;e:/86vr/forum/webroot/web-inf/lib/msuti
闽公网安备 35060202000074号