We had a weird requirement where the same spring bean needs to be deployed on two different node types (storage and metadata). When its deployed on metadata node it needs to talk to a specific shard database and when its deployed on storage node it can talk to multiple shard databases. To achieve this in metadata node we wrote our own transaction annotation and applied a TransactionInterceptor to the bean in spring that would start transaction before every method.
Now in order for same bean in storage node talk to different databases over different transaction managers we created a pool of beans at startup each with its own interceptor and there we had to hand create the beans but now problem was how to manually apply the same AOP inteceptor. We tried using ProxyFactoryBean but it was not easy and then my colleague landed on to this which saved the day.
ProxyFactory proxyFactory = new ProxyFactory(sqlDirectoryService);
proxyFactory.addAdvice(transactionInterceptor);
proxyFactory.setProxyTargetClass(true);
return proxyFactory;
Now in order for same bean in storage node talk to different databases over different transaction managers we created a pool of beans at startup each with its own interceptor and there we had to hand create the beans but now problem was how to manually apply the same AOP inteceptor. We tried using ProxyFactoryBean but it was not easy and then my colleague landed on to this which saved the day.
ProxyFactory proxyFactory = new ProxyFactory(sqlDirectoryService);
proxyFactory.addAdvice(transactionInterceptor);
proxyFactory.setProxyTargetClass(true);
return proxyFactory;
Comments
Post a Comment