国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Hibernate數(shù)據(jù)源不得不注意的問題

2019-11-18 12:59:24
字體:
供稿:網(wǎng)友

  Hibernate數(shù)據(jù)源
  
  運(yùn)行環(huán)境:Eclipse 3.0.2+MyEclipse 3.8.3+Tomcat5.0.28+MS SQL Server2000+ MS JDBC
  
  一、  在Tomcat5.0.28中配置數(shù)據(jù)源,并保證配置成功
  
  二、  在Hibernate中配置數(shù)據(jù)源
  
  在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>
  
  在此文件中,我使用了兩種方法來實(shí)現(xiàn)到數(shù)據(jù)庫的連接,一種是使用了JDBC的方法,另一種是使用了數(shù)據(jù)源的方法。
  
  當(dāng)時(shí)我在測試的時(shí)候出了一點(diǎn)問題:當(dāng)時(shí)我配置好數(shù)據(jù)源后,啟動Tomcat,我以為數(shù)據(jù)源沒問題了,其實(shí)數(shù)據(jù)源就是沒問題,是我的程序有問題。我在一個(gè)類中寫了一個(gè)sessionFactory類,然后寫了一個(gè)測試類,但總是拋異常。后來我在jsp文件中測試,一下子就成功了。
  
  現(xiàn)在我終于明白了,原來,數(shù)據(jù)源一定要在Web工程的框架中使用,而不能在應(yīng)用程序中使用。
  
  其實(shí),那是因?yàn)檫@個(gè)數(shù)據(jù)源是在Tomcat服務(wù)器中做的配置,而我們知道,Tomcat僅僅可以做Servlet,JSP和WEB的容器,而不能做application的服務(wù)器,也就是說,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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 唐海县| 贵溪市| 揭西县| 邢台市| 丘北县| 青河县| 乌鲁木齐县| 巴马| 饶平县| 夏河县| 溆浦县| 普陀区| 体育| 英超| 柘荣县| 永嘉县| 梅河口市| 海林市| 中阳县| 巴林右旗| 射洪县| 镇远县| 朝阳县| 林口县| 盐津县| 锡林郭勒盟| 古丈县| 临邑县| 皮山县| 望江县| 广宁县| 奎屯市| 萝北县| 乐至县| 祁东县| 尖扎县| 定州市| 三台县| 黑龙江省| 固始县| 油尖旺区|