Community   SpringSource   Projects    Downloads    Documentation    Forums    Training   Exchange   Blogs

Go Back   Spring Community Forums > Core Spring Projects > Data Access

Reply
 
Thread Tools Display Modes
  #1  
Old Nov 30th, 2009, 04:43 PM
krishna81m krishna81m is offline
Junior Member
 
Join Date: Nov 2009
Posts: 8
Default Supporting multiple hibernate session factories based on keys

I had been working on a Struts 1.3, Hibernate 3.3, JSP stack and successfully implemented a flexible application where in the user's company ticker would be used to choose among a specific sessionFactory to return the right hibernate session. All I had to was pass on the ticker from his HttpSession to all DAO's from all services to access database. Here I controlled what sessionFactory is used to create a ThreadLocal session variable and had this companyTicker passing along all layers.

The application is now migrated to Spring, Hibernate, JSP stack. The LocalSessionFactoryBean could solve the problem for one sessionFactory. How would I achieve the same with Spring's support for Hibernate?

Thanks
Krishna
Reply With Quote
  #2  
Old Nov 30th, 2009, 04:51 PM
krishna81m krishna81m is offline
Junior Member
 
Join Date: Nov 2009
Posts: 8
Default

Just a note that I need to be able to switch between session factories at run time and not just have multiple session factories here.
Reply With Quote
  #3  
Old Nov 30th, 2009, 06:45 PM
krishna81m krishna81m is offline
Junior Member
 
Join Date: Nov 2009
Posts: 8
Default

Found a solution at http://mdeinum.wordpress.com/2007/01...ient-database/ but need help in configuring this!

This article discusses exactly what I have done in my Struts/Hibernate stack, all is well
The author suggests using a ContextSwappableTargetSource implements TargetSource, InitializingBean but I am unaware as to where this goes into my xml declarations.

I could, at application startup, load a bunch of sessionfactories and store it in a hashMap but how would I configure this in my Spring/Struts/Hibernate app?

I have the following configuration:

applicationContext-hibernate.xml

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--<property name="mappingResources">
<value>classpath:database/*.hbm.xml</value>
</property>-->
<property name="mappingDirectoryLocations">
<value>WEB-INF/classes/database</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQ L5Dialect</prop>
</props>
</property>
</bean>

<!-- org.hibernate.dialect.HSQLDialect -->

<bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="userDAO" class="dao.hibernate.UserDAOHibernate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

My DAO's


public class UserDAOHibernate extends HibernateDaoSupport implements UserDAO {

public List getUsers() {
return getHibernateTemplate().find("from User");
}

}


From what I understand here, HibernateDaoSupport gets assigned the sessionFactory via setterInjection but in my case all of my Managers are part of singleton Struts Action classes meaning, these managers are linked to just one sessionFactory once they are created. What changes should I be making to lookup the sessionFactory at runTime based on a key assigned to a threadLocal variable? Did anybody successfully implement this on sessionFactories?

Thanks
Krishna
Reply With Quote
  #4  
Old Nov 30th, 2009, 07:09 PM
krishna81m krishna81m is offline
Junior Member
 
Join Date: Nov 2009
Posts: 8
Default

I guess I can create my customHibernateDaoSupport extending
import org.springframework.orm.hibernate3.support.Hiberna teDaoSupport; All of the DAO's would extend this new DAO.

and overriding the default
public SessionFactory getSessionFactory() {
return sessionFactory;
}

where the sessionFactory would now be from a map of sessionFactories aquired based on a key from a ThreadLocal object. All of the methods use getSessionFactory() method above which would then be from my custom method.

Create a customTransactionManager that is passed a customHibernateTransactionManager that extends
import org.springframework.orm.hibernate3.HibernateTransa ctionManager;
and overrides
public SessionFactory getSessionFactory() {
return sessionFactory;
}

Again most of the methods use the getSessionFactory() which I will be modifying without any issues to correctly return the right sessionFactory. One method however the afterPropertiesSet (InitalizingBean implementation) uses

DataSource sfds = SessionFactoryUtils.getDataSource(getSessionFactor y());
if (sfds != null) {
// Use the SessionFactory's DataSource for exposing transactions to JDBC code.
}
setDataSource(sfds);
}

I guess I have to modify getDataSource() again. Only issues are in the cases of singleton action classes that use the same Manager or DAO's configured only once.

Is this good enough? Or am I missing something guys? anybody?

Help
Krishna
Reply With Quote
  #5  
Old Dec 3rd, 2009, 06:43 AM
SIqbal SIqbal is offline
Junior Member
 
Join Date: Jan 2007
Posts: 16
Default

Is there a specific reason for you to choose Struts 1.x over 2.1?
__________________
Saeed Iqbal
Independant Consultant (Freelance)
J2EE - Application Architect / Developer
http://code.google.com/p/startsoft/ Open Source Developer
Reply With Quote
  #6  
Old Dec 3rd, 2009, 08:16 AM
krishna81m krishna81m is offline
Junior Member
 
Join Date: Nov 2009
Posts: 8
Default

Thanks for stopping by. The development team is familiar with struts1.x

Krishna
Reply With Quote
  #7  
Old Dec 3rd, 2009, 10:43 PM
SIqbal SIqbal is offline
Junior Member
 
Join Date: Jan 2007
Posts: 16
Default

Sounds Good!
__________________
Saeed Iqbal
Independant Consultant (Freelance)
J2EE - Application Architect / Developer
http://code.google.com/p/startsoft/ Open Source Developer
Reply With Quote
  #8  
Old Dec 4th, 2009, 02:59 AM
pzoio pzoio is offline
Junior Member
 
Join Date: Nov 2004
Posts: 25
Default

Quote:
Originally Posted by krishna81m View Post
DataSource sfds = SessionFactoryUtils.getDataSource(getSessionFactor y());
if (sfds != null) {
// Use the SessionFactory's DataSource for exposing transactions to JDBC code.
}
setDataSource(sfds);
}
I think you are on the right track with the DataSource. I have solved a similar problem in the past through a custom DataSource implementation which wraps multiple other DataSource instances, and uses the user credentials to dynamically select and delegate to the correct data source. You still end up with a single SessionFactory, just delegating to different data sources.

Phil Zoio
Impala - simple dynamic modules for Spring
http://www.impalaframework.org/
http://impalablog.blogspot.com/
Reply With Quote
  #9  
Old Dec 4th, 2009, 05:42 PM
krishna81m krishna81m is offline
Junior Member
 
Join Date: Nov 2009
Posts: 8
Default

Aaah, finally some confidence. I was actually waiting for someone to comment before proceeding cause I have not worked on a dedicated spring project before.

I will definitely post my workings once I have the code built and then tested.

Thanks
Krishna
Reply With Quote
  #10  
Old Jan 29th, 2010, 03:47 PM
smadarapu smadarapu is offline
Junior Member
 
Join Date: Jan 2010
Posts: 17
Default

Quote:
Originally Posted by krishna81m View Post
Aaah, finally some confidence. I was actually waiting for someone to comment before proceeding cause I have not worked on a dedicated spring project before.

I will definitely post my workings once I have the code built and then tested.

Thanks
Krishna
Hi Krishna

Did you have a solution, can you post any details of your solution. Thanks in advance.

Srikanth
Reply With Quote
Reply

Tags
datasource at runtime, dynamic datasource

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 09:28 PM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.