-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Dynamic Database Switching -Hibernate Template Problem
PostPosted: Thu Apr 22, 2010 5:03 am 
Newbie

Joined: Wed Apr 21, 2010 5:49 am
Posts: 2
Hi,

My application has to switch the database connection dynamically whenever i change the ipaddress and the database name.The following code works fine when i create the session factory using ClassPathXmlApplicationContext. My application context file looks like this.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" .........

   <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
      <property name="environment">
         <props>
            <prop key="java.naming.factory.initial">com.sun.jndi.fscontext.RefFSContextFactory
            </prop>
            <prop key="java.naming.provider.url">file:D:/temp</prop>
         </props>
      </property>
   </bean>

   <bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiTemplate">
         <ref bean="jndiTemplate" />
      </property>
      <property name="jndiName">
         <value>MYCONNECTIONKEY</value>
      </property>
   </bean>

   <bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

      <property name="dataSource">
         <ref bean="jndiDataSource" />

      </property>

      <property name="annotatedClasses">
         <list>
            <value>com.kumaran.testing.UserDetailsDTO
            </value>
         </list>
      </property>

      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">
               org.hibernate.dialect.MySQLInnoDBDialect
            </prop>
         </props>
      </property>

   </bean>

   <bean id="testDAO" class="com.kumaran.testing.TestDAO">
      <property name="sessionFactory" ref="sessionFactory" />
   </bean>

</beans>
and i binded the MYCONNECTIONKEY value using the following code

Code:
InputStream cpdsPropFile = null;
      System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
      System.setProperty(Context.PROVIDER_URL, "file:D:/temp");
      try {
         Context registry = new InitialContext();
         // create a new connection pool data source, read the properties
         // file
         // and bind it to a name
         ComboPooledDataSource cpds = new ComboPooledDataSource();
         Properties dsProperties = new Properties();
         cpdsPropFile = TestDAO.class.getClassLoader().getResourceAsStream("connection.properties");
         dsProperties.load(cpdsPropFile);
         cpds.setDriverClass(dsProperties.getProperty("driverClass"));
         cpds.setUser(dsProperties.getProperty("user"));
         cpds.setDescription("MYCONNECTIONKEY" + " Data Source");
         cpds.setPassword(dsProperties.getProperty("password"));
         String jdbcURL = dsProperties.getProperty("jdbcUrl1") + ip + dsProperties.getProperty("jdbcUrl2")
               + tableName;
         cpds.setJdbcUrl(jdbcURL);

         if (cpds.getConnection() != null) {
            // Success
            System.out.println("connection created successfully");
            registry.rebind("MYCONNECTIONKEY", cpds);

            // "closing" a connection sends it back to the pool
            // cpds.getConnection().close();
         } else {
            // Something wrong
            System.out.println("connection not established");
         }
      } catch (Exception e) {
         e.printStackTrace();
      }

The application switches the database whenever i reloaded the applicationContext.xml after dynamically changing the database using

Code:
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("/applicationContext.xml");
   sessionFactory=(SessionFactory)context.getBean("sessionFactory");

It creates the new session instance whenever the applicationContext.xml is binded with the new MYCONNECTIONKEY value.

But the problem is SpringUtil.getBean("sessionFactory") doesnt returns the new instance for dynamic changes and getHibernateTemplate() is not working .Can any one help me to solve this issue.

Is there any other way to switch the database connection dynamically?If so help me out.


Thanks in advance

Logeswaran Radhakrishnan


Top
 Profile  
 
 Post subject: Re: Dynamic Database Switching -Hibernate Template Problem
PostPosted: Tue Nov 30, 2010 4:25 pm 
Newbie

Joined: Tue Mar 02, 2010 4:28 pm
Posts: 8
this may help :
http://dutoutetdurien.wordpress.com/201 ... switching/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.