PDA

View Full Version : Transaction Management for one datasource and two hibernate session factories



kamalpreet.chd@gmail.com
Mar 14th, 2010, 05:57 AM
Hi

I would like to know how to apply/use transactions on a application with "one datasource" and "two hibernate session factories".

Actually our current application uses spring and hibernate. It has one data source and one hibernate session factory. This current application uses "HibernateTransactionManager" provided by spring framework. This "HibernateTransactionManager" knows about session factory.



<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransa ctionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>


As per our new requirement (another application) we have been told to
1) Create new tables in the existing database and hence use same datasource.
2) But create new hibernate session factory (AnnotationSessionFactoryBean) for new tables/schema.
3) Use Spring Transactions, again using annotations (@Transactional)

We started following this path and faced below issues:
1) Not able to update data in DB for new tables. We are using


getHibernateTemplate().update(entity);

but this does not work, because we are not using transactions. To persist changes in DB we are now using:


getHibernateTemplate().update(entity);
getHibernateTemplate().flush();


But we don't want to explicitly flush data, what we want is Transactions. Transactions are necessary for our application.

2) Secondly we cannot change hibernate/spring configuration of our existing application. So which transaction manager to use and how? As "HibernateTransactionManager" is for only one datasource/sessionfactory but we have two session factories.

3) How to use/configure OpenSessionInViewFiler in case of two session factories.

Thanks in advance
Kamal

Marten Deinum
Mar 14th, 2010, 03:30 PM
Just one question why on earth would you want to do that... That is imho a good sample of making a solutions way to complex....

But if you really must it depends on what you want/need, do you need to use 2 sessionfactories in call if so you might need JTA for transaction synchronisation, if you don't you need 2 transaction managers and either spring 3 for declarative transaction management (because you can select which txmanager to use) or do manual transaction management with TransactionTemplate or the PlatformTransactionManager directly.

Also flushing doesn't mean commit!!!