知识大全 Hibernate数据源不得不注意的问题

Posted

篇首语:生活可以五颜六色,但绝不能乱七八糟。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Hibernate数据源不得不注意的问题相关的知识,希望对你有一定的参考价值。

Hibernate数据源不得不注意的问题  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

   Hibernate数据源    运行环境 Eclipse +MyEclipse +Tomcat +MS SQL Server + MS JDBC    一   在Tomcat 中配置数据源 并保证配置成功    二   在Hibernate中配置数据源    在hibernate cfg xml文件中 配置如下    <?xml version= encoding= UTF ?>    <!DOCTYPE hibernate configuration PUBLIC   //Hibernate/Hibernate Configuration DTD //EN      configuration 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:p/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 >     microsoft jdbc sqlserver SQLServerDriver    </property>    <property name= connection url >    jdbc:microsoft:sqlserver:// : ;DatabaseName=northwind    </property>    <property name= connection username >sa</property>    <property name= connection password >jckjdkmcj</property>    <property name= nnection pool size > </property>    <property name= hibernate show_sql >true</property>    <property name= jdbc fetch_size > </property>    <property name= jdbc batch_size > </property>    <property name= jdbc use_scrollable_resultset >false</property>     >    <!     <property name= hibernate dialect >    net sf hibernate dialect SQLServerDialect    </property>    <property name= connection datasource >    java:p/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     */    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 = /nf xml     * CONFIG_FILE_LOCATION = //foo/bar/nf 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     nfigure(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     *    * 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 ment 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 cha138/Article/program/Java/ky/201311/28303

相关参考