Hi,
I am required to do datasource routing using AbstractRoutingDataSource.
java in my web application. We are using JNDI for datasource mapping. Here's the code:
----------------------Dyna
micDatasou
rceRouter.
java------
----------
----
package com.walmart.eai.util;
import java.sql.SQLException;
import java.sql.Wrapper;
import javax.sql.DataSource;
import org.springframework.jdbc.d
atasource.
lookup.Abs
tractRouti
ngDataSour
ce;
public class DynamicDatasourceRouter extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey(
)
{
return EnvironmentContextHolder.g
etEnvironm
entType();
}
}
--------------------------
------Envi
ronmentCon
textHolder
.java-----
----------
----------
----------
--------
package com.walmart.eai.util;
public class EnvironmentContextHolder {
private static final ThreadLocal<EnvironmentTyp
e> contextHolder = new ThreadLocal<EnvironmentTyp
e>();
public static void setEnvironmentType(Environ
mentType environmentType){
if (environmentType == null) System.out.println("Enviro
nmentType cannot be null");
contextHolder.set(environm
entType);
}
public static EnvironmentType getEnvironmentType(){
return (EnvironmentType) contextHolder.get();
}
public static void clearEnvironmentType(){
contextHolder.remove();
}
}
--------------------------
----------
---Environ
mentType.j
ava-------
----------
-------
package com.walmart.eai.util;
public enum EnvironmentType {
C1,
C2,
}
--------------------------
----------
applicatio
nContext.x
ml--------
----------
<bean id="eaiDataSource" class="com.walmart.eai.uti
l.DynamicD
atasourceR
outer">
<property name="targetDataSources">
<map key-type="com.walmart.eai.
util.Envir
onmentType
">
<entry key="C1" value="JNDI1" />
<entry key="C2" value="JNDI2" />
</map>
</property>
<property name="defaultTargetDataSou
rce" value="JNDI1" />
</bean>
--------------------------
----------
----------
----------
----------
----------
----------
-------
I am setting the value of the EnvironmentType of the EnvironmentContextHolder just before querying the DB as follows:
EnvironmentContextHolder.s
etEnvironm
entType(En
vironmentT
ype.C2);
--------------------------
----------
----------
----------
----------
----------
----------
---------
But this not working. Where am I going wrong?