How to use user defined database proxy in @DataJpaTest

rajadilipkolli

We need to track database metrics so we are using datasource-proxy to track this to integrate the same in spring boot project we have created custom datasource as below

@Component
@Slf4j
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceBeanConfig
{

    public DataSource actualDataSource()
    {
        EmbeddedDatabaseBuilder databaseBuilder = new EmbeddedDatabaseBuilder();
        return databaseBuilder.setType(EmbeddedDatabaseType.H2).build();
    }

    @Bean
    @Primary
    public DataSource dataSource() {
        // use pretty formatted query with multiline enabled
        PrettyQueryEntryCreator creator = new PrettyQueryEntryCreator();
        creator.setMultiline(true);

        log.info("Inside Proxy Creation");

        SystemOutQueryLoggingListener listener = new SystemOutQueryLoggingListener();
        listener.setQueryLogEntryCreator(creator);

        return ProxyDataSourceBuilder
                .create(actualDataSource())
                .countQuery()
                .name("MyDS")
                .listener(listener)
                .build();
    }
}

When we run main application datasource-proxy is picked up but when we use @DataJpaTest it is not picking up. How to enable datasource-proxy in JUNIT test cases?

Edit::

Using Spring BeanPostProcessor to configure Proxy DataSource

@Slf4j
@Configuration
public class DataSourceBeanConfig implements BeanPostProcessor
{
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException
    {

        if (bean instanceof DataSource)
        {
            System.out.println("AfterInitialization : " + beanName);

            // use pretty formatted query with multiline enabled
            PrettyQueryEntryCreator creator = new PrettyQueryEntryCreator();
            creator.setMultiline(true);

            log.info("Inside Proxy Creation");

            SystemOutQueryLoggingListener listener = new SystemOutQueryLoggingListener();
            listener.setQueryLogEntryCreator(creator);

            return ProxyDataSourceBuilder.create((DataSource) bean).countQuery()
                    .name("MyDS").listener(listener).build();

        }
        return bean; // you can return any other object as well
    }
}
rajadilipkolli

Here is the solution we need to create TestConfiguration to use in @DataJpaTest

@RunWith(SpringRunner.class)
@DataJpaTest
public class DataTestJPA
{

    @TestConfiguration
    static class ProxyDataSourceConfig implements BeanPostProcessor
    {
        public Object postProcessAfterInitialization(Object bean, String beanName)
                throws BeansException
        {

            if (bean instanceof DataSource)
            {
               return ProxyDataSourceBuilder
                            .create((DataSource) bean)
                            .countQuery()
                            .name("MyDS")
                            .build();
                // @formatter:on

            }
            return bean; // you can return any other object as well
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I use @SpringBootTest(webEnvironment) with @DataJpaTest?

How to use user-defined class object as a networkx node?

Use a user-defined variable to dynamically name second database to JOIN tables with that second database

How to use Primefaces galleria with user defined objects in jsf

How to design a database for User Defined Fields?

How to import and use user defined classes in robot framework with python

How can I make Qt use a user-defined class?

How to use user defined function in mustache php

How to use user defined variable inside of `IF` expression?

kableExtra how to use cell_spec from a user defined range?

C++ how to use emplace_back for user defined structure

How to use Cloud SQL Proxy with a regular user account?

How to use user defined function for jquery chaining

Angular components how to use stylesheet defined be the user?

How to use user defined property in construction method for a winform?

Database Design for user defined groups

How can I use LightDM for user-defined sessions?

How to use User Defined Attribute in D language?

How to use a $_session[] name to defined a table for mysql database?

How to use BOOST_CHECK_CLOSE for User defined types

How to use the current user for proxy with WinSCP

How to Insert Data to the Database? - User Defined Classes

How to use an IN Clause with a user defined list

How do I use a User Defined Type in a function?

How do I list user defined types in a HANA SQL Database?

How to use user defined variable names in a linux command?

how to use a user input defined in one html page in another php page to fetch data from oracle database

how to not use require() in my user-defined function in R

Ruby: How to save user defined logic in database