[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Datasource routing using AbstractRoutingDataSource with JNDI

Posted on 2009-05-17
4
Medium Priority
?
3,119 Views
Last Modified: 2015-01-05
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:

----------------------DynamicDatasourceRouter.java--------------------
package com.walmart.eai.util;
import java.sql.SQLException;
import java.sql.Wrapper;
import javax.sql.DataSource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDatasourceRouter extends AbstractRoutingDataSource{
            
    @Override
    protected Object determineCurrentLookupKey()
    {
       return EnvironmentContextHolder.getEnvironmentType();
          
    }
   
}


--------------------------------EnvironmentContextHolder.java-------------------------------------------
package com.walmart.eai.util;

public class EnvironmentContextHolder {
      

      private static final ThreadLocal<EnvironmentType> contextHolder = new ThreadLocal<EnvironmentType>();

      public static void setEnvironmentType(EnvironmentType environmentType){
      if (environmentType == null) System.out.println("EnvironmentType cannot be null");
      contextHolder.set(environmentType);
      }

      public static EnvironmentType getEnvironmentType(){
      return (EnvironmentType) contextHolder.get();
      }

      public static void clearEnvironmentType(){
      contextHolder.remove();
      }

}

---------------------------------------EnvironmentType.java------------------------
package com.walmart.eai.util;

public enum EnvironmentType {
      C1,
      C2,
      }

------------------------------------applicationContext.xml------------------
<bean id="eaiDataSource" class="com.walmart.eai.util.DynamicDatasourceRouter">
            <property name="targetDataSources">
             <map key-type="com.walmart.eai.util.EnvironmentType">
            <entry key="C1" value="JNDI1" />
                        <entry key="C2" value="JNDI2" />
                        
                  </map>
            </property>
            <property name="defaultTargetDataSource" value="JNDI1" />
            
      </bean>

---------------------------------------------------------------------------------------------
I am setting the value of the EnvironmentType of the EnvironmentContextHolder just before querying the DB as follows:
EnvironmentContextHolder.setEnvironmentType(EnvironmentType.C2);

-----------------------------------------------------------------------------------------------
But this not working. Where am I going wrong?
0
Comment
Question by:cuttieveenz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:cuttieveenz
ID: 24405774
After debugging I found that the above code is working fine. The JNDI is getting correctly mapped to the datasource. The datasource object is of type com.ibm.ws.rsadapter.jdbc.wsjdbcdatasource. But when the getConnection() is called it's giving error from Database side.

IO Exception opening socket to server USMUMVSALVI2 on port 50000.  The DB2 Server may be down.
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 24405797
is the database listeniig on that port?

0
 

Author Comment

by:cuttieveenz
ID: 24405821
Hmmm,....I think u r right....When I tried to do 'Test Connection' on this datasource through the webphere administrative console, it gave me the same error. Then this shouldn't be the web application problem.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question