Run SQL statement at beginning of each DB connection in Spring Boot

ε祈祈猫儿з 提交于 2019-12-03 21:53:33

问题


How can I run a custom SQL statement directly after obtaining a DB connection with Spring Boot?

The SQL needs to be run each time a new connection is established.

The solution should work with the Spring Boot default DataSource implementation (which I think is a Tomcat pooling data source).

It doesn't really matter what the statement is, but in my case it will be ALTER SESSION SET CURRENT_SCHEMA=xxxx


回答1:


Tomcat Jdbc Connection pool has a parameter "initSQL" https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html

It looks like you can configure each pool parameter in your .properties file (@see https://stackoverflow.com/a/25573035/280244)

So give a try,

Spring Boot before 1.4:

spring.datasource.initSQL=ALTER SESSION SET CURRENT_SCHEMA=xxxx

Spring Boot 1.4 or later:

spring.datasource.tomcat.initSQL=ALTER SESSION SET CURRENT_SCHEMA=xxxx


来源:https://stackoverflow.com/questions/38793953/run-sql-statement-at-beginning-of-each-db-connection-in-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!