|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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.
|
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
Thanks for stopping by. The development team is familiar with struts1.x
Krishna |
|
#7
|
|||
|
|||
|
Sounds Good!
__________________
Saeed Iqbal Independant Consultant (Freelance) J2EE - Application Architect / Developer http://code.google.com/p/startsoft/ Open Source Developer |
|
#8
|
|||
|
|||
|
Quote:
Phil Zoio Impala - simple dynamic modules for Spring http://www.impalaframework.org/ http://impalablog.blogspot.com/ |
|
#9
|
|||
|
|||
|
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 |
|
#10
|
|||
|
|||
|
Quote:
Did you have a solution, can you post any details of your solution. Thanks in advance. Srikanth |
![]() |
| Tags |
| datasource at runtime, dynamic datasource |
| Thread Tools | |
| Display Modes | |
|
|