熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Hibernate數據源不得不注意的問題

2022-06-13   來源: Java開源技術 

  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 execution Follows 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 instance Lazy 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() {
  
  Session sessio
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28303.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.