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