Results 1 to 2 of 2

Thread: Most efficient way to configure a lot of JDBC templates

  1. #1
    Join Date
    Mar 2005
    Posts
    9

    Default Most efficient way to configure a lot of JDBC templates

    We have one database per state (e.g. CA, NY, FL) and our DAO layer may need to get a JdbcTemplate for any of the 50 state databases.

    What's the most efficient way in Spring to configure the 50 jdbc templates? Do I have to enumerate each one?

    And is there some sort of JdbcTemplateFactory I can dependency inject in my DAO's and then in the DAO say factory.getJdbcTemplate("FL");

    Thanks,
    Todd

  2. #2
    Join Date
    Oct 2004
    Location
    Antwerp, Belgium
    Posts
    96

    Default Re: Most efficient way to configure a lot of JDBC templates

    Quote Originally Posted by thuss
    We have one database per state (e.g. CA, NY, FL) and our DAO layer may need to get a JdbcTemplate for any of the 50 state databases.

    What's the most efficient way in Spring to configure the 50 jdbc templates? Do I have to enumerate each one?

    And is there some sort of JdbcTemplateFactory I can dependency inject in my DAO's and then in the DAO say factory.getJdbcTemplate("FL");
    Maybe this will work for you:

    Code:
    <bean name="CA_dataSource" class="...">
        properties ...
    </bean>
    
    <bean name="dataSourceMap" class="java.util.HashMap">
        <constructor-arg>
           <map>
              <entry key="CA"><ref bean="CA_dataSource"/></entry>  
           </map>
       </constructor-arg>
    </bean>
    
    <bean name="myDao" class="...">
       <property name="dataSourceMap">
          <ref bean="dataSourceMap"/>
       </property>
    </bean>
    In your DAO you could then do this:

    Code:
    private Map dataSourceMap = null;
    
    public void setDataSourceMap&#40;Map dataSourceMap&#41; &#123; this.dataSourceMap = dataSourceMap; &#125;
    
    private JdbcTemplate getJdbcTemplate&#40;String state&#41; &#123;
       return new JdbcTemplate&#40;&#40;DataSource&#41;this.dataSourceMap.get&#40;state&#41;&#41;;
    &#125;

Similar Threads

  1. Replies: 4
    Last Post: May 11th, 2012, 08:34 AM
  2. Spring JDBC for unit tests?
    By amkush in forum Data
    Replies: 7
    Last Post: Aug 20th, 2008, 02:29 PM
  3. stale Oracle processes
    By compostellas in forum Data
    Replies: 7
    Last Post: Jun 27th, 2005, 12:14 PM
  4. Replies: 0
    Last Post: Apr 6th, 2005, 08:24 AM
  5. JDBC Abstraction and Spring
    By spring04 in forum Data
    Replies: 5
    Last Post: Nov 22nd, 2004, 03:27 PM

Posting Permissions

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