How to create factory and transaction manager at run time?

Asked on October 21, 2013
In my application, I use Spring and Hibernate with latest version. As per new Spring and Hibernate version I couldn't user HibernateDaoSupport as it doesn't support by latest version of spring. So I implemented hibernate with spring by using normal flow like using @Transactional annotation, created factory using LocalSessionFactoryBean, created transaction manager using HibernateTransactionManager and attached factory bean in transaction manager. This way it works fine.

As per flow of project when user get logged in to application, he will get list of some cases. For each case I have different schema in database. Credential details are in master schema means I have total n+1 schema (n cases + master schema).

I have applied above mentioned way to configure master schema details as it fix. But I have query how to implement same thing on case selection? I have to create factory object dynamically (run time) based on selected case. i.e. when user select case from list, I have to create factory of that schema. So how to get this done?

1. How to define factory (LocalSessionFactoryBean) run time ?
2. How to create transaction manager run time and attach factory created in first step with this transaction manager?

Please provide me the workaround for it how to achieve my goal here?
Hope I am clear with my questions and scenario.

Jigar Shah

Replied on October 21, 2013
In addition to above, cases are not fixed. New cases can be added so couldn't specify all data sources in xml as it would not work for newly added cases. I want complete things dynamic so when new case has been added, it should work without any changes in code or configuration.

Replied on October 23, 2013
Hi Jigar,

You need to try PropertyPlaceholderConfigurer. That will enable changing in  database configuration at run time. You can get help by below URL how to use PropertyPlaceholderConfigurer.

Replied on October 23, 2013
Thanks for your reply..

Actually PropertyPlaceholderConfigurer would not work for me. We don't have anything in property file. We have all schema details of various cases in master database.

So when user select case, I have to get all details from database which need to create DataSource and have to create SessionFactory and TransactionManager. All things want to create dynamically.

Even more cases can be added so it should work without any changes.

Replied on October 23, 2013
You can explore one thing more. Do not use XML to get session factory.
Use @Configuration instead of spring context xml.

Read the two below example and then you can do the changes like

And then you can do the changes like

public SessionFactory sessionFactory() {
// make conditional on the basis of selection of database.
Properties prop= new Properties();
prop.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate");
prop.setProperty("hibernate.connection.username", "root");
prop.setProperty("hibernate.connection.password", "");
prop.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
concreteSessionFactory = new AnnotationConfiguration()


return concreteSessionFactory ;

Replied on October 23, 2013
I am not getting SessionFactory through XML. I have mentioned in my previous posts that I have to create SessonFactory and TransactionManager dynamically so naturally nothing is in XML.
Even datasource details like uname, pwd all comes from database dynamically when I select case.

I am not getting how to make it possible with the way you mentioned?

Replied on May 27, 2014
Hi Jigar, did you solve your problem?Because, I am also stuck with the same issue. Please share some links if you got solution..

Write Answer

©2024 | Privacy Policy | Contact Us