persistence of data in two or more banks simultaneously [closed]

1

Greetings!

I would like someone to help me with some example of a JSF application that persists data simultaneously in more than one bank, ie: it will connect to 2+ banks and inject data into them.

I'm using Hibernate.

The project is in: link

Thank you!

    
asked by anonymous 03.09.2015 / 14:07

1 answer

2

Already, I did it!

If anyone has the same question, I'll share my solution here:

  • In addition to hibernate.cfg.xml, I created another file that I called hibernate1.cfg.xml in which I include the sessionfactory of the other bank. NOTE: In my project, the first hibernate.cfg.xml is for MySQL and the second for PostgreSQL.

  • In the package br.com.meuprojeto.util I already had the class HibernateUtil.java. So I created another class, this time HibernateUtil1.java whose implementation follows:

  • import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry;

    public class HibernateUtil1 {

    private static final SessionFactory sessionFactory = buildSessionFactory();
    
    private static SessionFactory buildSessionFactory() {
    
        try {
            // Cria SessionFactory a partir do hibernate.cfg.xml
            Configuration configuration = new Configuration();
            configuration.configure("hibernate1.cfg.xml").buildSessionFactory();
    
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();
            SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            return sessionFactory;
    
        } catch (Throwable ex) {
            System.err.println("Falha ao tentar criar o SessionFactory." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    

    }

  • In my package br.com.myuprojeto.main, I have the GeraTabela.java class with the main method calling the two classes HibernateUtil and HibernateUtil1, as follows:
  • public class GeraTabela {

    public static void main(String[] args) {
        HibernateUtil.getSessionFactory();
        HibernateUtil.getSessionFactory().close();
    
        HibernateUtil1.getSessionFactory();
        HibernateUtil1.getSessionFactory().close();
    
    }
    

    }

    It may not be the most elegant solution. But I ran this class and there were the tables created in both banks.

    Thanks to those who accompanied!

        
    03.09.2015 / 14:43