【问题标题】:Spring validation-query for multi-tenant system多租户系统的 Spring 验证查询
【发布时间】:2018-12-29 14:54:16
【问题描述】:

我有一个在 MySQL 数据库上运行的多租户 Spring Boot 应用程序,为各个租户提供单独的架构。 dataSource 是通过在运行时传递标头代码来决定的。如何确保连接不会在这样的系统中掉线?一旦系统的不活动时间超过了设定的时间,“SELECT 1”命令就需要运行,并且没有向系统指定要连接的数据源的任何内容,因此系统会抛出 sql 异常。

这是我的配置属性:

spring.datasource.url=jdbc:mysql://xxxxxxxx/xxx?useUnicode=true&characterEncoding=utf8&useSSL=false&autoReconnect=true spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57InnoDBDialect spring.jpa.database=MYSQL spring.jpa.hibernate.ddl-auto=验证 spring.datasource.test-on-borrow=true spring.datasource.testWhileIdle = true spring.datasource.timeBetweenEvictionRunsMillis = 60000 spring.datasource.validation-query=SELECT 1 spring.datasource.tomcat.max-wait=10000 spring.datasource.tomcat.max-active=50 spring.datasource.tomcat.test-on-borrow=true spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext hibernate.c3p0.max_size=20 hibernate.c3p0.min_size=5 hibernate.c3p0.idle_test_period=600

【问题讨论】:

  • 希望我能看到你的配置文件。 1.检查配置文件中的超时,理想时间属性。 2. 解决方法 当新查询出现时,它应该先尝试建立连接,然后执行查询,但理想情况下,有一个正确配置的连接池可以在我们的生产服务器中工作。

标签: java spring connection


【解决方案1】:

我发现只要我们有hibernate.multiTenancy=DATABASE设置,application.properties文件中指定的数据源验证查询设置将没有任何效果,因为我们需要拦截@987654322 @ 以编程方式调用并解析“数据源”租户。因此,我使用扩展的CustomMultiTenantConnectionProviderImpl 解决了它,它实现了MultiTenantConnectionProvider 接口并覆盖了getConnectionreleaseConnection 方法的定义来完成需要做的事情。现在我的连接正在得到正确验证。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多