Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example to use r2dbc-proxy in a spring boot project #474

Open
rajadilipkolli opened this issue Jan 7, 2025 · 2 comments
Open

Example to use r2dbc-proxy in a spring boot project #474

rajadilipkolli opened this issue Jan 7, 2025 · 2 comments

Comments

@rajadilipkolli
Copy link

Hi @hantsy ,

Hope you are doing well, can you please create a sample to use r2dbc-proxy to log the queries.

@rajadilipkolli
Copy link
Author

Hi @hantsy, I was able to achieve this using declaring a bean as

    @Bean
    ConnectionFactoryOptionsBuilderCustomizer connectionFactoryOptionsBuilderCustomizer(
            CustomExecutionListener customExecutionListener) {
        return builder -> {
            builder.option(ConnectionFactoryOptions.DRIVER, "proxy");
            builder.option(ConnectionFactoryOptions.PROTOCOL, "pool:postgresql");
            builder.option(ProxyConnectionFactoryProvider.PROXY_LISTENERS, customExecutionListener);
        };
    }

and listener as

@Component
public class CustomExecutionListener implements ProxyExecutionListener {

    private static final Logger logger = LoggerFactory.getLogger(CustomExecutionListener.class);

    @Override
    public void afterQuery(QueryExecutionInfo queryExecutionInfo) {
        QueryExecutionInfoFormatter formatter = QueryExecutionInfoFormatter.showAll();
        String str = formatter.format(queryExecutionInfo);
        logger.info("Query: {}", str);
        logger.info("Execution Time: {} ms", queryExecutionInfo.getExecuteDuration().toMillis());
    }
}

If you are fine with the approach I can raise a PR.

@hantsy
Copy link
Owner

hantsy commented Jan 8, 2025

It is great to send a PR for this.

I think creating a new simple project is ok, and named it r2dbc-proxy.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants