网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  web环境 hibernate数据源问题补充     
  文章作者:未知  文章来源:水木森林  
  查看:108次  录入:管理员--2007-11-17  
 
  在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
 
 
上一篇: 精通hibernate之映射继承关系六    下一篇: 精通hibernate之映射继承关系五
  相关文档
当mysql集群时, jdbc路径的使用方法. 11-17
spring+hibernate+jbpm整合成功 11-17
java技巧:java向web站点发送post请求 11-17
在oracle中存取blob对象实现文件的上传和下载 11-17
进阶:不使用泛型如何保证程序的可读性 01-16
spring,ioc模式与ejb3的slsb实现 11-17
英文打字练习 11-17
hibernate 3 annotations 进阶 11-17
彻底解决tomcat的页面提交时的中文问题 11-17
freemarker设计指南(4) (完) 11-17
swt/jface开发入门指南之搭建开发环境 11-17
如何处理http post/get请求 11-17
怎样学好java的一些总结 11-17
project jxta 中对等管道的构建 11-17
关于refactoring思考 11-17
实例讲解java语言利用搜索引擎收集网址 11-16
java助力火星探测 其认证深受重视 11-17
一个奇怪的问题 11-17
深入weblogic portal ui编程 11-17
jdo 2.0 的查询语言新特性 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息