{"id":13565345,"url":"https://github.com/gavlyukovskiy/spring-boot-data-source-decorator","last_synced_at":"2025-05-14T08:07:58.844Z","repository":{"id":44726382,"uuid":"88676590","full_name":"gavlyukovskiy/spring-boot-data-source-decorator","owner":"gavlyukovskiy","description":"Spring Boot integration with p6spy, datasource-proxy, flexy-pool and spring-cloud-sleuth","archived":false,"fork":false,"pushed_at":"2025-04-26T01:52:12.000Z","size":2950,"stargazers_count":932,"open_issues_count":8,"forks_count":86,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-04-26T02:53:16.533Z","etag":null,"topics":["connection-pool","datasource","datasource-proxy","flexy-pool","jdbc","micrometer","p6spy","spring-boot","spring-boot-starters","spring-cloud-sleuth","sql-queries"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gavlyukovskiy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"gavlyukovskiy"}},"created_at":"2017-04-18T22:39:27.000Z","updated_at":"2025-04-26T01:52:15.000Z","dependencies_parsed_at":"2024-02-04T10:23:48.097Z","dependency_job_id":"5ec087c5-7df3-4595-9e04-88951be678ea","html_url":"https://github.com/gavlyukovskiy/spring-boot-data-source-decorator","commit_stats":{"total_commits":273,"total_committers":12,"mean_commits":22.75,"dds":0.06227106227106227,"last_synced_commit":"e5edad0a73e55edfb7df71ccc82d78d042c7d104"},"previous_names":[],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gavlyukovskiy%2Fspring-boot-data-source-decorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gavlyukovskiy%2Fspring-boot-data-source-decorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gavlyukovskiy%2Fspring-boot-data-source-decorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gavlyukovskiy%2Fspring-boot-data-source-decorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gavlyukovskiy","download_url":"https://codeload.github.com/gavlyukovskiy/spring-boot-data-source-decorator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254101557,"owners_count":22014908,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["connection-pool","datasource","datasource-proxy","flexy-pool","jdbc","micrometer","p6spy","spring-boot","spring-boot-starters","spring-cloud-sleuth","sql-queries"],"created_at":"2024-08-01T13:01:45.059Z","updated_at":"2025-05-14T08:07:58.836Z","avatar_url":"https://github.com/gavlyukovskiy.png","language":"Java","funding_links":["https://github.com/sponsors/gavlyukovskiy"],"categories":["Java"],"sub_categories":[],"readme":"### Spring Boot DataSource Decorator\n\n[![Build status](https://github.com/gavlyukovskiy/spring-boot-data-source-decorator/workflows/Build/badge.svg)](https://github.com/gavlyukovskiy/spring-boot-data-source-decorator/actions/workflows/on-master.yml?query=branch%3Amaster)\n[![Latest release](https://img.shields.io/badge/dynamic/xml.svg?label=Maven%20Central\u0026color=green\u0026query=%2F%2Fmetadata%2Fversioning%2Flatest\u0026url=https%3A%2F%2Frepo1.maven.org%2Fmaven2%2Fcom%2Fgithub%2Fgavlyukovskiy%2Fdatasource-decorator-spring-boot-autoconfigure%2Fmaven-metadata.xml)](https://mvnrepository.com/artifact/com.github.gavlyukovskiy/datasource-decorator-spring-boot-autoconfigure/)\n\n[Spring Boot](https://github.com/spring-projects/spring-boot) auto-configuration for integration with\n* [P6Spy](https://github.com/p6spy/p6spy) - adds ability to intercept and log sql queries, including interception of a most `Connection`, `Statement` and `ResultSet` methods invocations\n* [Datasource Proxy](https://github.com/ttddyy/datasource-proxy) - adds ability to intercept all queries and `Connection`, `Statement` and `ResultSet` method calls\n* [FlexyPool](https://github.com/vladmihalcea/flexy-pool) - adds connection pool metrics (jmx, codahale, dropwizard) and flexible strategies for adjusting pool size on demand\n\n#### Why not wrap DataSource in a configuration?\n\nInstead of using the library you can manually wrap your `DataSource`, but this library also provides\n* ability to use `@ConfigurationProperties` provided by Spring Boot (`spring.datasource.hikari.*`, `spring.datasource.dbcp2.*`)\n* disabling decorating by deployment property `decorator.datasource.enabled=true/false`\n* configure proxies through spring properties `application.properties/yml` and customize proxies by defining beans in the spring context\n\n#### Quick Start\n\nAdd one of the starters to the classpath of a Spring Boot 3 application and your datasources (auto-configured or custom) will be wrapped into one of a datasource proxy providers below.\n\nThe latest release version is [![Latest release](https://img.shields.io/badge/dynamic/xml.svg?label=\u0026color=green\u0026query=%2F%2Fmetadata%2Fversioning%2Flatest\u0026url=https%3A%2F%2Frepo1.maven.org%2Fmaven2%2Fcom%2Fgithub%2Fgavlyukovskiy%2Fdatasource-decorator-spring-boot-autoconfigure%2Fmaven-metadata.xml)](https://mvnrepository.com/artifact/com.github.gavlyukovskiy/datasource-decorator-spring-boot-autoconfigure/)\n\n\u003e [!NOTE]  \n\u003e For Spring Boot 2 applications, the latest compatible version is 1.8.1.\n\nIf you want to use [P6Spy](https://github.com/p6spy/p6spy)\n```groovy\nimplementation(\"com.github.gavlyukovskiy:p6spy-spring-boot-starter:${version}\")\n```\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.gavlyukovskiy\u003c/groupId\u003e\n    \u003cartifactId\u003ep6spy-spring-boot-starter\u003c/artifactId\u003e\n    \u003cversion\u003e${version}\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nor [Datasource Proxy](https://github.com/ttddyy/datasource-proxy):\n```groovy\nimplementation(\"com.github.gavlyukovskiy:datasource-proxy-spring-boot-starter:${version}\")\n```\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.gavlyukovskiy\u003c/groupId\u003e\n    \u003cartifactId\u003edatasource-proxy-spring-boot-starter\u003c/artifactId\u003e\n    \u003cversion\u003e${version}\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nor [FlexyPool](https://github.com/vladmihalcea/flexy-pool)\n\u003e To use FlexyPool with connection pool other than HikariCP you must add `PoolAdapter` for your [particular connection pool](https://github.com/vladmihalcea/flexy-pool/wiki/Installation-Guide#connection-pool-settings).\n```groovy\nimplementation(\"com.github.gavlyukovskiy:flexy-pool-spring-boot-starter:${version}\")\n```\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.gavlyukovskiy\u003c/groupId\u003e\n    \u003cartifactId\u003eflexy-pool-spring-boot-starter\u003c/artifactId\u003e\n    \u003cversion\u003e${version}\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n##### What if I add multiple decorators?\n\nYou can use all decorators at the same time if you need, if so decorating order will be:\n\n```P6DataSource -\u003e ProxyDataSource -\u003e FlexyPoolDataSource -\u003e DataSource```\n\n#### P6Spy\n\nAfter adding p6spy starter you'll start getting all sql queries in the logs:\n```text\n2017-06-07 21:42:08 INFO p6spy: #1496860928120 | took 0ms | statement | connection 0|SELECT NOW()\n;\n2017-06-07 21:51:07 INFO p6spy: #1496861467802 | took 0ms | statement | connection 1|SELECT NOW()\n;\n2017-06-07 21:51:07 INFO p6spy: #1496861467803 | took 0ms | statement | connection 2|SELECT NOW()\n;\n2017-06-07 21:51:08 INFO p6spy: #1496861468806 | took 0ms | statement | connection 3|SELECT NOW()\n;\n```\n\nAll beans of type `JdbcEventListener` are registered in P6Spy:\n```java\n@Bean\npublic JdbcEventListener myListener() {\n    return new JdbcEventListener() {\n        @Override\n        public void onAfterGetConnection(ConnectionInformation connectionInformation, SQLException e) {\n            System.out.println(\"got connection\");\n        }\n\n        @Override\n        public void onAfterConnectionClose(ConnectionInformation connectionInformation, SQLException e) {\n            System.out.println(\"connection closed\");\n        }\n    };\n}\n```\n\nThis done by adding `RuntimeListenerSupportFactory` into P6Spy `modulelist`, overriding this property will cause to not registering factory thus listeners will not be applied\n\nYou can configure small set of parameters in your `application.properties`:\n\u003e [!NOTE]\n\u003e Configuration below indicates al possible parameters together with their default values and **does not** need to be set explicitly\n```properties\n# Register P6LogFactory to log JDBC events\ndecorator.datasource.p6spy.enable-logging=true\n# Use com.p6spy.engine.spy.appender.MultiLineFormat instead of com.p6spy.engine.spy.appender.SingleLineFormat\ndecorator.datasource.p6spy.multiline=true\n# Use logging for default listeners [slf4j, sysout, file, custom]\ndecorator.datasource.p6spy.logging=slf4j\n# Log file to use (only with logging=file)\ndecorator.datasource.p6spy.log-file=spy.log\n# Class file to use (only with logging=custom). The class must implement com.p6spy.engine.spy.appender.FormattedLogger\ndecorator.datasource.p6spy.custom-appender-class=my.custom.LoggerClass\n# Custom log format, if specified com.p6spy.engine.spy.appender.CustomLineFormat will be used with this log format\n# see https://p6spy.readthedocs.io/en/latest/configandusage.html#customlogmessageformat\ndecorator.datasource.p6spy.log-format=\n# Use regex pattern to filter log messages. If specified only matched messages will be logged.\ndecorator.datasource.p6spy.log-filter.pattern=\n# Exclude certain categories from logging. If specified only matched messages will be logged.\n# see https://p6spy.readthedocs.io/en/latest/configandusage.html#excludecategories\ndecorator.datasource.p6spy.exclude-categories=\n```\n\nAlso you can configure P6Spy manually using one of available configuration methods. For more information please refer to the [P6Spy Configuration Guide](http://p6spy.readthedocs.io/en/latest/configandusage.html)\n\n#### Datasource Proxy\n\nAfter adding datasource-proxy starter you'll start getting all sql queries in the logs with level `DEBUG` and slow sql queries with level `WARN`:\n```text\n2017-06-07 21:58:06 DEBUG n.t.d.l.l.SLF4JQueryLoggingListener:\nName:, Time:0, Success:True\nType:Statement, Batch:False, QuerySize:1, BatchSize:0\nQuery:[\"SELECT NOW()\"]\nParams:[]\n2017-06-07 21:58:06 DEBUG n.t.d.l.l.SLF4JQueryLoggingListener:\nName:, Time:0, Success:True\nType:Statement, Batch:False, QuerySize:1, BatchSize:0\nQuery:[\"SELECT NOW()\"]\nParams:[]\n2017-06-07 21:58:06.630  DEBUG 8492 --- [ool-1-worker-50] n.t.d.l.l.SLF4JQueryLoggingListener      :\nName:, Time:0, Success:True\nType:Statement, Batch:False, QuerySize:1, BatchSize:0\nQuery:[\"SELECT NOW()\"]\nParams:[]\n2017-06-07 22:10:50 WARN n.t.d.l.logging.SLF4JSlowQueryListener:\nName:, Time:0, Success:False\nType:Statement, Batch:False, QuerySize:1, BatchSize:0\nQuery:[\"SELECT SLEEP(301000)\"]\nParams:[]\n```\n\nYou can add custom `QueryExecutionListener` by registering them in the context, as well you can override `ParameterTransformer`, `QueryTransformer` and `ConnectionIdManager`:\n```java\n@Bean\npublic QueryExecutionListener queryExecutionListener() {\n    return new QueryExecutionListener() {\n        @Override\n        public void beforeQuery(ExecutionInfo execInfo, List\u003cQueryInfo\u003e queryInfoList) {\n            System.out.println(\"beforeQuery\");\n        }\n\n        @Override\n        public void afterQuery(ExecutionInfo execInfo, List\u003cQueryInfo\u003e queryInfoList) {\n            System.out.println(\"afterQuery\");\n        }\n    };\n}\n\n@Bean\npublic ParameterTransformer parameterTransformer() {\n    return new MyParameterTransformer();\n}\n\n@Bean\npublic QueryTransformer queryTransformer() {\n    return new MyQueryTransformer();\n}\n\n@Bean\npublic ConnectionIdManagerProvider connectionIdManagerProvider() {\n    return MyConnectionIdManager::new;\n}\n```\nYou can configure logging, query/slow query listeners and more using your `application.properties`:\n\u003e [!NOTE]\n\u003e Configuration below indicates al possible parameters together with their default values and **does not** need to be set explicitly\n```properties\n# One of logging libraries (slf4j, jul, common, sysout)\ndecorator.datasource.datasource-proxy.logging=slf4j\n\ndecorator.datasource.datasource-proxy.query.enable-logging=true\ndecorator.datasource.datasource-proxy.query.log-level=debug\n# Logger name to log all queries, default depends on chosen logging, e.g. net.ttddyy.dsproxy.listener.logging.SLF4JQueryLoggingListener\ndecorator.datasource.datasource-proxy.query.logger-name=\n\ndecorator.datasource.datasource-proxy.slow-query.enable-logging=true\ndecorator.datasource.datasource-proxy.slow-query.log-level=warn\ndecorator.datasource.datasource-proxy.slow-query.logger-name=\n# Number of seconds to consider query as slow and log it\ndecorator.datasource.datasource-proxy.slow-query.threshold=300\n\ndecorator.datasource.datasource-proxy.multiline=true\n\n# Formats the SQL for better readability. Uses Hibernate's formatter if present on the class path. If you opted in for a different JPA provider you need to add https://github.com/vertical-blank/sql-formatter as a runtime dependency to your app  to enable this. \n# Mutually exclusive with json-format=true\ndecorator.datasource.datasource-proxy.format-sql=false\ndecorator.datasource.datasource-proxy.json-format=false\n\n# Enable Query Metrics\ndecorator.datasource.datasource-proxy.count-query=false\n```\n\n#### Flexy Pool\n\nIf the `flexy-pool-spring-boot-starter` is added to the classpath your datasource will be wrapped to the `FlexyPoolDataSource`.\n\n\u003e [!IMPORTANT]\n\u003e If you are not relying on Spring Boot autoconfiguration and instead declare a custom `DataSource` bean, you need to use\n\u003e a concrete `DataSource` type as the return value (e.g., `@Bean HikariDataSource dataSource()`) to match the FlexyPool adapter.\n\nWith default setting you will start getting messages about acquiring and leasing connections:\n```text\n2017-07-13 01:31:02 INFO c.v.flexypool.FlexyPoolDataSource: Connection leased for 1500 millis, while threshold is set to 1000 in dataSource FlexyPoolDataSource\n2017-07-13 01:31:03 WARN PoolOnTimeoutConnectionAcquisitionStrategy: Connection was acquired in 1502 millis, timeoutMillis is set to 500\n2017-07-13 01:31:03 INFO PoolOnTimeoutConnectionAcquisitionStrategy: Pool size changed from previous value 10 to 11\n```\n\nAll beans of type `ConnectionAcquisitionStrategyFactory` are used to provide `ConnectionAcquisitionStrategy` for the pool.\n\n`MetricsFactory` and `ConnectionProxyFactory` beans can be used to customize metrics and connection decorators.\n\n`EventListener\u003c? extends Event\u003e` beans can be registered to subscribe on events of flexy-pool (e.g. `ConnectionAcquisitionTimeThresholdExceededEvent`, `ConnectionLeaseTimeThresholdExceededEvent`).\n\nYou can configure your `FlexyPoolDataSource` by using bean `FlexyPoolConfigurationBuilderCustomizer` or properties:\n\u003e [!NOTE]\n\u003e Configuration below indicates al possible parameters together with their default values and **does not** need to be set explicitly\n```properties\n# Increments pool size if connection acquisition request has timed out\ndecorator.datasource.flexy-pool.acquisition-strategy.increment-pool.max-overgrow-pool-size=15\ndecorator.datasource.flexy-pool.acquisition-strategy.increment-pool.timeout-millis=500\n\n# Retries on getting connection\ndecorator.datasource.flexy-pool.acquisition-strategy.retry.attempts=2\n\n# Enable metrics exporting to the JMX\ndecorator.datasource.flexy-pool.metrics.reporter.jmx.enabled=true\ndecorator.datasource.flexy-pool.metrics.reporter.jmx.auto-start=false\n\n# Millis between two consecutive log reports\ndecorator.datasource.flexy-pool.metrics.reporter.log.millis=300000\n\n# Enable logging and publishing ConnectionAcquisitionTimeThresholdExceededEvent when a connection acquisition request has timed out\ndecorator.datasource.flexy-pool.threshold.connection.acquisition=50\n# Enable logging and publishing ConnectionLeaseTimeThresholdExceededEvent when a connection lease has exceeded the given time threshold\ndecorator.datasource.flexy-pool.threshold.connection.lease=1000\n```\n\n#### Spring Cloud Sleuth (removed since 1.9.0)\n\n##### For Spring Boot users, that DO NOT use Spring Cloud Sleuth\nNothing has changed, this project is continued to be supported and maintained, and can be used to enable JDBC logging\nand provide auto-configuration of P6Spy, Datasource-Proxy and FlexyPool.\n\n\n##### For Spring Cloud Sleuth users\nAs of release 1.8.0 Spring Cloud Sleuth integration was deprecated in favor of [Spring Cloud Sleuth: Spring JDBC](https://docs.spring.io/spring-cloud-sleuth/docs/3.1.0/reference/html/integrations.html#sleuth-jdbc-integration)\nwhich provides JDBC instrumentation out of the box.\n\nAs of release 1.9.0 Spring Cloud Sleuth integration was removed.\n\nSpring Cloud Sleuth JDBC was based on this project and keeps all functionality including logging, tracing, configuration and customizations.\n\n##### Migration process:\n1. If you are using Spring Cloud Sleuth you can migrate all properties from `decorator.datasource.*` to `spring.sleuth.jdbc.*` with minimal changes.\n2. If you have query logging enabled (default state) then you need to explicitly enable logging using:\n   - P6Spy: `spring.sleuth.jdbc.p6spy.enable-logging=true`\n   - Datasource-Proxy: `spring.sleuth.jdbc.datasource-proxy.query.enable-logging=true`\n3. If you were using decoration customizers please consult with Spring Cloud Sleuth documentation and migrate usage of those to appropriate alternatives in Spring Cloud Sleuth\n4. _(Optional)_ Replace dependency on this starter with the particular library\n   - P6Spy: replace `com.github.gavlyukovskiy:p6spy-spring-boot-starter` with `p6spy:p6spy`\n   - Datasource-Proxy: replace `com.github.gavlyukovskiy:datasource-proxy-spring-boot-starter` with `net.ttddyy:datasource-proxy`\n5. Any issues can be raised in Spring Cloud Sleuth  project on GitHub, you may tag me (@gavlyukovskiy) and I'll try to help.\n6. Enjoy using JDBC instrumentation, and thank you for using this library :)\n \nDue to similarities in implementation, using starters from this library together with Spring Cloud Sleuth 3.1.0 is possible, although decoration will be automatically disabled in favor of Spring Cloud Sleuth to avoid duplicated logging, tracing or any other potential issues.\n\n#### Custom Decorators\n\nCustom data source decorators are supported through declaring beans of type `DataSourceDecorator`\n```java\n@Bean\npublic DataSourceDecorator customDecorator() {\n  return (beanName, dataSource) -\u003e new DataSourceWrapper(dataSource);\n}\n```\n\n#### Disable Decorating\n\nIf you want to disable decorating set `decorator.datasource.exclude-beans` with bean names you want to exclude.\nAlso, you can disable decorating for `AbstractRoutingDataSource` setting property `decorator.datasource.ignore-routing-data-sources` to `true`\nSet `decorator.datasource.enabled` to `false` if you want to disable all decorators for all datasources. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgavlyukovskiy%2Fspring-boot-data-source-decorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgavlyukovskiy%2Fspring-boot-data-source-decorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgavlyukovskiy%2Fspring-boot-data-source-decorator/lists"}