spring mvc hibernate creating and manipulating sessionfactorys of several databases

一个人想着一个人 提交于 2019-12-12 01:09:18

问题


here I have a problem that I want to find a solution here, I work on a project using spring mvc and hibernate in my project I have several databases with same architecture (database by company) and a database for authentication, when the user login i have to generates a SessionFactory that corresponds to database of the user company, can you help me by offering me an architecture and give me some examples please, sorry for my English.


回答1:


Depending on which hibernate version you use you can just utilize the built in multi tenant features of hibernate. For that you need to implement the CurrentaTenantIdentifierResolver and MultiTenantConnectionProvider following this documentationthis documentation

the following hibernateProperties have to be set additionaly to use them:

                hibernate.multiTenancy=DATABASE
                hibernate.tenant_identifier_resolver=xxx.xxx.CurrentTenantIdentifierResolverImpl
                hibernate.multi_tenant_connection_provider=xxx.xxx.MultiTenantConnectionProviderImpl

Sample Implementation: CurrentTenantIdentifier:

import javax.faces.context.FacesContext;

import org.hibernate.context.spi.CurrentTenantIdentifierResolver;

public class CurrentTenantIdentifierResolverImpl implements CurrentTenantIdentifierResolver {



    @Override
        public String resolveCurrentTenantIdentifier() {
            if (SecurityContextHolder.getContext().getAuthentication().getPrincipal().yourOwnRoutineToDetermineCompanyxxx=="companyxxx"){
                return "companyxxx";

            } else  if ... furhter companies{
                ...
            }
        }

        @Override
        public boolean validateExistingCurrentSessions() {
            return true;
        }

    }

MultiTenantConnectionProvider:

public class MultiTenantConnectionProviderImpl extends AbstractMultiTenantConnectionProvider {
    @Autowired
    private DataSoruce DScompany02;
    @Autowired
    private DataSource DScompany01;
    @Autowired
    private DataSource default;

    @Override
    protected ConnectionProvider getAnyConnectionProvider() {
        return new DatasourceConnectionProvider(default);
    }

    @Override
    protected ConnectionProvider selectConnectionProvider(String tenantIdentifier) {
         if (tenantidentifer == "company01"){
        return new DatasourceConnectionProvider(DScompany01);
         } repeat....
    }
}

now just define a datasource per company DB so it can be swapped by the multitenantconnectionprovider.

A call to the sessionFactory in the DAO can be made by using SessionFactory.getCurrentSession() or in special cases by using sessionFactory.withOptions().tenantIdentifier("company01").openSession()

This should cover the basics but might need some tayloring to your application.



来源:https://stackoverflow.com/questions/16768921/spring-mvc-hibernate-creating-and-manipulating-sessionfactorys-of-several-databa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!