• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem managing session with multiple data sources

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
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..

<bean id="readOnlyUserProxy" class="org.springframework.aop.framework.ProxyFact oryBean">
<property name="target" ref="readOnlyDao"/>
<property name="interceptorNames">
<list>
<value>readOnlyTransactionInterceptor</value>
</list>
</property>
</bean>

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.

Thanking you all for your support,
Av~
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With the association with lazy. Are they from different databases. Meaning one side is in one database/datasource, and the other side is from a different database/datasource?

If so, Hibernate can only communicate with one database, a SessionFactory is one to one to a datasource/database.

In some cases, where the database store the same data model, then you can use Hibernate Shards, but I doubt that is your case.

Mark
 
Aravind Bhat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah they are from different databases . Guess I can use JTA for this but again how do I keep the sessiona open till the handler layer?
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Aravind Bhat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ben,

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

Thanks,
Aravind
 
Ben Narendren
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)



 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Mark
 
Aravind Bhat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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". "

Thanks again for your help,
Av~
 
Ben Narendren
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Aravind Bhat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ben ,
As mentioned earlier, I have begun my work on using JtaTransactionManager . I thought that there may be some route other than this coz Tomcat dosen't support JTA.

After this, I feel that using JTA with Tomcat + JOTM may be the only route out. But again the problem lies in the requirement of keeping various sessions open till the handler layer .

Thanks Again,
Av~
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic