Results 1 to 6 of 6
- 06-18-2007, 08:12 AM #1
Member
- Join Date
- Jun 2007
- Posts
- 2
- Rep Power
- 0
- 06-18-2007, 08:23 AM #2
Check the thread below. It may solve your problem.
http://www.java-forums.org/database/...-database.html
- 06-18-2007, 10:41 AM #3
Member
- Join Date
- Jun 2007
- Posts
- 2
- Rep Power
- 0
hi
i have worked with multiple databases using two hibernate.cfg.xml and writing two seperate methods to get the session factory object for the different database. Is it the wright way? Or is there any better way of doing this?
Thanks
- 07-12-2007, 07:29 AM #4
Member
- Join Date
- Jul 2007
- Posts
- 1
- Rep Power
- 0
try this
Java Code:public class HibernateUtil { private static Log log = LogFactory.getLog(HibernateUtil.class); private static HashMap<String, SessionFactory> sessionFactoryMap = new HashMap<String, SessionFactory>(); public static final ThreadLocal sessionMapsThreadLocal = new ThreadLocal(); public static Session currentSession(String key) throws HibernateException { HashMap<String, Session> sessionMaps = (HashMap<String, Session>) sessionMapsThreadLocal.get(); if (sessionMaps == null) { sessionMaps = new HashMap(); sessionMapsThreadLocal.set(sessionMaps); } // Open a new Session, if this Thread has none yet Session s = (Session) sessionMaps.get(key); if (s == null) { s = ((SessionFactory) sessionFactoryMap.get(key)).openSession(); sessionMaps.put(key, s); } return s; } public static Session currentSession() throws HibernateException { return currentSession(""); } public static void closeSessions() throws HibernateException { HashMap<String, Session> sessionMaps = (HashMap<String, Session>) sessionMapsThreadLocal.get(); sessionMapsThreadLocal.set(null); if (sessionMaps != null) { for (Session session : sessionMaps.values()) { if (session.isOpen()) session.close(); }; } } public static void closeSession() { HashMap<String, Session> sessionMaps = (HashMap<String, Session>) sessionMapsThreadLocal.get(); sessionMapsThreadLocal.set(null); if (sessionMaps != null) { Session session = sessionMaps.get(""); if (session != null && session.isOpen()) session.close(); } } public static void buildSessionFactories(HashMap<String, String> configs) { try { // Create the SessionFactory for (String key : configs.keySet()) { URL url = HibernateUtil.class.getResource(configs.get(key)); SessionFactory sessionFactory = new Configuration().configure(url).buildSessionFactory(); sessionFactoryMap.put(key, sessionFactory); } } catch (Exception ex) { ex.printStackTrace(System.out); log.error("Initial SessionFactory creation failed.", ex); throw new ExceptionInInitializerError(ex); } // end of the try - catch block } public static void buildSessionFactory(String key, String path) { try { // Create the SessionFactory URL url = HibernateUtil.class.getResource(path); SessionFactory sessionFactory = new Configuration().configure(url).buildSessionFactory(); sessionFactoryMap.put(key, sessionFactory); } catch (Throwable ex) { log.error("Initial SessionFactory creation failed.", ex); throw new ExceptionInInitializerError(ex); } // end of the try - catch block } } // end of the classLast edited by JavaBean; 07-12-2007 at 07:43 AM. Reason: Code placed inside [code] tag
- 05-22-2008, 10:06 AM #5
Member
- Join Date
- May 2008
- Posts
- 1
- Rep Power
- 0
there is a method called currentSession(String key) inside the class which need s method closeSession(String key). I try to add it.I don't know if it is right.please feel free to propose your suggestions.
-----codes as below
public static void closeSession(String key) {
HashMap<String, Session> sessionMaps = (HashMap<String, Session>) sessionMapsThreadLocal.get();
if (sessionMaps != null) {
Session session = sessionMaps.get(key);
if (session != null && session.isOpen())
session.close();
}
}
- 06-23-2008, 01:06 PM #6
Member
- Join Date
- Jun 2008
- Posts
- 1
- Rep Power
- 0
Accessing two schema from spring and hibernate
Hi,
I am new to hibernate and Spring
My application use spring , struts and hibernate mapping. Its cuurently connecting to a single database
The requirement is to create a new schema and to fetch the data from both the schemas ….. I need to take data from both the schemas…. Both the schemas are having the same set of tables (Table names are same across the schemas as the new one is the archival schema )
In my dataAccessContext-jta.xml , datasource is defined as follows….
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc racle:thin:@someserver</value>
</property>
<property name="username">
<value>usrname</value>
</property>
<property name="password">
<value>passwd/value>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<constructor-arg>
<ref bean="dataSource"/>
</constructor-arg>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean"> <property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>trial.hbm.xml</value> </list> </property> <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.O racle9Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.region_prefix">hibernate.cach e</prop>
<prop key="hibernate.cache.provider_class"> net.sf.hibernate.cache.EhCacheProvider </prop> </props></property></bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate.Hibernate TransactionManager"> <property name="sessionFactory">
<ref local="sessionFactory"/>
</property> </bean>
My question is whether its possible to connect to two different schema s using hibernate … If so how can we go about it… Here we are usnig hibernate transaction manager .. Please explain in detail as I dont have any idea of how to go about it... Please help me in this…
Thanks
Similar Threads
-
Hibernate with multiple database
By Marty in forum JDBCReplies: 3Last Post: 12-23-2008, 12:57 PM -
how to use XML databases
By paty in forum XMLReplies: 3Last Post: 08-16-2008, 12:11 AM -
Recommendation about databases
By tommy in forum JDBCReplies: 2Last Post: 07-28-2007, 05:04 AM -
multiple databases
By varunthecool in forum JDBCReplies: 2Last Post: 07-09-2007, 08:06 AM -
org.hibernate.ejb.Version <clinit> INFO: Hibernate EntityManager 3.2.0.CR1
By Heather in forum JDBCReplies: 2Last Post: 06-30-2007, 03:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks