Announcement Announcement Module
Collapse
No announcement yet.
using a dynamic datasource in a roo generated project. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • using a dynamic datasource in a roo generated project.

    We created a project using roo and have been successfully operating the project with two static data sources for the past year. Our roo project focuses almost exclusively on the persistence layer - we do not use the roo supplied controllers or other components. For our persistence layer, we are using mysql and hibernate.

    Using the entities that were generated and annotated by roo, what approach should we use so that we dynamically determine the datasource for one of our entities?

    What I mean is (this is a contrived example):

    1. We have an entity that we generated using roo called employee.
    2. We have 26 mysql instances of exactly the same schema (same tables, same username, etc), where the first database is called "A" and the 26th database is called "Z".
    3. When I create an instance of employee, if the employee's first name starts with the letter "A", then I want that entity to be persisted to the "A" database. If the employee's first name started with "B", then I would have wanted to store the entity within the "B" database.


    Side note: It is ok to pre-configure all of the datasources within the application context (would like to avoid it if possible), but I must be able to establish the datasource for the entity at run time.

    Thanks in advance.

    Regards,

    SCott

    PS We are using ROO 1.1.2 and STS 2.6.1
    Last edited by scottccote; Aug 30th, 2011, 11:24 AM. Reason: adding env description

  • #2
    Hi Scott,

    Your implementation is using a lot of resources... Probably using only one db and a field to discriminate the way you need would do it. Hard to say for me from here.
    Anyway -back to your question- you can review my article at http://viralpatel.net/blogs/2011/01/...iguration.html. It is not exactly answering your question but I think it might shed some ideas how to solve it.

    Another quick ideas

    1) You can create a JPA entity-manager-manager that selects an em on the fly.

    2) Create specific controllers grouped by letter. In other words controllers for letter A, controllers for letter B, etc. I kind of code intensive but basic to implement.

    I hope it helps you
    jD

    Comment


    • #3
      Thank your for your response. We want to be able to select the datasource for our JPA actiivity based on more complex criteria. Here is a code snippet to illustrate what I am looking for. Keep in mind that the class called JPAFriendlyDatabaseLayer is completely contrived and the setting of it explicitly in the finder is also contrived to illustrate the runtime quality and explicit control ....


      Code:
      @RequestMapping("/resources")
      @Controller
      public class ResourceController extends OurController implements OurConstants
      {
      
              ServletContext sc;
      
      
              @ResponseStatus(value = HttpStatus.CREATED)
              @RequestMapping(value = "", method = RequestMethod.POST)
              public String update(@RequestBody final ResourceContainer resourceContainer, final ModelMap modelMap,
                              final HttpServletRequest request, final HttpServletResponse response)
              {
                      contextData.putTransactionIdentifier(aTransactionIdentifier);
                      JPAFriendlyDatabaseLayer aJPAFriendlyDatabaseLayer = findJPAFriendlyDatabaseLayer(resourceContainer);
                      String result = ResourceFacade.update(resourceContainer, modelMap, aJPAFriendlyDatabaseLayer);
      
                      return result;
              }
      
              private JPAFriendlyDatabaseLayer afindJPAFriendlyDatabaseLayer(ResourceContainer resourceContainer)
              {
                      // inspect the guts of the resourceContainer and create or lookup from a cache an instance of JPAFriendlyDatabaseLayer
                      // lookup could be based on some complicated routine, but lets suppose that we have a value and lookup a match
                      // using hash algorithm or a table lookup ....
                      return null;   // obviously - this should return an instance - not null - this is an example
              }
      }
      
      
      public class ResourceFacade
      {
              public String update(ResourceContainer resourceContainer,ModelMap modelMap,JPAFriendlyDatabaseLayer aJPAFriendlyDatabaseLayer)
              {
                      // find the resource using the correct database
                      ResourceThing aResourceThing = ResourceThing.findResourceThing(resourceContainer.getId(), aJPAFriendlyDatabaseLayer);
                      aResourceThing.setFoo("bar");
                      // now save the updated resource to the correct database
                      aResourceThing.merge();
                      return "resourceThing/updated";   // obviously - this should return an instance - not null - this is an example
              }
      }
      Hopefully, I will be able to provide some assistance to you later for your help. If you lived near, I would take you out for a Beer.



      SCott

      Comment

      Working...
      X