简体   繁体   English

在OSGI和Hibernate中使用Liquibase

[英]Using Liquibase with OSGI and Hibernate

I would like to integrate a Liquibase to my project. 我想将一个Liquibase集成到我的项目中。 My first idea was to use a blueprint's bean that starts the Liquibase update from an onInit method. 我的第一个想法是使用一个蓝图bean,它通过onInit方法启动Liquibase更新。 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. 但是休眠状态存在问题,因为我们将“ hbm2ddl.auto”设置为“ validate”,并且在我的bean的onInit之前执行了验证。 (we use container managed persistence with persistence.xml in META-INF). (我们在META-INF中将容器管理的持久性与persistence.xml一起使用)。 So my second attemp was to use BundleTracker and when some entry in manifest exists, the liquibase performs db update. 因此,我的第二个尝试是使用BundleTracker,并且在清单中存在某些条目时,liquibase会执行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. 但是我不知道如何从BundleTracker方法addBundle中做到这一点。 Is there any way how to prevent starting bundle from BundleTracker event? 有什么方法可以防止从BundleTracker事件启动捆绑包?

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. 我有另一个可能的想法,可以执行此操作来创建执行更新的附加捆绑包,而其他具有persistence.xml的捆绑包将依赖于此捆绑包。 But we have a lot of bundles with persistence.xml that`s why the solution with BundleTracker seems better to me. 但是我们有很多与persistence.xml捆绑在一起的东西,这就是为什么BundleTracker解决方案对我来说似乎更好的原因。

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. 一旦动态依赖项成为服务,OSGi中的所有部分(尤其是声明性服务)就使其在动态处理方面变得微不足道。 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. 因此,在您的情况下,您将创建一个LiquibaseHappy服务或其他东西,并且使用db的捆绑软件应依赖于它。

You might be able to use the newest pax-jdbc-config for this task. 您也许可以使用最新的pax-jdbc-config来完成此任务。 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 . 现在,最新版本包含一个称为PreHook的功能。 Basically you implement an OSGi service that implements the PreHook interface and publish it with a name property. 基本上,您实现的OSGi服务实现了PreHook接口 ,并使用name属性发布它。 In your DataSource config you can then refer to that service by its name. 然后,在您的DataSource配置中,您可以通过名称来引用该服务。 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. 因此,在PreHook中,您可以使用liquibase处理数据库,并且基于hibernate的捆绑软件仅在数据库处于新状态时才能看到DataSource。

For an example see also the integration test for the PreHook . 有关示例,请参见PreHook集成测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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