栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在运行时休眠多租户创建架构

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在运行时休眠多租户创建架构

我想出了解决我的问题的解决方案。我希望这对外面的人有用。

因此,主要问题归结为Hibernate的限制,即在多租户配置中在运行时为新客户端创建模式。

“ Hibernate在多租户环境中不支持自动模式导出。”

为了解决此限制(使用Spring),我的解决方案是创建一个新的LocalSessionFactoryBean,将其配置为不支持多租户。所以基本上我有两个LocalSessionFactoryBeans。

  1. 用于多租户会话的多租户LocalSessionFactoryBean
  2. 非多租户LocalSessionFactoryBean,用于使用spring文件中的配置为租户创建架构。

弹簧配置

<!-- Multi-tenant SessionFactory --><bean id="sessionFactory"    >    <property name="dataSource" ref="dataSource" />    <property name="hibernateProperties">        <map> <entry key="hibernate.dialect" value="${hibernate.dialect}" /> <entry key="hibernate.hbm2ddl.auto" value="NONE" /> <!-- Multi-tenancy support --> <entry key="hibernate.multiTenancy" value="SCHEMA" /> <entry key="hibernate.tenant_identifier_resolver" value="${hibernate.tenant_identifier_resolver}" /> <entry key="hibernate.multi_tenant_connection_provider" value-ref="multiTenantConnectionProvider" />        </map>    </property>    <property name="mappingResources">        <list> <COMMON SCHEMA MAPPING RESOURCES />        </list>    </property></bean><!-- SessionFactory capable of managing multi-tenant schemas --><bean id="sessionFactorySchemaManager"    >    <property name="dataSource" ref="dataSource" />    <property name="hibernateProperties">        <map> <entry key="hibernate.dialect" value="${hibernate.dialect}" /> <entry key="hibernate.hbm2ddl.auto" value="CREATE" /> <!-- Multi-tenancy support --> <entry key="hibernate.multiTenancy" value="NONE" />        </map>    </property>    <property name="mappingResources">        <list> <TENANT SPECIFIC SCHEMA MAPPING RESOURCES />        </list>    </property></bean>

用于创建架构的代码

public boolean createSchema(final String tenantId) throws SQLException {    boolean result = false;    if(_configuration != null && _dataSource != null) {        // Get a local configuration to configure        final Configuration tenantConfig = _configuration;        // Set the properties for this configuration        Properties props = new Properties();        props.put(Environment.DEFAULT_SCHEMA, tenantId);        tenantConfig.addProperties(props);        // Get connection        Connection connection = DriverManager.getConnection(_dataSource.getUrl(),      _dataSource.getUsername(), _dataSource.getPassword());        // Create the schema        connection.createStatement().execute("CREATE SCHEMA " + tenantId + "");        // Run the schema update from configuration        SchemaUpdate schemaUpdate = new SchemaUpdate(tenantConfig);        schemaUpdate.execute(true, true);        // Set the schema        connection.createStatement().execute("SET SCHEMA " + tenantId + "");        // Set the result        result = true;    } else if(_configuration == null) {        if(_LOGGER.isWarnEnabled()) { _LOGGER.warn("No configuration was specified for " + getClass().getSimpleName());        }    } else if(_dataSource == null) {        if(_LOGGER.isWarnEnabled()) { _LOGGER.warn("No dataSource was specified for " + getClass().getSimpleName());        }    }    return result;}

请注意,此代码中的_configuration来自Non-Tenant LocalSessionFactoryBean



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/446338.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号