Announcement Announcement Module
Collapse
No announcement yet.
Proxied Datasource (Multiple Databases) +TheadLocal Question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Proxied Datasource (Multiple Databases) +TheadLocal Question

    Hi all,

    I've read through this forum regarding this topic and I found some entries:

    Proxied Datasource (without Hibernate)
    http://forum.springframework.org/showthread.php?t=15055

    Hibernate and multiple databases
    http://forum.springframework.org/showthread.php?t=10001
    I have an app that connects to multiple datasources but the tables underneath are exactly the same.
    This is also my exact Problem, where I'm looking for a solution.

    1. Create a DatasourceProxy and propagate the id in some thread local variable and call getConnection to retrieve the correct datasource. The datasourceproxy would be preloaded with a map of the userid with the associated datasource.
    My Questions:
    has anyone made some experience with this way?
    Can someone provide some more details howto set up this configuration?
    Or give a hint where to look for some more infos?
    Are there problems, maybe because of connection pooling, because of using only one SessionFactory?
    Maybe "user1" gets a pooled connection where he will access the "user2" - database?

    EDIT:
    Searching in Hibernate Forums, the answer is clear:
    one SF for each Database. :-(
    So from this direction I won't get any help...
    /EDIT

    I need this configuration to get my App (the database) multi-client ready
    so i have a "main-table" (users) and for each user a single db.

    Thank you in advance!

    best regards
    Hans
    Last edited by robyn; May 14th, 2006, 08:44 PM.

  • #2
    Spending some more hours investigating forums and "internet" I got some more information concerning multiple databases.
    This thread:
    "Setting Schema for Each DataSource.getConnection() Call"
    http://forum.springframework.org/showthread.php?t=10728
    is worth reading.

    The Solution now I use for me is simpler,
    I use the TransactionAwareDSProxy as DataSource for Hibernate 3
    Code:
      <bean id="myDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
          <property name="targetDataSource"><ref local="appDataSourceDefault"/></property>
       </bean>
    and I have specified all datasources I need:
    Code:
    	<bean id="appDataSourceDefault" class="org.apache.commons.dbcp.BasicDataSource">
    ...
    	</bean>
    	<bean id="appDataSource1" class="org.apache.commons.dbcp.BasicDataSource">
    ...
    	</bean>
    	<bean id="appDataSource..n" class="org.apache.commons.dbcp.BasicDataSource">
    ...
    	</bean>
    within businessLogic I simply do the following:

    Code:
            ds1 = (BasicDataSource)ctx.getBean("appDataSourceDefault");
            ds2 = (BasicDataSource)ctx.getBean("appDataSource2");
            
            dsProxy = (TransactionAwareDataSourceProxy)ctx.getBean("myDataSource");
    
    //change DS 
    dsProxy.setTargetDataSource(ds1);
    And this works with hibernate 3 and mysql 4.x (innodb)
    I run my transaction-unit-Test over it and this works well!
    (I don't change DS within transactions, this is not needed for me, and I haven't tried it, but I think it shouldn't work)

    Is this a preferrable way?
    Or is there anything else to attend?

    Is this "threadsave", e.g. multiple users can use different DataSources
    (webapp)?
    Last edited by robyn; May 14th, 2006, 08:38 PM.

    Comment


    • #3
      problem with concurrency

      Running some concurrency tests I found out,
      that my solution will not work correctly, because the DS-Proxy seems to be not thread-save.
      If I have 2 http-sessions both sessions change their DS frequently to load data, sometimes I get an error, that session one reads results from the other
      DS.

      How do I have to change my setup
      (org.springframework.jdbc.datasource.TransactionAw areDataSourceProxy)
      for getting clean results?

      I read about Threadlocal-pattern, but how can I set up this correctly
      for DataSource?

      Thanks

      Comment


      • #4
        Bad links

        Hi,

        The 3 forum threads referred to by Hans are unreachable :

        Proxied Datasource (without Hibernate)
        http://forum.springframework.org/vie...ied+datasource

        Hibernate and multiple databases
        http://forum.springframework.org/vie...oxy+datasource

        "Setting Schema for Each DataSource.getConnection() Call"
        http://forum.springframework.org/viewtopic.php?t=1213

        Dealing with multiple Hibernate datasources with Spring is a topic I am currently investigating.

        If anyone has working URLs or copied mirrors, that would be nice.

        - Alvin

        Comment


        • #5
          any progress?

          any progress on the above issue?

          Comment


          • #6
            I am Assuming that you are using tomcat and you may need distributed transaction

            Hope this link solves your problem

            Integrating JOTM into Tomcat 5.5.x and configuring 2PC with Spring Category: Spring Live

            http://www.jroller.com/page/raible?e...mmit_in_tomcat

            Comment


            • #7
              To avoid the concurrency problem use a threadlocal for storing the datasource. use a filter or an interceptor that, at the beginning of the thread (the web request or whatever triggers the thread) will decide which datasource will be used and set the value of the thread local. The target source will simply return the value of the thread local.
              Btw, Spring already provides some nice TargetSource implementations out of the box under http://static.springframework.org/sp...e-summary.html

              Comment

              Working...
              X