【发布时间】:2017-12-31 22:12:39
【问题描述】:
我在 MySQL 中使用多租户模式,
class SchemaPerTenantConnectionProvider : MultiTenantConnectionProvider {
@Autowired
private lateinit var dataSource: DataSource
@Throws(SQLException::class)
override fun getAnyConnection() = this.dataSource.connection
@Throws(SQLException::class)
override fun releaseAnyConnection(connection: Connection) {
connection.close()
}
@Throws(SQLException::class)
override fun getConnection(tenantIdentifier: String): Connection {
val connection = this.anyConnection
try {
connection.createStatement().execute("USE $tenantIdentifier ")
} catch (e: SQLException) {
throw SQLException("Could not alter JDBC connection to schema [$tenantIdentifier]")
}
return connection
}
...
}
我的连接池大小是 10,现在如果任何无效的tenantIdentifier 被传递 10 次,则 10 个连接被耗尽,之后该应用程序无法获取任何连接。
尝试投掷 Exception, HibernateException 并没有帮助。使用default schema 的连接将获取错误的结果。有没有办法在getConnection() 中处理这种情况,而不是耗尽连接限制?
【问题讨论】:
-
我希望你也覆盖
public void releaseConnection(String tenantIdentifier, Connection connection)并在此处关闭连接
标签: spring hibernate spring-boot multi-tenant