facebook

[Closed] Making Hibernate connections to 2 databases

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #257539 Reply

    MikeKing
    Member

    I have created an application that needs to read databaseA and both read and write databaseB (i.e. 2 databases).
    Apparently I need to configure 2 different datasources and use 2 different sessionFactories.

    MyEclipse gives 3 views to manage the hibernate.cfg.xml file, namely configuration view, design view and source view.
    Can I use the design view or the source view to add a second session-factory to hibernate.cfg.xml? Is this the correct kind of approach?

    Else, what is the best way to use MyEclipse to achieve this?
    Will I have to do it outside MyEclipse?

    Your suggestions will be appreciated.

    Mike King

    #257578 Reply

    Brian Fernandes
    Moderator

    Mike,

    I’m afraid you can only have a single Session Factory in your configuration file – this is not a MyEclipse restriction but a hibernate design principle.

    I would suggest creating a second configuration file for the connection settings to database2 and switching the configuration file pointed to in your program depending on what you wish to achieve.

    Let us know if you require further assistance,
    Brian.

    #257672 Reply

    danielkohl
    Participant

    Hi,

    it’s very easy to connect to 2 (or N) databases:

    You only have create to configurations:

    
        // Define the locations of the hibernate-configurations
        private final String LOCAL_CONFIG_FILE_LOCATION = "/local_hibernate.cfg.xml";
        private final String WEB_CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
        ........
    
            try {
                config = new Configuration().configure(configfile);
                logger.debug("Config: "+config.getProperties());
                        sessionFactory = config.buildSessionFactory();
                logger.debug("Factory: "+sessionFactory.getStatistics());
            } catch (Exception excp) {
                logger.error("Creating configuration for config-file: "+configfile+" failed! Details:"+excp.getMessage());
                config = null;
                sessionFactory = null;
            }
    .......
    
            hibernateSession = config.getFactory().openSession();
    
        }
    

    The code isn’t complete here and it on’t work, but i hope you understand the solution 🙂

    Greetings

    Daniel

    #257818 Reply

    MikeKing
    Member

    Brian & Daniel,

    Thank you for your assistance. I am just starting to set up 2 configuration files and two sessions that use them.

    Mike

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: [Closed] Making Hibernate connections to 2 databases

You must be logged in to post in the forum log in