在Spring Boot中的每个数据库连接的开头运行SQL语句 [英] Run SQL statement at beginning of each DB connection in Spring Boot
问题描述
如何在使用Spring Boot获取数据库连接后直接运行自定义SQL语句?
How can I run a custom SQL statement directly after obtaining a DB connection with Spring Boot?
每次建立新连接时都需要运行SQL。
The SQL needs to be run each time a new connection is established.
该解决方案应该与Spring Boot默认的DataSource实现一起使用(我认为它是一个Tomcat池数据源)。
The solution should work with the Spring Boot default DataSource implementation (which I think is a Tomcat pooling data source).
声明是什么并不重要,但在我的情况下它将是 ALTER SESSION SET CURRENT_SCHEMA = xxxx
It doesn't really matter what the statement is, but in my case it will be ALTER SESSION SET CURRENT_SCHEMA=xxxx
推荐答案
Tomcat Jdbc连接池有一个参数initSQL https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html
Tomcat Jdbc Connection pool has a parameter "initSQL" https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html
它看起来您可以在.properties文件中配置每个池参数(@see https://stackoverflow.com/a/25573035/280244 )
It looks like you can configure each pool parameter in your .properties file (@see https://stackoverflow.com/a/25573035/280244)
所以试一试,
Spring Bo ot之前1.4:
Spring Boot before 1.4:
spring.datasource.initSQL=ALTER SESSION SET CURRENT_SCHEMA=xxxx
Spring Boot 1.4或更高版本:
Spring Boot 1.4 or later:
spring.datasource.tomcat.initSQL=ALTER SESSION SET CURRENT_SCHEMA=xxxx
这篇关于在Spring Boot中的每个数据库连接的开头运行SQL语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!