Multiple databases specified in hibernate.properties
Wednesday, February 25th, 2004Looking at a little tool project and I’m going to give Hibernate another kick at the can. Used it before on another small project in the past.
On this project, I’ll be connecting to 2 different datasources. The first datasource I’ll just be doing read-only queries. The second datasource I’ll be inserting data based on what I read & rearrange from the first datasource. I’m looking at how to specify using multiple databases. I can’t see any way to give a name to a datasource within hibernate.properties to distinguish between the two.
One way I was thinking I might have to do this is create 2 different hibernate.properties files (ie, ds1.properties & ds2.properties) and then passing whichever one I needed at that time into Configuration.setProperties(). Is there any smarter/easier way to do this? I’ve got this working and it seems alright.
Update: this didn’t turn out to be too difficult. I just make 2 different calls to my cfg files when I need my connection. Maybe there’s a more elegant way of doing this, but it works fine for me.
foo.java:
SessionFactory sf = new Configuration().configure("/configA.cfg.xml").buildSessionFactory();
Session sess = sf.openSession();bar.java:
SessionFactory sf = new Configuration().configure("/configB.cfg.xml").buildSessionFactory();
Session sess = sf.openSession();



