I want to connect different databases based on User login, So I am having the TransactionManager bean as Prototype scope and it is creating transacionManager bean everytime, and works well. But , Is it good when the scope of the application increases and how stable it will be?
Any ideas ? Thanks.
@Bean(initMethod = "init", destroyMethod = "destroy")
@Scope(value = "prototype")
public PlatformTransactionManager transactionManager() {
Using it as prototype is a not a good practice. This actually means that each time
transactionManager()is called, a newTransactionManageris created, which is redundant resource consuming, as a single instance for eachTransactionManagerconfiguration is enough.Instead, create some factory bean, you can call it
TransactionManagerFactory, which exposes a getter such asPlatformTransactionManager getUserTxManager(SomeRelevantUserDetails). This factory will create the variousPlatformTransactionManagers for the various DB vendors only once each and return it to the caller according toSomeRelevantUserDetails.TransactionManagerFactory class:
Other service that requires a user-dependent transaction manager: