postgresql - 嵌套异常是 org.hibernate.exception.SQLGrammarException: could not extract ResultSet
问题描述
当我在代码中引入基于模式的多租户时出现此错误。我正在使用 spring boot、hibernate 和 postgresql。我已经通过 JHipster 生成了初始代码,并尝试在生成的代码之上添加多租户功能。这是我添加的文件**
TenantConnectionProvider 类
@Component
public class TenantConnectionProvider implements MultiTenantConnectionProvider {
private static Logger logger = LoggerFactory.getLogger(TenantConnectionProvider.class);
private String DEFAULT_TENANT = "public";
private DataSource datasource;
public TenantConnectionProvider(DataSource dataSource) {
this.datasource = dataSource;
}
@Override
public Connection getAnyConnection() throws SQLException {
return datasource.getConnection();
}
@Override
public void releaseAnyConnection(Connection connection) throws SQLException {
connection.close();
}
@Override
public Connection getConnection(String tenantIdentifier) throws SQLException {
logger.info("Get connection for tenant {}", tenantIdentifier);
final Connection connection = getAnyConnection();
connection.setSchema(tenantIdentifier);
return connection;
}
@Override
public void releaseConnection(String tenantIdentifier, Connection connection) throws SQLException {
logger.info("Release connection for tenant {}", tenantIdentifier);
connection.setSchema(DEFAULT_TENANT);
releaseAnyConnection(connection);
}
}
休眠配置
@Configuration
public class HibernateConfig {
@Autowired
private JpaProperties jpaProperties;
@Bean
JpaVendorAdapter jpaVendorAdapter() {
return new HibernateJpaVendorAdapter();
}
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(
DataSource dataSource,
MultiTenantConnectionProvider multiTenantConnectionProviderImpl,
CurrentTenantIdentifierResolver currentTenantIdentifierResolverImpl
) {
Map<String, Object> jpaPropertiesMap = new HashMap<>(jpaProperties.getProperties());
jpaPropertiesMap.put(Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA);
jpaPropertiesMap.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProviderImpl);
jpaPropertiesMap.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolverImpl);
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("org.confiz*");
em.setJpaVendorAdapter(this.jpaVendorAdapter());
em.setJpaPropertyMap(jpaPropertiesMap);
return em;
}
}
这是 TenantSchemaResolver 类
@Component
public class TenantSchemaResolver implements CurrentTenantIdentifierResolver {
private String defaultTenant ="public";
@Override
public String resolveCurrentTenantIdentifier() {
String t = TenantContext.getCurrentTenant();
if(t!=null){
return t;
} else {
return defaultTenant;
}
}
@Override
public boolean validateExistingCurrentSessions() {
return true;
}
}
租户上下文
public class TenantContext {
private static ThreadLocal<String> currentTenant = new InheritableThreadLocal<>();
public static String getCurrentTenant() {
return currentTenant.get();
}
public static void setCurrentTenant(String tenant) {
currentTenant.set(tenant);
}
public static void clear() {
currentTenant.set(null);
}
}
解决方案
推荐阅读
- r - ggplot facet y 垂直包裹
- java - 如何将已从 JWT 中排除的权限传递给端点的参数
- paypal - 卖家账户下的 PayPal 订阅计划(市场)
- c# - userInputsgirl 被称为变量,即使它是一个类
- symfony5 - 用 symfony 更新我的控制器中的数据库
- google-cloud-firestore - 通过 Firestore 将数据从 BigQuery 公开到移动/Web 应用程序
- java - 由编译器完成的 Java try-with-resource 实现
- javascript - 如何将 Json 数据从 python 发送到 javascipt
- python - 如何让程序不断重复,直到我输入停止它的特定数据?
- python - 不匹配值的 Pandas 系列和 Nan 值