简体   繁体   中英

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.

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.

You might be able to use the newest pax-jdbc-config for this task. It provides a DataSource service from a configuration. So this in it self is already a useful abstraction.

The newest version now contains a feature called PreHook . Basically you implement an OSGi service that implements the PreHook interface and publish it with a name property. In your DataSource config you can then refer to that service by its name. When the DataSource is created this service is called before the DataSource is published for use by other bundles. So inside the PreHook you can work on the databse using liquibase and the hibernate based bundle will only see the DataSource when the database is in the new state.

For an example see also the integration test for the PreHook .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM