首先,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; }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)