?
Solved

Datasource routing using AbstractRoutingDataSource with JNDI

Posted on 2009-05-17
4
Medium Priority
?
3,092 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month8 days, 13 hours left to enroll

764 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