Announcement Announcement Module
Collapse
No announcement yet.
DAOs with dynamic data source Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • DAOs with dynamic data source

    I'm currently using Spring/Hibernate with an ApplicationContext. My DAOs use inversion of control (HibernateTemplate) and they are all wrapped by proxies provided by TransactionProxyFactoryBean. Everything works great.

    However, now I'm in a situation where I will need to have DAOs that can't know the target data source until well after the application starts. It can vary at runtime, and the DAOs can't be singletons because I may need to use two different data sources at the same time. There is no need to share transactions, though, as each DAO instance is independent of the others.

    Is there a pattern for this situation that still allows me to take advantage of the ApplicationContext features of Spring? I assume I need separate instances of the transaction manager, Hibernate session factory, etc. per data source ...or do I?

    Thanks.

  • #2
    the target data source ... can vary at runtime
    Can you expand on this.

    DAOs that can't know the target data source until well after the application starts
    Why not use JNDI. Maybe expand on well after - it's difficult to picture why you're doing this - maybe there's another way than changing data sources at runtime.

    Comment


    • #3
      Is there a pattern for this situation that still allows me to take advantage of the ApplicationContext features of Spring? I assume I need separate instances of the transaction manager, Hibernate session factory, etc. per data source ...or do I?
      No - you can still work with one transaction manager/session factory. All you need to do is swap the data source they use. To do this you probably want to extend DelegatingDataSource to swap data sources based on whatever criteria it is that you use to determine the data source for your DAOs. UserCredentialsDataSourceAdapter should give you some idea of where to start.

      Ollie

      Comment


      • #4
        All you need to do is swap the data source they use.
        We assume here that the databases you use are of the same type (oracle, mysql, postgresql...) as Hibernate does not swaps dynamically the dialect it uses.

        Comment


        • #5
          Originally posted by irbouho
          We assume here that the databases you use are of the same type (oracle, mysql, postgresql...) as Hibernate does not swaps dynamically the dialect it uses.
          Unfortunately, I cannot make that assumption.

          What I'm trying to do is create a group of DAOs "on demand" that can be pointed to a unique data source. I may need to instantiate several such groups during the course of the JVM process and use them simultaneously to access different databases.

          I think I have a solution, though. My idea is to instantiate a unique BeanFactory for each unique data source and DBMS type combination. I'll configure the data source through a PropertyPlaceholderConfigurer. The DAOs won't be true singletons, but that's fine.

          Comment


          • #6
            did you consider using org.springframework.aop.target.HotSwappableTargetS ource?

            Comment


            • #7
              Interesting. I hadn't seen that. But I don't think it'll help me. My goal isn't to swap the DataSource/SessionFactory of a DAO instance after it has been created. My goal is to have several independent instances of the same DAO class that each have their own DataSource/SessionFactory.

              I don't think there is any way around creating separate BeanFactories (or ApplicationContexts). Not a big deal.

              Comment


              • #8
                Have you figured out a solution to your problem yet? I am running across the same issue too

                Comment


                • #9
                  Originally posted by jivesociety
                  Have you figured out a solution to your problem yet? I am running across the same issue too
                  Yes. Basically, I introduced a new kind of DAO into my system, which I've been calling a "dynamic data source" DAO. These are DAOs that can be pointed to different data sources "on demand". There is one instance of each dynamic DAO class per unique data source.

                  To make this work, I created a factory called a DynamicDaoBeanFactoryProvider. It creates and initializes a new Spring BeanFactory for each unique set of data source connection properties that a caller passes into it. (A factory of factories, so to speak.)

                  Each BeanFactory that it generates creates an object graph of DAOs bound to a particular data source using the caller-specified connection properties. I use a PropertyPlaceholderConfigurer to "pass in" the connection properties when the BeanFactory is created.

                  Theoreically, the provider could generate a new BeanFactory each time a group of dynamic DAOs is requested, but it's much more efficient to cache them. If the caller specifies the same set of connection properties (driver, URL, username, password, and a DBMS platform enum that I've defined), they get back the same BeanFactory.

                  In my "main" Spring application context, I've defined a business service whose sole purpose is to retrieve dynamic DAOs from the DynamicDaoBeanFactoryProvider. I call it a DAO broker.

                  Hope this helps.

                  Comment


                  • #10
                    Can you post some code.

                    Thanks

                    Comment

                    Working...
                    X