Solved

Datasource routing using AbstractRoutingDataSource with JNDI

Posted on 2009-05-17
4
3,005 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

821 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