Results 1 to 5 of 5

Thread: Help to understand some concepts of AbstractRoutingDataSource

Hybrid View

  1. #1
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default Help to understand some concepts of AbstractRoutingDataSource

    I need som help understanding AbstractRoutingDataSource.

    There are some aspects of my AbstractRoutingDataSource I do not understand.

    This is my recipe:
    1: Created an Enumeration(EnvironmentType) of the different data sources.
    2: Created an EnvironmentContextHolder.
    3: Created a RoutingDataSource which extends AbstractRoutingDataSource
    This has one overrided method determineCurrentLookupKey which return an EnvironmentContextHolder.getEnvironmentType().

    So when I switch datasource all I do is
    EnvironmentContextHolder.setEnvironmentType(Enviro nmentType.PP1);

    The RoutingDataSource is wired in my application context file.

    What I don't understand is where is the current datasource stored? I get it that the ContextHolder is to hold that value, but who is holding an instance of the contextholder? How is the ThreadLocal variable tied to my RoutingDataSource defined in the application context?

    My EnvironmentContextHolder code:
    Code:
    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();
          }

  2. #2
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    It seems like my RoutingDataSource implementation of AbstractRoutingDataSource is using the ThreadLocal contextholder to retrieve the current datasource and set a new datasource.

    I looked in the code of AbstractRoutingDataSource. I could not find anything that could explain how it accessed this ThreadLocal contextholder I have.

  3. #3
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Do you have set up a map of data sources and pass it to your RoutingDataSource? The EnvironmentType returned by determineCurrentLookupKey() is used to lookup the data source in this map.

    Jörg

  4. #4
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    Quote Originally Posted by Jörg Heinicke View Post
    Do you have set up a map of data sources and pass it to your RoutingDataSource? The EnvironmentType returned by determineCurrentLookupKey() is used to lookup the data source in this map.

    Jörg
    I understand that. But I was wondering how the current datasource value is handled. The map is only a list of the different datasources, but what if datasource 4 is the one being used. I recon it is the ThreadLocal variable which knows which datasource is the current one. I did not understand how AbstractRoutingDataSource was using the ThreadLocal to find this value. I could not see any threading references in the source to look this value up.

    My dataSource bean
    Code:
          <bean id="dataSource" class="datasource.RoutingDataSource">
                <property name="targetDataSources">
                      <map key-type="datasource.EnvironmentType">
                            <entry key="PP2" value-ref="dataSourcePP2" />
                            <entry key="PP3" value-ref="dataSourcePP3" />
                            <entry key="PP4" value-ref="dataSourcePP4" />
                            <entry key="ST1" value-ref="dataSourceST1" />
                            <entry key="ST2" value-ref="dataSourceST2" />
                            <entry key="ST3" value-ref="dataSourceST3" />
                            <entry key="ST4" value-ref="dataSourceST4" />
                      </map>
                </property>
                <property name="defaultTargetDataSource" ref="dataSourcePP1" />
          </bean>
    And I have defined several beans for the different datasources
    Code:
        <bean id="parentDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" abstract="true">
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
            <property name="username" value="******"/>
            <property name="password" value="******"/>
        </bean>
    
        <bean id="dataSourcePP1" parent="parentDataSource">
            <property name="url" value="jdbc:oracle:thin:@****"/>
        </bean>
    ....
    Last edited by DJViking; Jul 18th, 2007 at 03:04 AM.

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    It doesn't hold a reference... For each thread and for each call to the dao the method determineTargetDataSource is called. (Well only one method will probably be called and that is getConnection or the one with params). When the connection is returned to the dao which needs it (for that thread) there is no need to hold a reference to the current datasource.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •