Hibernate Sessionfactory 重启 | Spring

4
我的要求如下:
我需要在我的Spring Web应用程序中以频繁的间隔使用来自外部的新HBM文件重新启动(或重建)Hibernate会话工厂。
目前,我的SessionFactory类如下所示,其中包含一个SessionFactory代理来拦截“OpenSession”调用。
在那里,我正在检查一个条件以重新启动和重建sessionFactory。
我的问题在于,在并发环境中,正在进行其他事务的其他用户在此重新启动期间受到影响。
是否有任何方式通过检查所有交易和打开的会话来执行重启,并在所有其他交易完成后进行重建会话工厂?
还是存在任何其他解决方案。
代码:
public class DataStoreSessionFactory extends LocalSessionFactoryBean
{


   private boolean restartFactory = false;



   @Override
   protected void postProcessConfiguration(Configuration config) throws HibernateException
   {
      super.postProcessConfiguration(config);
      updateHBMList(config);
   }


   private void updateHBMList(final Configuration config)
   {

      config.addXML(modelRegistry.generateMapping());
   }

   @Override
   public SessionFactory getObject()
   {

      Object obj = super.getObject();

      /*
       * Invocation handler for the proxy
       */
      SessionFactoryProxy proxy = new SessionFactoryProxy(this, (SessionFactory) obj);

      /**
       * All the methods invoked on the returned session factory object will pass through this proxy's invocation
       * handler
       */
      SessionFactory sessionFactory = (SessionFactory) Proxy.newProxyInstance(getClass().getClassLoader(),
                                                                              new Class[] { SessionFactory.class },
                                                                              proxy);
      return sessionFactory;
   }

   static class SessionFactoryProxy implements InvocationHandler
   {


      private SessionFactory sessionFactory;

      private LocalSessionFactoryBean factoryBean;

      public SessionFactoryProxy(LocalSessionFactoryBean factoryBean, SessionFactory sessionFactory)
      {
         this.factoryBean = factoryBean;
         this.sessionFactory = sessionFactory;
      }

      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
      {
         /**
          * Only if the method invoked is openSession - check if the session factory should be restarted, and only then
          * invoke the requested method
          */
         if (method.getName().equals("openSession"))
         {
            restartSessionFactoryIfNecessary();
         }
         return method.invoke(sessionFactory, args);
      }

      private void restartSessionFactoryIfNecessary()
      {
         restartSessionFactory();
         /*if (((DataStoreSessionFactory) factoryBean).isRestartFactory())
         {
            restartSessionFactory();
         }*/
      }

      private synchronized void restartSessionFactory()
      {
         log.info("Restarting session...");
         factoryBean.destroy();
         try
         {
            factoryBean.afterPropertiesSet();
            sessionFactory = factoryBean.getObject();
         }
         catch (Exception e)
         {
            log.error("Error while restarting session: " + e.getMessage());
            throw new RuntimeException(e);
         }
      }
   }

感谢您,Appasamy。

为什么不在非工作时间重建您的会话工厂? - Sajan Chandran
哎呀,你的应用程序设计架构真是有点恶心啊。在运行时更改 hibernate/database 配置并不美观... - pap
1个回答

2
您可以使用SessionFactoryUtils来确定是否在Session工厂中进行事务,并决定是否重新启动Session工厂: 您需要在文件中导入--> org.springframework.orm.hibernate.SessionFactoryUtils,并使用以下API。
    static boolean  hasTransactionalSession(SessionFactory sessionFactory); 

以上API返回当前线程是否存在事务性Hibernate会话,即由Spring事务设施绑定到当前线程的会话。如果需要检查会话工厂中的会话是否具有事务处理,则还有另一个API:

static boolean isSessionTransactional(Session session,SessionFactory sessionFactory);

以上API返回给定的Hibernate会话是否是事务性的,也就是是否由Spring的事务机制绑定到当前线程。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接