CDI Extension for Flyway

眉间皱痕 提交于 2019-11-28 17:12:28

问题


I tried to run flyway in my application before hibernate is hooking in on my JBoss AS 7.1. I tried with an @javax.ejb.Startup annotation, but this gets executed AFTER Hibernate is initialized and the database scheme is checked.

So as far as I understand we can use a CDI Extension which hooks in before Hibernate is initialized. Is there some support for that out of the box for flyway? And if not, has anyone tried to do this before?


回答1:


Ok I finally found out how to do this: I had to use the Hibernate Integration API. This is the whole code I had to write:

public class FlywayIntegrator implements Integrator {

  @Override
  public void integrate(final Configuration configuration, final SessionFactoryImplementor sessionFactoryImplementor, final SessionFactoryServiceRegistry sessionFactoryServiceRegistry) {
    final Flyway flyway = new Flyway();

    flyway.setDataSource(....);
    flyway.migrate();
  }

  @Override
  public void integrate(final MetadataImplementor metadataImplementor, final SessionFactoryImplementor sessionFactoryImplementor, final SessionFactoryServiceRegistry sessionFactoryServiceRegistry) {
    //no-op
  }

  @Override
  public void disintegrate(final SessionFactoryImplementor sessionFactoryImplementor, final SessionFactoryServiceRegistry sessionFactoryServiceRegistry) {
    //no-op
  }
}

If anyone is interested in more details, I created a github project which demonstrates that: https://github.com/dobermai/Hibernate-Flyway-Integration




回答2:


CDI defines its own lifecycle which is executed when an applications starts / stops. (Shouldn't you know about it already: This is a good place to learn about the basic mechanism.)

The problem - to my best knowledge - is that the Hibernate initialization process is not directly linked to the CDI startup. This means that I'm not sure if its safe to rely on a relation between Hibernate & CDI "events". There is certainly nothing like a CDI-Event HibernateInitialized.

Having said this, I'd give it a try :) You should implement a simple extension that hooks up at BeforeBeanDiscovery, which is as early as it gets.

This online presentation gives an overview about the different CDI events and their order. It's in German, unfortunately.



来源:https://stackoverflow.com/questions/11071821/cdi-extension-for-flyway

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!