I am using tomcat-jdbc
pool in default spring-boot
setup. I would like to run some custom Java code each time new JDBC connection is established in the pool and before it is used for the first time. How to do it, and if there are several possibilities which one is the best?
相关问题
- Difference between Types.INTEGER and Types.NULL in
- java.lang.IllegalArgumentException: Cannot set to
- Spring Data MongoDB - lazy access to some fields
- Declaring an explict object dependency in Spring
- Decoding body parameters with Spring
相关文章
- java JDK动态代理和cglib动态代理最后获取的代理对象都为null的问题
- org.xml.sax.SAXParseException; lineNumber: 7; colu
- Java的JDBC可以返回HashMap吗?
- SpringMVC如何把File封装到Map中?
- Spring: controller inheritance using @Controller a
- How to load @Configuration classes from separate J
- Java spring framework - how to set content type?
- Java/Spring MVC: provide request context to child
To extend already accepted answer, you can use Spring AOP without full AspectJ if you use pointcut as this one:
Well, I can think of two options:
Create your own wrapper class - either by extending Tomcat's
DataSource
class or by implementing Java'sDataSource
interface and delegating to the wrappedDataSource
- and then add the logic you want to the desired methods and register a bean in a@Configuration
class by manually instantiating yourtomcat-jdbc
DataSource
(for examples on how to do so, refer to DataSourceConfiguration.Tomcat class) and wrapping it with your class.Create an aspect and use Spring's AOP support to intercept calls to
getConnection
. SinceDataSource
class is in the javax package, I think you'll have to use AspectJ, and for some examples refer to this linkMy suggestion would be to go with the first option, it should give you fewer headaches, here's a small example how you'd define your wrapper bean: