5 Replies Latest reply on Apr 26, 2010 4:08 PM by bobthule

    How to run code before JPA startup?

    bobthule

      I want a chance to run code before JPA starts up to have a chance to run SQL to modify the database.  The only way I can think of to try and accomplish this is to create a servlet with a low load-on-startup value.  Is that the best or only option?

        • 1. Re: How to run code before JPA startup?
          nickarls

          Isn't the boot order pretty much appserver-specific? What is it in JBoss? CDI/JSF/JPA?

          • 2. Re: How to run code before JPA startup?
            mwohlf

            take a look at liquibase if you want to do database upgrades/checks on startup

            • 3. Re: How to run code before JPA startup?
              bobthule

              Good point about the appserver-specific.  It will be CDI/JSF/JPA all deployed in a single war file.  It will be deployed on JBoss but in the future for other clients it may need to be deployed on Glassfish or Tomcat.


              I looked at liquibase and they use a ServletListener to do the updates at startup, but I am unclear whether that doing it that way is guaranteed to run before JPA starts up.


              It seems like JPA and/or Hibernate would have a hook to run code before it starts up but I couldn't find anything on that.  I also looked to see if Weld had a way, but I couldn't find it, either.


              I would most prefer a solution with Weld.



              • 4. Re: How to run code before JPA startup?
                pmuir

                If you are on JBoss AS and just want to run SQL, Hibernate will execute the import.sql AFTER JPA gets set up.


                You can't guarantee any ordering that I know of, and I don't know of a hook to do this.

                • 5. Re: How to run code before JPA startup?
                  bobthule

                  Thanks Pete,


                  Yeah I know about import.sql, but the goal is more to help ease customer installations by allowing database schema migrations to automatically run when the website starts up. 


                  I had an idea of creating my own PersistenceProvider and overriding the methods to do my upgrades and then passing off to the Hibernate PersistenceProvider.