0 Replies Latest reply on Apr 27, 2016 10:43 AM by Mohamed Yousfi

    probleme with "connection.provider_class" and hibernate search for Multi-tenancy connection

    Mohamed Yousfi Newbie

      Hello

      We use "hibernate.connection.provider_class" in multitenant connection with hibernate 4.0 and we try to integrate hibernate search 4.1.1.Final for our application, But the problem is when we try to purge all of the database, we can not get the session with the exact tenantid with this code " FullTextSession fullTextSession = org.hibernate.search.Search.getFullTextSession(hibernateManagement.getHibernateSession());"

       

       

      because ""hibernateManagement.getHibernateSession()" does not  call the "connection.provider_class" to get the exact session with tenant id so we purge all the provider_database  instead of the client database

       

       

       

       

       

      So there is any way to get the session with the tenantid by passing through the "hibernate.connection.provider_class" because we cannot integrate such code ?

           "  <prop key="hibernate.multiTenancy">DATABASE</prop>

      <prop key="hibernate.tenant_identifier_resolver">factory.MultiTenantIdentifierResolver</prop>

      <prop key="hibernate.multi_tenant_connection_provider">factory.MultiTenantConnectionProvider</prop> "

      because it has a lot of influence in the rest of the application.

       

       

       

       

      So Can we do this by getting the session of this property "<property name="hibernate.connection.provider_class" value="com.application.resources.controller.ConnectionProvider" />" to build the

      FullTextSession?

      our   com.application.resources.controller.ConnectionProvider  Connectionmethode is :

      public Connection getConnection() throws SQLException {

         try {

       

        String tenantId = null;

       

        Connection connection;

        GestionAffichageBean gestionAffichageBean=null;

        String host="";

         boolean distantConnection =false;

       

         if (FacesContext.getCurrentInstance() != null) {

       

        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);

       

         if (session != null && session.getAttribute("tenantId") != null) {

       

        tenantId = (String) session.getAttribute("tenantId");

        }

        } else if (InjectionService.getBeanManager() != null) {

         if(gestionAffichageBean==null) {

        gestionAffichageBean = (GestionAffichageBean) InjectionService.getBeanByName("gestionaffichage");

        }

        

        tenantId = gestionAffichageBean.getTenantId();

        } else {

        tenantId = AppUtil.threadTenantId.get(Thread.currentThread().getId());

        }

       

         if (gestionAffichageBean!=null && InjectionService.getBeanManager() != null){

        

        distantConnection=gestionAffichageBean.isDistantConnection();

         if(gestionAffichageBean.getHostIP()!=null)

        host=gestionAffichageBean.getHostIP();

        

        }

        

         if(distantConnection && !host.equals("") && !(host.equals("localhost")|| host.equals("127.0.0.1")) ){

       

        System.out.println("getConnection() distantConnection => " + tenantId);

       

        Class.forName("com.mysql.jdbc.Driver");

        Properties prop = new Properties();

       

        String jdbcUrl;

       

        prop.put("user", "");

        prop.put("password", "");

        jdbcUrl="jdbc:mysql://"+host+":3306/";

       

        connection = DriverManager.getConnection(jdbcUrl, prop);

        connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

       

        }else{

        javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:jboss/datasources/" + dataSourceNameDefault);

        connection = ds.getConnection();

        }

        

       

         if (tenantId != null) {

        connection.setCatalog(tenantId);

        }

        

        tenantId = null;

        

         return connection;

        

       

        } catch (Exception e) {

         throw new SQLException(e.getMessage());

        }

      }