0

I am developing a Java application using Spring + Hibernate. i want this application to run on more then one Database on same time.

For Example if user tries to search for some data, Application has to search for that data in all the configured data-sources at a same time.

I am looking for a solution which will create different threads for each data-source and when user perform any operation all the threads needs to perform that operation

Edit 1 Let me explain my problem in detail below is my DAO class

@Repository("engineDAO")
public class EngineDAOImpl implements EngineDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void persistEngine(Engine engine, String[] DataSourceNames) {
        sessionFactory.getCurrentSession().persist(engine);
    }
}

Now from service class i will call persistEngine method of EngineDAO class with argument as String[] DataSourceNames so this operation needs to be performed on all the Data-sources provided as argument. What all changes i have to make for this? Thanks in advance

Kushal
  • 71
  • 8
  • So the databases will have identical content? Or does your application have to decide which database or databases to store each item in? – Dawood ibn Kareem Mar 05 '15 at 07:34
  • Did you think about database replication instead? – Uwe Allner Mar 05 '15 at 07:35
  • It’s like I am trying to examine the stat of data for all the configured database. So application has to perform the same operation on all the configured databases. – Kushal Mar 05 '15 at 07:42
  • If all the databases have to have identical content, then you should use a replication solution, rather than trying to implement this at the level of an application. Read up on the replication options that are available for whatever DBMS you are using. – Dawood ibn Kareem Mar 05 '15 at 07:49
  • Content will be different in all the database, operations which application will perform will be same. – Kushal Mar 05 '15 at 09:01

1 Answers1

1

You can have multiple SessionFactory or EntityManagerFactory objects, each associated with different DataSource. If you want to manage transaction across different datasources I would recommend to use JTA Transaction Manager. If your application is not running in Java EE environment, you can use some 3rd party JTA transaction manager, for example Atomikos Transaction Manager

There are some threads at Stackoverflow that discuss about this problem. Try this

Edit 1: If you neet to select datasources by name, than your DAO can implement BeanFactoryAware and you will obtain BeanFactory object, which you can use for accessing SessionFactory beans by name. Your code should look like something like that

@Repository("engineDAO")
public class EngineDAOImpl implements EngineDAO, BeanFactoryAware {

    private org.springframework.beans.factory.BeanFactory beanFactory;

    @Override
    public void persistEngine(final Engine engine, final String[] sessionFactoryNames) {
        for (final String sessionFactoryName : sessionFactoryNames) {
            final SessionFactory sessionFactory = beanFactory.getBean(sessionFactoryName, SessionFactory.class);
            sessionFactory.getCurrentSession().persist(engine);
        }
    }

    @Override
    public void setBeanFactory(final BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }
}
Community
  • 1
  • 1
Maroš Tyrpák
  • 305
  • 1
  • 6
  • The problem with given solution is, each time you are adding a new data source you have to go and add a new session factory object in your code, I am looking for some generic solution. – Kushal Mar 05 '15 at 09:05
  • Yes, but you don't have to change your code to add a new `SessionFactory`. You just need some class that is able to create `SessionFactory` objects. This is as generic as you're going to get. @Kushal – Dawood ibn Kareem Mar 05 '15 at 10:40
  • It might be a vary basic thing, but i don't know how to write that generic class which i can use to create `SessionFactory` can you help me out.. – Kushal Mar 05 '15 at 11:06