使用Hibernate 4.2和Spring 3.1.1设置MultiTenantConnectionProvider

使用Hibernate 4.2和Spring 3.1.1设置MultiTenantConnectionProvider,第1张

使用Hibernate 4.2和Spring 3.1.1设置MultiTenantConnectionProvider

首先,Hibernate“实例化… MULTI_TENANT_CONNECTION_PROVIDER和MULTI_TENANT_IDENTIFIER_RESOLVER引用的类”是不对的。Hibernate首先尝试将这些设置视为其预期类型的​​对象((用于MULTI_TENANT_CONNECTION_PROVIDER的MultiTenantConnectionProvider和用于MULTI_TENANT_IDENTIFIER_RESOLVER的CurrentTenantIdentifierResolver。

因此,只需将你的bean直接传递,即可根据需要进行配置。

我只是听从他的建议,并设法使它可行。

这是

CurrentTenantIdentifierResolver
定义为Spring Bean的:

@Component@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)public class RequestURITenantIdentifierResolver implements CurrentTenantIdentifierResolver {    @Autowired    private HttpServletRequest request;    @Override    public String resolveCurrentTenantIdentifier() {        String[] pathElements = request.getRequestURI().split("/");        String tenant = pathElements[1];        return tenant;    }    @Override    public boolean validateExistingCurrentSessions() {        return true;    }}

这是

MultiTenantConnectionProvider
定义为Spring Bean的:

@Componentpublic class SchemaPerTenantConnectionProviderImpl implements MultiTenantConnectionProvider {    @Autowired    private DataSource dataSource;    @Override    public Connection getAnyConnection() throws SQLException {        return dataSource.getConnection();    }    @Override    public void releaseAnyConnection(final Connection connection) throws SQLException {        connection.close();    }    @Override    public Connection getConnection(final String tenantIdentifier) throws SQLException {        final Connection connection = getAnyConnection();        try { connection.createStatement().execute("USE " + tenantIdentifier);        } catch (SQLException e) { throw new HibernateException("Could not alter JDBC connection to specified schema [" + tenantIdentifier + "]",        e);        }        return connection;    }    @Override    public void releaseConnection(final String tenantIdentifier, final Connection connection) throws SQLException {        try { connection.createStatement().execute("USE dummy");        } catch (SQLException e) { // on error, throw an exception to make sure the connection is not returned to the pool. // your requirements may differ throw new HibernateException(         "Could not alter JDBC connection to specified schema [" +      tenantIdentifier + "]",         e );        } finally { connection.close();        }    }    @Override    public boolean supportsAggressiveRelease() {        return true;    }    @Override    public boolean isUnwrappableAs(Class aClass) {        return false;    }    @Override    public <T> T unwrap(Class<T> aClass) {        return null;    }}

最后,这是

LocalContainerEntityManagerFactoryBean
利用上述两个组件的连接:

@Configurationpublic class HibernateConfig {    @Bean    public JpaVendorAdapter jpaVendorAdapter() {        return new HibernateJpaVendorAdapter();    }    @Bean    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,     MultiTenantConnectionProvider multiTenantConnectionProvider,     CurrentTenantIdentifierResolver tenantIdentifierResolver) {        LocalContainerEntityManagerFactoryBean emfBean = new LocalContainerEntityManagerFactoryBean();        emfBean.setDataSource(dataSource);        emfBean.setPackagesToScan(VistoJobsApplication.class.getPackage().getName());        emfBean.setJpaVendorAdapter(jpaVendorAdapter());        Map<String, Object> jpaProperties = new HashMap<>();        jpaProperties.put(org.hibernate.cfg.Environment.MULTI_TENANT,    MultiTenancyStrategy.SCHEMA);        jpaProperties.put(org.hibernate.cfg.Environment.MULTI_TENANT_CONNECTION_PROVIDER,    multiTenantConnectionProvider);        jpaProperties.put(org.hibernate.cfg.Environment.MULTI_TENANT_IDENTIFIER_RESOLVER,    tenantIdentifierResolver);        emfBean.setJpaPropertyMap(jpaProperties);        return emfBean;    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5014381.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存