Hibernate Sessionfactory 重启 |春天

问题描述 投票:0回答:1

我的要求如下:

我需要使用从外部获得的新 HBM 文件频繁地在 Spring Web 应用程序中重新启动(或重建)休眠会话工厂。

目前我的 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);
         }
      }
   }

谢谢, 阿帕萨米

java spring hibernate sessionfactory
1个回答
2
投票

您可以通过 SessionFactoryUtils 来判断 Session Factory 中是否正在发生事务,然后决定是否重新启动 Session Factory: 您需要在文件中导入--> org.springframework.orm.hibernate.SessionFactoryUtils,并使用以下API。

static boolean  hasTransactionalSession(SessionFactory sessionFactory); 

上面的 API 返回当前线程是否存在事务性 Hibernate Session,即通过 Spring 的事务工具绑定到当前线程的 Session。还有另一个 API,以防万一您需要检查会话是否是事务性的当前会话工厂:

static boolean isSessionTransactional(Session session,SessionFactory sessionFactory);

上面的 API 返回给定的特定 Hibernate Session 是否是事务性的,即通过 Spring 的事务工具绑定到当前线程。

© www.soinside.com 2019 - 2024. All rights reserved.