Solved

Datasource routing using AbstractRoutingDataSource with JNDI

Posted on 2009-05-17
4
2,937 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
  • 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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now