gavlyukovskiy/spring-boot-data-source-decorator

NPE with RoutingDatasource

oburgosm opened this issue · 3 comments

I have an application with a RoutingDatasource. Using datasource-decorator (datasource-proxy-spring-boot-starter), we have a NPE sometimes.

I created a simple app to reproduce the problem: https://github.com/oburgosm/routing-datasource-test

The exception is:

java.lang.NullPointerException: null
	at com.github.gavlyukovskiy.cloud.sleuth.TracingListenerStrategy.afterQuery(TracingListenerStrategy.java:83) ~[datasource-decorator-spring-boot-autoconfigure-1.5.8.jar:na]
	at com.github.gavlyukovskiy.cloud.sleuth.TracingQueryExecutionListener.afterQuery(TracingQueryExecutionListener.java:62) ~[datasource-decorator-spring-boot-autoconfigure-1.5.8.jar:na]
	at net.ttddyy.dsproxy.listener.ChainListener.afterQuery(ChainListener.java:27) ~[datasource-proxy-1.5.1.jar:na]
	at net.ttddyy.dsproxy.proxy.StatementProxyLogic.performQueryExecutionListener(StatementProxyLogic.java:395) ~[datasource-proxy-1.5.1.jar:na]
	at net.ttddyy.dsproxy.proxy.StatementProxyLogic.access$700(StatementProxyLogic.java:37) ~[datasource-proxy-1.5.1.jar:na]
	at net.ttddyy.dsproxy.proxy.StatementProxyLogic$1.execute(StatementProxyLogic.java:123) ~[datasource-proxy-1.5.1.jar:na]
	at net.ttddyy.dsproxy.listener.MethodExecutionListenerUtils.invoke(MethodExecutionListenerUtils.java:42) ~[datasource-proxy-1.5.1.jar:na]
	at net.ttddyy.dsproxy.proxy.StatementProxyLogic.invoke(StatementProxyLogic.java:120) ~[datasource-proxy-1.5.1.jar:na]
	at net.ttddyy.dsproxy.proxy.jdk.PreparedStatementInvocationHandler.invoke(PreparedStatementInvocationHandler.java:37) ~[datasource-proxy-1.5.1.jar:na]
	at com.sun.proxy.$Proxy101.executeQuery(Unknown Source) ~[na:na]
	at jdk.internal.reflect.GeneratedMethodAccessor27.invoke(Unknown Source) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
	at net.ttddyy.dsproxy.proxy.StatementProxyLogic.performQueryExecutionListener(StatementProxyLogic.java:316) ~[datasource-proxy-1.5.1.jar:na]
	at net.ttddyy.dsproxy.proxy.StatementProxyLogic.access$700(StatementProxyLogic.java:37) ~[datasource-proxy-1.5.1.jar:na]
	at net.ttddyy.dsproxy.proxy.StatementProxyLogic$1.execute(StatementProxyLogic.java:123) ~[datasource-proxy-1.5.1.jar:na]
	at net.ttddyy.dsproxy.listener.MethodExecutionListenerUtils.invoke(MethodExecutionListenerUtils.java:42) ~[datasource-proxy-1.5.1.jar:na]
	at net.ttddyy.dsproxy.proxy.StatementProxyLogic.invoke(StatementProxyLogic.java:120) ~[datasource-proxy-1.5.1.jar:na]
	at net.ttddyy.dsproxy.proxy.jdk.PreparedStatementInvocationHandler.invoke(PreparedStatementInvocationHandler.java:37) ~[datasource-proxy-1.5.1.jar:na]
	at com.sun.proxy.$Proxy101.executeQuery(Unknown Source) ~[na:na]
	at com.bracso.test.routing.RoutingTestApplicationTests.executeDummyQuery(RoutingTestApplicationTests.java:63) ~[test-classes/:na]
	at com.bracso.test.routing.RoutingTestApplicationTests.lambda$testConcurrencia$0(RoutingTestApplicationTests.java:83) ~[test-classes/:na]
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
	at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]

Right now, I updated my example, excluding routingDatasource from decorating (for avoid that it was decorated twice), but adding another datasource to execution test. The problem still persist

Hi @oburgosm, thank you for the provided sample, it looks like you seeing this issue because two instances of ProxyDataSource can generate the same connectionId (two different instances of DefaultConnectionIdManager generate ids sequentially), so that connections from different data sources replace each other inside TracingListenerStrategy#openConnections.

I have opened issue in datasource-proxy, if issue gets resolved there this one will be fixed as well.