Java资源网

| JAVA基础 | 环境配置 | JDBC | 线程技术 | Socket编程 | JavaMail | JAVA与XML | 设计模式 | 技术新闻 | Java认证 | 程序人生 软件下载
| JSP&Servlet | Spring | Struts | Hibernate | JBuilder | Eclipse | WebService | EJB技术 | J2ME开发 | 应用服务器 | JXTA | Ajax
Articles search文章搜索
   关键字:
   类 别:
       
New download 最新下载
· [组件]HTML Parser 1.5
· [教程]WebSphere Studio应用教程
· [组件]JDom 1.0
· [工具]Junit3.8.1
· [教程]EJB编程及J2EE系统架构和设计
· [教程]EJB教程
· [教程]J2EE Tutorial中文版
· [教程]Java编程思想2(英文)
· [教程]java编程思想(完整版)
· [教程]Java网络编程
New articles 最新文章
· 设计移动 Web 服务
· 解析XML的时候完全忽略DTD
· 理解XML Schema XML Schema 初步
· 标签库的深入研究
· 提升JSP应用程序的七大绝招
· 如何使用JDOM对XML文件进行操作
· 处理XML字符串中特殊字符
· 利用Digester把XML转换成为Java对象
· 使用WebService 和RMI远程协作
· 使用Axis开发Web Service程序
Articles top 热门文章
· Eclipse基础--plugin插件安装(6644)
· eclipse+tomcat+lomboz的安装配置说明(4774)
· Java程序员就业前景(4584)
· Windows下JAVA环境变量的设置祥解(3788)
· Tomcat下JSP、Servlet和JavaBean环境的配置(3716)
· 使用links方式安装Eclipse插件(3698)
· 一个老程序员的心理话(3533)
· linux下jdk的安装与配置(3459)
· 初学者入门:Structs中基本配置入门(3334)
· Eclipse 运行命令行参数大全(3084)
您的位置:首页>>Hibernate>>Hibernate数据源不得不注意的问题
Hibernate数据源不得不注意的问题
2007-04-17   来源:www.javaresearch.org  作者:未知

  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
  --相关文章--
· 精通Hibernate之映射继承关系四(图) (2007-04-17)
· 精通Hibernate之映射继承关系六 (2007-04-17)
· 精通Hibernate之映射继承关系八 (2007-04-17)
· 精通Hibernate之映射继承关系五 (2007-04-17)
· 精通Hibernate之映射继承关系二(图) (2007-04-17)
· 精通Hibernate之映射继承关系三 (2007-04-17)

版权所有©2005-2006 JAVA资源网 渝ICP备05007591号 虚拟主机 | 关于我们 | 联系方式 | 广告业务 | 网站地图 | 友情链接