简体   繁体   中英

Spring 2.x multiple datasources and p6spy

I've created a multiple datasource setup with spring-boot. It looks like this:

application.yaml:

app:
   db1:
      jdbc-url:...
      username:...
      password:...
   db2:...

Config-class:

@Configuration(proxyBeanMethods = false)
public class DataSourceConfig {

        @Bean(name = "db1")
        @Primary
        @ConfigurationProperties("app.db1")
        public DataSource statusServiceDataSource(){
                return DataSourceBuilder.create().build();
        }

        @Bean(name = "db2")
        @ConfigurationProperties("app.db2")
        public DataSource identifierResolverServiceDataSource(){
                return DataSourceBuilder.create().build();
        }

I use the datasource this way:

@Autowired
@Qualifier("db2")
DataSource db2;

Until here everything works fine. Now I wanted to include p6spy depending on a value of the properties, but I get different errors.

First approach:

return new P6DataSource(DataSourceBuilder.create().build());

leads to:

java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.

Second approach:

return DataSourceBuilder.create().type(P6DataSource.class).build();

leads to:

P6DataSource: no value for Real Data Source Name, cannot perform jndi lookup

Looking at the P6Spy docs, I thought my approach was enough, but It looks like it isn't. What can I do to solve the problem? Do I really have to go the whole way with defining jndi/lookUp names for my 7+ DataSources?

Also when looking up my datasource properties in application.yaml I get an error from IntelliJ saying "Cannot resolve configuration property", which is strange, as the app works just fine with all datasources without p6spy.

I'm working in a similar solution. I couldn't use the spring boot autoconfiguration so... first thing to do is to add your maven dependency for p6spy. Then, you need a psy.properties where you could add your driver to driverlist like this (DB2 example)

driverlist=com.ibm.db2.jcc.DB2Driver

I had troubles at first using P6DataSource, so I tried the url + driver approach in order to test the library configurations.

Once the url + driver approach worked for me, I returned to P6DataSource

https://p6spy.readthedocs.io/en/latest/integration.html

Maybe you could try it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM