Hi folks,
In last couple of weeks, My team was working on configuring our application's Hibernate layer to use multiple database schema. Following are the steps we followed:
- created hibernate.properties and hibernate.cfg.xml for every database schema with different name eg. for schema zMRDB, we have created hibernatezMRDB.properties and hibernatezMRDB.cfg.xml files.
- don't use hibernate.properties and hibernate.cfg.xml default files for any schema
- Finally at the time of Session Factory creation use following code
- Use SessionFactory returned by method in STEP 3 to open and use session with specific Database.
public static SessionFactory getSessionFactory() { if (sessionFactory == null) { try { final Configuration cfg = new Configuration(); Properties connectionProperties = new Properties(); InputStream inStream = ThreadLocalUtil.class.getClassLoader().getResourceAsStream("hibernatezMRDB.properties"); connectionProperties.load(inStream); cfg.setProperties(connectionProperties); sessionFactory = cfg.configure("hibernatezMRDB.cfg.xml").buildSessionFactory(); } catch (Throwable ex) { ex.printStackTrace(); } } return sessionFactory; }
No comments:
Post a Comment