I am working on a project where we each service refers four separate data-source. Until now, we have been using ProxyFactoryBean to refer to the Dao target and the Transaction Intereceptor - something like this..
There are 3 other similar proxies for the different DAOs . All these refer to different transaction interceptors which in turn connect to different transaction managers. In short, each service connects to 4 dao proxies each of which refer a separate transaction interceptor, each of which in turn refer to a separate transaction manager connecting to 4 different data sources. All work fine till now with lazy="false".
Now, In order to optimize the performance, we wish to enable 'Lazy loading' and carry the hibernate session to the handler layer. We think that the best way for this would be through the 'TransactionProxyFactoryBean' as we do not want to use the OpenSessionInView approach .
We have tried some approaches but are stuck because we connect to 4 separate data sources through each service and in now way can we connect the four separate transaction managers to the 'TransactionProxyFactoryBean'. Therefore, we are not able to find a way to manage the transactions from different data sources in the handler/service layer.
I have just started on this work and do not have much experience in Spring transaction management. Kindly guide me on any possible approach I could take.
The way to keep sessions open in the controller layer is by using OpenSessionInViewInterceptor provided by Spring. If you don't like what Spring is offering, you might have to write your own interceptor but the concept would be the same. I don't think that using TransactionProxyFactoryBean can help you with the session in view pattern. By the way, if you are using version 2.0 or up, its not a good idea to use TransactionProxyFactoryBean to configure transactions. You could use annotations or if you want to stick to xml , there is a much more sophisticated way to do this using AOP.
Can you please elaborate on how I can use annotations to control two separate session factories to connect to different data sources. As far as I know, tx:annotation-driven will not be able to support this. Kindly correct me if I am wrong. And also It would be helpful if you could elaborate on the 'more sophisticated way' you had mentioned
Hmm..you are right .. I never did anything like that with annotations. I'll do some research on it and let you know if I find something. Anyway using regular XML I think we should be able to do this. Following is a section of code which I picked up from spring reference and tweaked to suit your purposes.
If you look at the code above , you'll see that there are two sets of services (service1Operations1 and service2Operations) and those two are seperately linked to two different TransactionManagers (txManager1 and txManager2)
I still don't think you will be able to "Eager" the assocation. Because Hibernate can only work with one Database at a time. Shards might help, but the two databases have to be identical in terms of data model. there might be a version later on of Shards that will allow two table association where each table resides in different databases, but at this time it just isn't possible.
Thanks again guys but in the example, each of the service connects to a separate transaction manager. What I need is a single service which connects to multiple data sources using separate transaction managers and to manage the transactions between them. As I menthioned,
"each service connects to 4 dao proxies each of which refer a separate transaction interceptor, each of which in turn refer to a separate transaction manager connecting to 4 different data sources. All work fine till now with lazy="false". "
Mark, I was under the impression that what Aravind meant by 'Lazy loading' was the bringing of entities to the controller layer and fetching data from there. As far the association goes, I agree with you that we can't have two ends of an association placed in two different databases. Aravind, could you clarify if this is what you want?
Aravind Bhat wrote:What I need is a single service which connects to multiple data sources using separate transaction managers and to manage the transactions between them.
Aravind, I am not clear on something. Do you want multiple dbs to be part of the same transaction?
If yes, you don't need multiple transaction mangers. You need one JtaTransactionManager.
if No, you can use the same code I pasted in my post above and change the AOP settings to apply to the same service and specify which methods you want specific transaction managers to act on. But if you have a single method which accesses two different dbs under two transactional contexts, I suggest you separate these methods such that there is only one transactional context per method.