Spring Boot integration problem

791 views
Skip to first unread message

rustem.y...@gmail.com

unread,
Dec 25, 2016, 1:16:05 PM12/25/16
to p6spy...@googlegroups.com

 

Dear p6spy contributors!

 

I’ve been trying for few days to integrate p6spy into spring boot application without any success.

Hope you can help me. As I already mentioned I have a spring boot application (v1.4.1) with hibernate (v4.3.11.FINAL), h2 memory database (v.1.4.192) and p6spy (v3.0.0).

 

My configuration files are given below.

application.yml

spring:
    application:
        name:
persistence
   
datasource:
        url:
jdbc:p6spy:h2:mem:persistence;DB_CLOSE_DELAY=-1
       
username: sa
       
password:
        driver-class-name:
com.p6spy.engine.spy.P6SpyDriver #org.h2.Driver
   
jpa:
        database-platform:
kz.dits.persistence.domain.util.FixedH2Dialect
       
database: H2
       
open-in-view: false
       
hibernate:
            ddl-auto:
create-drop
           
naming:
                strategy:
org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy

 

spy.properties

driverlist=org.h2.Driver

 

As I understood from docs such configurations should create spy.log file inside project directory. However I don’t see any log files.

 

I tried to override appender in spy.properties file

driverlist=org.h2.Driver
appender=com.p6spy.engine.spy.appender.StdoutLogger

 

But I don’t see sql parameters in sql queries. All I see are standard hibernate logs

Hibernate: insert into its_vehicle_group (created_by, created_date, last_modified_by, last_modified_date, code, coefficient, description, id) values (?, ?, ?, ?, ?, ?, ?, ?)
 
Hibernate: select vehiclegro0_.id as id1_15_0_, vehiclegro0_.created_by as created_2_15_0_, vehiclegro0_.created_date as created_3_15_0_, vehiclegro0_.last_modified_by as last_mod4_15_0_, vehiclegro0_.last_modified_date as last_mod5_15_0_, vehiclegro0_.code as code6_15_0_, vehiclegro0_.coefficient as coeffici7_15_0_, vehiclegro0_.description as descript8_15_0_ from its_vehicle_group vehiclegro0_ where vehiclegro0_.id=?

 

I would appreciate any help.

 

Best regards,

Rustem Yessekeyev

 

P.S Sorry for my English. Not my first or second language

 

Sent from Mail for Windows 10

 

butk...@gmail.com

unread,
Dec 26, 2016, 5:38:43 PM12/26/16
to p6spy users
Hey Rustem,

I was wondering, how well does p6spy integrate with spring boot, so gave it a try and created repo: https://github.com/p6spy/p6spy-it-spring-boot

Not sure why it didn't work for you, as your approach seems to be OK in general. I just:
* took jpa sample form: https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-jpa
* used gradle for build
* took your application.yml and simplified it a bit
* (as a bonus step) I also created spy.properties to see if specific properties are correctly loaded

and things work just fine, even the custom property set via spy.properties is considered in the output.
Feel free to give it a try with that one and compare with your situation.

Kind regards,
Peter Butkovic

--
You received this message because you are subscribed to the Google Groups "p6spy users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to p6spy-users+unsubscribe@googlegroups.com.
To post to this group, send email to p6spy...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/p6spy-users/58600ce3.879e190a.31dab.e8e3%40mx.google.com.
For more options, visit https://groups.google.com/d/optout.

Quinton McCombs

unread,
Dec 28, 2016, 2:41:22 PM12/28/16
to p6spy users
The easiest way to integrate into a spring application is to wrap the DataSource.  


With Spring Boot that means that you will have to manually configure the data source instead of relying on auto configuration though.

--

jorg.h...@gmail.com

unread,
Jul 26, 2017, 7:26:59 AM7/26/17
to p6spy users
Reviving this old thread because i am faced with the same issue.

I am using HikariCP as datasource, in a spring-boot application. I figured i could just use a BeanPostProcessor to wrap as a P6DataSource:


  @Bean
  @Order(Ordered.HIGHEST_PRECEDENCE)
  BeanPostProcessor dataSourcePostProcessor() {

    return new BeanPostProcessor() {
      @Override
      public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return wrapDataSource(bean);
      }

      @Override
      public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
      }

      Object wrapDataSource(Object bean) {
        if (bean instanceof DataSource) {
          return new P6DataSource((DataSource) bean);
        }
        return bean;
      }
    };
  }

but the instanceOf check never passes. If i use the postProcessAfterInitialization then it seems to get wrapped, but the problem is that by this time Spring already created most of the application context beans (including all of my MappingSqlQuery classes that get the plain DataSource injected). 

So failing to get BPP to work I then tried to make hikaricp link to the p6driver and datasource but didn't get anywhere. Has anyone done this, is it supported ?

Thanks,
Jorg


To unsubscribe from this group and stop receiving emails from it, send an email to p6spy-users...@googlegroups.com.

Quinton McCombs

unread,
Jul 26, 2017, 5:48:00 PM7/26/17
to p6spy users
Have you tried just wrapping the data source in your config instead of using a BPP? The example at https://stackoverflow.com/questions/14706029/usage-of-p6spy-with-datasource-in-spring-applicationcontext-xml is for XMl configuration but the same approach works for java config as well.

To unsubscribe from this group and stop receiving emails from it, send an email to p6spy-users+unsubscribe@googlegroups.com.

To post to this group, send email to p6spy...@googlegroups.com.

Jorg Heymans

unread,
Jul 27, 2017, 4:51:10 AM7/27/17
to p6spy users
This turned out quite hard to do as well, with circular references all over the place. I suspect the datasource autoconfiguration is getting in the way but i don't have time to look into it further now. 

The funny thing is that for another (much smaller) spring boot application, th BPP works fine and is invoked correctly very early when the application context is still initializing, so that all subsequent repository beans get the P6 datasource. 

Jorg


On Wednesday, July 26, 2017 at 11:48:00 PM UTC+2, Quinton McCombs wrote:
Have you tried just wrapping the data source in your config instead of using a BPP? The example at https://stackoverflow.com/questions/14706029/usage-of-p6spy-with-datasource-in-spring-applicationcontext-xml is for XMl configuration but the same approach works for java config as well.

Jorg Heymans

unread,
Jul 27, 2017, 6:37:44 AM7/27/17
to p6spy users
In the end i traced it down to the fact that a bean was causing the datasource to be very-eagerly instantiated, before the BPP was known to the application context. Adding a depends-on from this application bean to the BPP made it work.

Thanks,
Jorg

Quinton McCombs

unread,
Jul 27, 2017, 10:05:22 PM7/27/17
to p6spy users
Oh, Spring Boot autoconfiguration is the reason for the BPP. That makes sense now. Is the code that you submitted in the prior message still correct for the BPP? If so, do you mind if we include it in our documentation (and perhaps in an autoconfig module)?

--
You received this message because you are subscribed to the Google Groups "p6spy users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to p6spy-users+unsubscribe@googlegroups.com.

To post to this group, send email to p6spy...@googlegroups.com.

Jorg Heymans

unread,
Jul 28, 2017, 4:34:44 AM7/28/17
to p6spy users
Sure no problem. The correct bpp would be this:

 @Bean
 BeanPostProcessor dataSourcePostProcessor() {

    return new BeanPostProcessor() {
     @Override
     public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
       return bean;
     }

      @Override
     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
       return wrapDataSource(bean);
     }

      Object wrapDataSource(Object bean) {
       if (bean instanceof DataSource) {
         return new P6DataSource((DataSource) bean);
       }
       return bean;
     }
   };
 }

It's just another way to get it to work, because i use HikariCP and jdbc autoconfig this seemed to me the least invasive with whatever spring boot does. In case of more complex applications, it's possible that other beans will trigger the datasource to be initialized before Spring sees the BPP, in which case the BPP will be applied very late and it doesn't work. In that case you'll need to add a depends-on or @DependsOn from that particular bean to the BPP. Finding out which bean is causing the early init is the hard part though. You need to set a breakpoint in DataSourceConfiguration and inspect the callstack to find it.

One more thing, is the P6DataSource checking double-init, does it wrap again if the datasource is already a P6DataSource ?

HTH
Jorg



On Friday, July 28, 2017 at 4:05:22 AM UTC+2, Quinton McCombs wrote:
Oh, Spring Boot autoconfiguration is the reason for the BPP. That makes sense now. Is the code that you submitted in the prior message still correct for the BPP? If so, do you mind if we include it in our documentation (and perhaps in an autoconfig module)?
On Thu, Jul 27, 2017 at 5:37 AM, Jorg Heymans via p6spy users <p6spy-users+APn2wQdxifwsrkbPOZwiwzq_KCfC1Fy9rUGWdMFoF0e9SbFAgIA_@googlegroups.com> wrote:
In the end i traced it down to the fact that a bean was causing the datasource to be very-eagerly instantiated, before the BPP was known to the application context. Adding a depends-on from this application bean to the BPP made it work.

Thanks,
Jorg

On Thursday, July 27, 2017 at 10:51:10 AM UTC+2, Jorg Heymans wrote:
This turned out quite hard to do as well, with circular references all over the place. I suspect the datasource autoconfiguration is getting in the way but i don't have time to look into it further now. 

The funny thing is that for another (much smaller) spring boot application, th BPP works fine and is invoked correctly very early when the application context is still initializing, so that all subsequent repository beans get the P6 datasource. 

Jorg

On Wednesday, July 26, 2017 at 11:48:00 PM UTC+2, Quinton McCombs wrote:
Have you tried just wrapping the data source in your config instead of using a BPP? The example at https://stackoverflow.com/questions/14706029/usage-of-p6spy-with-datasource-in-spring-applicationcontext-xml is for XMl configuration but the same approach works for java config as well.

--
You received this message because you are subscribed to the Google Groups "p6spy users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to p6spy-users...@googlegroups.com.

To post to this group, send email to p6spy...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages