【问题标题】:How to use a singleton Session factory in an application with multiple modules如何在具有多个模块的应用程序中使用单例会话工厂
【发布时间】:2014-07-07 08:45:20
【问题描述】:

我有一个这样定义的会话工厂 bean:

   <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
    </bean>
    <bean id="dataSource" 
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:tcp://localhost/~/test" />
    </bean>

我知道配置数据在另一个文件中有点奇怪,而我可以直接将其写入 bean 定义中。但它确实有效。

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
     <session-factory>
      <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
      <property name="hibernate.connection.username">sa</property>
      <property name="hibernate.connection.password"></property>
      <property name="hibernate.hbm2ddl.auto">update</property>
     </session-factory>
    </hibernate-configuration>

所以我正在尝试使用会话工厂做一些工作。它正在工作。我已连接到数据库,我可以使用会话工厂来保存或更新数据库或从数据库中获取内容。实施有效。

现在我正在尝试在另一个模块中使用相同的会话工厂。我使用 spring 将这个 mySessionFactory bean 注入其中。而且它不起作用。

然后我将 mySessionFactory bean 的范围更改为“原型”,这两个模块中的一切都运行良好。

现在我猜测问题是当 SessionFactory 是单例时,它不能在不同的模块中看到。下面是一个使用 SessionFactory 的方法示例:

public T fetchById(Class<T> p_type, Long p_id) {

        Session session;
        Transaction transaction;

        T returnT;

        session = sessionFactory.openSession();

        transaction = session.beginTransaction();

        returnT = (T) session.get(p_type, p_id);

        transaction.commit();
        session.close();

        return returnT;

    } 

我有几个,如果 mySessionFactory 是原型,它们都可以在整个应用程序中工作,所以我不认为这是一个依赖问题。但是,如果 mySessionFactory 是默认单例,则这些方法仅在定义 bean 的模块中有效。

我得到的例外是:

org.hibernate.service.UnknownServiceException:请求未知服务 [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]

我的问题是在整个应用程序中使用 SessionFactory 的单个实例的方法。我不想每次调用方法时都创建一个新的 SessionFactory。

我对 java 中的线程没有透彻的了解,所以我不确定是什么问题。我只能猜测。如果这个问题缺少信息,我很抱歉。

感谢您的帮助。

【问题讨论】:

    标签: spring hibernate session thread-safety sessionfactory


    【解决方案1】:

    两个选项;

    1.hibernate 单例会话生成器:

    使用单例设计模式并使用返回单例会话的静态方法创建一个类

    import org.hibernate.cfg.Configuration;
    import org.hibernate.Session;        
    import org.hibernate.SessionFactory;
    
    public class HibernateSession {
    
      static Configuration config;
      static SessionFactory sf;
      static Session session;
    
      public static Session getSession() {
    
        if(session==null){
    
          config=new Configuration();
          sf=config.configure().buildSessionFactory();
          session = sf.openSession();
        }
    
        return session;
      }
    }
    

    2.

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class SpringHibernateSession extends HibernateDaoSupport {   
    
      static SessionFactory sf;
      static Session session;
    
      public static Session getSession() {
    
        if(session==null){
    
             ApplicationContext appContext = new ClassPathXmlApplicationContext("path to the xml file with ur bean id mySessionFactory ");
    
             sf=(SessionFactory) appContext.getBean("mySessionFactory");
             session=sf.openSession();
    
        }
        return session;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 2019-01-22
      相关资源
      最近更新 更多