8 Replies Latest reply on Aug 16, 2007 3:18 PM by calidoggg

    Dynamically add persistence units

    calidoggg

      Wow,

      I was on my co-worker's computer and he had a japanese-translated keyboard. That was why it shows up as question marks.

      Here is my question again (sorry!!) :

      Is there a way to dynamically add or remove persistence units from the persistence.xml file? My application is built on top of a system that lets end-users add or remove databases dynamically so there can theoretically be an infinite amount of datasources and persistence units. I have not found any kind of feasible way of doing this using the persistence layer. Please advise.

      -Confused and Bewildered-

        • 1. Re: Dynamically add persistence units
          waynebaylor

          when you say dynamically add, do you mean that the database drivers, etc. already exist on the end-user's system or are those dynamically added also?

          • 2. Re: Dynamically add persistence units
            calidoggg

             

            "waynebaylor" wrote:
            when you say dynamically add, do you mean that the database drivers, etc. already exist on the end-user's system or are those dynamically added also?


            Hi Wayne,

            The database drivers are dynamically added also by the end-user. Thanks.



            • 3. Re: Dynamically add persistence units
              waynebaylor

              I imagine you could auto-generate the xml datasource based on the provided info and deploy it. But you might have to restart JBoss so that it could load the JDBC driver jar(which you would noramally put in .../server/default/lib). There is an off chance you could hot deploy the driver jar in the deploy dir though, i'm not sure.

              • 4. Re: Dynamically add persistence units
                calidoggg

                 

                "waynebaylor" wrote:
                I imagine you could auto-generate the xml datasource based on the provided info and deploy it. But you might have to restart JBoss so that it could load the JDBC driver jar(which you would noramally put in .../server/default/lib). There is an off chance you could hot deploy the driver jar in the deploy dir though, i'm not sure.



                But is there a way to auto-generate our persistence.xml file based on dynamic addition of datasources? I agree with you that there is probably a way to auto-generate the datasource on JBoss's side, but what about on the persistence unit side? Thanks.

                • 5. Re: Dynamically add persistence units
                  waynebaylor

                  if you wanted to modify a persistence.xml which has already been deployed you would have to undeploy it, unpack the ear/jar, modify the file, re-jar it, and deploy it.

                  if, however, you just want to point the datasource name in the persistence.xml to another datasource you could try undeploying the ds which currently has that name a deploying the "new" ds which has the same name, but references a different DB.

                  • 6. Re: Dynamically add persistence units
                    calidoggg

                    Yeah I thought of doing the changing the DB inside of one datasource, but then inside of my application itself, the user has the ability to choose which DB they want to use from a current list of DBs, and so that doesn't work because we just changed the datasource to be pointing to the newly-created database.

                    • 7. Re: Dynamically add persistence units
                      waynebaylor

                      do you only support a given selection of DBs? if so, you could try specifying multiple datasources in your persistence.xml.

                      • 8. Re: Dynamically add persistence units
                        calidoggg

                        That's the thing. The selection of DBs can always change, based on the dynamic nature of the user creating a new one (which is in a separate application all together), so I would have to keep updating the persistence.xml with new units everytime I create a new datasource.