12 Replies Latest reply on Feb 4, 2011 10:34 AM by Dmitri Voronov

    Multiple repositories with JPA connector

    Dmitri Voronov Novice

      Hello all,

       

      I'm trying to use multiple repositories with JPA connectors (JpaSources) WITHIN a web application. Thus I have a repository config file with 3 repositories each of them has its own JPA-connector. All JPA connectorys are using the same datasource. Each of this repositories is using the same CND file, where I define a custom types e.g. "feed:feed".

      If I configure JPA's option autoGenerateSchema to "create", it works fine. If I switch it to "validate" I get exception:

       

      java.lang.RuntimeException: javax.jcr.RepositoryException: Table 'feed:feed' does not exist

       

      On debuggin I see that the nodes loaded by session have primary type "ns001:feed" (or similar).

       

      Is in my case the namespace not persisted?

      Could anybody help or has more experience with JPA connector and its capabilities.

       

      Thanks in advance.

        • 1. Multiple repositories with JPA connector
          Randall Hauch Master

          It all depends how you want to control your database schema, and what you want to have happen when your application restarts.

           

          You can set it to "create", in which case it will (upon startup) create the schema and destroy previous data; "update" value will update the schema to reflect the latest version; "create-drop" is just like "create" except that it drops the tables prior to shutdown, and "validate" which leaves the schema alone.

           

          The "validate" option is for production, since this option does absolutely nothing with the database schema. You can generate the DDL for the schema using our DDL generator tool for your DBMS, and can review/approve and manually apply to your production database.

           

          The "create" option works the best if you're running tests that don't involve shutting down and restarting. As soon as ModeShape starts up, the database will be (re)created, and all data lost.

           

          The "update" option may work for tests if the database doesn't already exist (I don't recall offhand), but it will check the database schema upon subsequent startups. Since it shouldn't find any differences in the schema, it won't attempt to change anything, and should leave all existing data untouched.

           

           

          Best regards,

          Randall

          • 2. Multiple repositories with JPA connector
            Dmitri Voronov Novice

            Hello Randall,

             

            my issue is not switching of the option autoGenerateSchema from "create" to "validate".

            On the first start-up:

            • I'm using "create" to generate schema
            • create some nodes of a custom type 'feed:feed'
            • execute a query SELECT * FROM ['feed:feed'] to check the result

            On the second start-up:

            • I switch autoGenerateSchema to "validate"
            • execute the same query SELECT * FROM ['feed:feed'] but inseat of the nodes created on the first run I get an exception
              java.lang.RuntimeException: javax.jcr.RepositoryException: Table 'feed:feed' does not exist
            • as I wrote on debuggin I see that the nodes loaded by session have primary type "ns001:feed"

            As I wrote I use several repositories; each of them has it's JPA connector, but they share the CND file.

            • 3. Multiple repositories with JPA connector
              Randall Hauch Master

              What DBMS are you using? If you're using HSQLDB or H2, what URL are you using?

              • 5. Multiple repositories with JPA connector
                Dmitri Voronov Novice

                Hi all,

                 

                does anybody have any idea, if it is possible to have multiple repositories with JPA connectors in the same (web)-application? Anyway I'm facing a namespace issues on using modeshape this way.

                • 6. Multiple repositories with JPA connector
                  Randall Hauch Master

                  I'm not sure I understand.

                   

                  You have three web applications deployed to a single web/app server, and you're trying to set up a single ModeShape engine configured with 3 repositories (I presume one for each web app?), 3 sources (1 for each repository), and each of those sources uses the JPA connector and the same data source (e.g., same database connection properties)?

                   

                  Do I have this right? If not, can you help me understand what you're trying to do?

                  • 7. Multiple repositories with JPA connector
                    Randall Hauch Master

                    does anybody have any idea, if it is possible to have multiple repositories with JPA connectors in the same (web)-application?

                    Let me say this: it is absolutely possible to have multiple JCR repositories each using JPA within a single web application, and all of those JCR repositories can be set up within a single ModeShape engine instance (a single configuration).

                     

                    However, earlier you said:

                     

                    All JPA connectorys are using the same datasource.

                     

                    Do you mean that you've configured all three JPA connectors to talk to the exact same database instance? That will definitely not work. Each JCR repository needs to use a different database, because each repository owns its own content.

                     

                    If you want three web to have a single repository, then you should set up each repository with its own source and it's own database. If you want each web app to have access to the same content, can you not just have all three web apps use the same JCR repository?

                     

                    Best regards,

                     

                    Randall

                    • 8. Re: Multiple repositories with JPA connector
                      Dmitri Voronov Novice

                      Hi Randall,

                       

                      I found out the origin of my issue. I forgot to configure the custom namespaces for the repository like follows:

                      ...

                                  <mode:namespaces jcr:primaryType="mode:namespaces">

                                      <feed jcr:primaryType="mode:namespace" mode:uri="http://my.org/feed/1.0" />

                                  </mode:namespaces>

                      ...

                      Nevertheless in my opinion there is an issue. If the namespace is not defined it seems to work with in-memory db as well as with a JPA-connector in with autoGenerateSchema set to "create" (see two autoGenerateSchema scenarios); there is no exception or warning saying that namespace is unknown. The namespace is just set to "ns001". Even the JCR queries work fine. But after restart with autoGenerateSchema set to "validate" the situation is different. The same queries don't work anymore and the exception is thrown:

                      java.lang.RuntimeException: javax.jcr.RepositoryException: Table 'feed:feed' does not exist

                       

                       

                      But again to my application.

                      I have one wep application using 3 repositories, which have their own JPA-connectors. The connectors are using the same DB BUT the diffrent schemas. Thus the content is not shared by the repositories.

                       

                      Regards

                      • 9. Re: Multiple repositories with JPA connector
                        Randall Hauch Master

                        But again to my application.

                        I have one wep application using 3 repositories, which have their own JPA-connectors. The connectors are using the same DB BUT the diffrent schemas. Thus the content is not shared by the repositories.

                         

                        Okay, that should work fine - there won't be any content shared.

                         

                        Nevertheless in my opinion there is an issue. If the namespace is not defined it seems to work with in-memory db as well as with a JPA-connector in with autoGenerateSchema set to "create" (see two autoGenerateSchema scenarios); there is no exception or warning saying that namespace is unknown. The namespace is just set to "ns001". Even the JCR queries work fine. But after restart with autoGenerateSchema set to "validate" the situation is different. The same queries don't work anymore and the exception is thrown:

                        java.lang.RuntimeException: javax.jcr.RepositoryException: Table 'feed:feed' does not exist

                         

                        Seems odd why the namespace is not set with your prefix. "ns001" is an auto-generated namespace, and these get created whenever needed (yes, there is no error). I'll look into this sometime this week. If you wanted to, you could log an issue in our JIRA.

                        • 10. Multiple repositories with JPA connector
                          Dmitri Voronov Novice
                          ...The connectors are using the same DB BUT the diffrent schemas. Thus the content is not shared by the repositories.

                          Actually it is not possible to define a DB schema name on the JPA connector. Therefore I had to extend the JPASource with an additional instance variable and configure( Ejb3Configuration configuration ) method to set another hibernate property "hibernate.default_schema".

                           

                          I think a defining of DB schema is a useful feature and it would be great to have if in JPASource in the future.

                          • 11. Multiple repositories with JPA connector
                            Randall Hauch Master

                            Dmitri, I agree that this sounds like a good idea. Would you mind logging a feature request in JIRA for adding the DB schema name as a configuration property to the JPA connector's RepositorySource implementation?

                            • 12. Multiple repositories with JPA connector
                              Dmitri Voronov Novice

                              Sure. Just wanted to know your opinion first.