ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – Hibernate Sessionfactory重启|弹簧

2019-07-04 02:06:22  阅读:215  来源: 互联网

标签:java spring hibernate sessionfactory


我的要求如下:

我需要在我的Spring Web应用程序中频繁地重新启动(或重建)hibernate会话工厂,并使用我从外部获取的新HBM文件.

目前我的Sessionfactory类如下所示,使用SessionFactory Proxy来拦截’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

解决方法:

您可以关注SessionFactoryUtils以确定事务是否在Session工厂中发生,然后决定是否重新启动会话工厂:
您需要导入 – >你文件中的org.springframework.orm.hibernate.SessionFactoryUtils,并使用以下API.

    static boolean  hasTransactionalSession(SessionFactory sessionFactory); 

以上API返回当前线程是否存在事务性Hibernate会话,即Spring的事务工具绑定到当前线程的Session.如果您需要检查会话是否在会话中是事务性的,还有另一个API工厂目前:

static boolean isSessionTransactional(Session session,SessionFactory sessionFactory);

上面的API返回给定的特定Hibernate会话是否是事务性的,即Spring的事务工具绑定到当前线程.

标签:java,spring,hibernate,sessionfactory
来源: https://codeday.me/bug/20190704/1372323.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有