Search code examples
hibernateosgiliquibaseblueprint-osgi

Using Liquibase with OSGI and Hibernate


I would like to integrate a Liquibase to my project. My first idea was to use a blueprint's bean that starts the Liquibase update from an onInit method. But there was a problem with a hibernate, because we have "hbm2ddl.auto" set to "validate" and validation was executed before my bean's onInit. (we use container managed persistence with persistence.xml in META-INF). So my second attemp was to use BundleTracker and when some entry in manifest exists, the liquibase performs db update. It is working well but I would like to enforce that bundle won't start if update of database won't be successful. But I don't have any idea how to do it from BundleTracker method addBundle. Is there any way how to prevent starting bundle from BundleTracker event?

I have another possible idea for doing it to create some addition bundle that performs update and other bundle with persistence.xml will depend on this bundle. But we have a lot of bundles with persistence.xml that`s why the solution with BundleTracker seems better to me.


Solution

  • In almost all cases these kind of dependencies should be modeled as services. Starting/stopping bundles may sound easy but will get you in a horrendously suckable morass over time.

    Once a dynamic dependency is a services, all parts in OSGi (especially Declarative Services) make it trivial to work with its dynamics. Not just in the 'happy' cases but also in the many ways these things can cause errors.

    So in your case you create a LiquibaseHappy service or something and the bundles that use the db should depend on it.