Solved

Datasource routing using AbstractRoutingDataSource with JNDI

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

717 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