Run initialization code in EJB3.1

I am currently migrating to EJB3.1 after using Spring for many years. One thing I would like to implement in EJB for which I could not find a suitable template is my MigrationManager. In Spring, I had a bean that dealt with database schema and data migration. For this, I applied Spring BeanFactoryPostProcessor, because in this way I connected the database connection, but the JPA system has not yet been initialized. Therefore, I could complete all the migration steps and then run the application.

How can I do something like this in EJB3.1 (Using CDI ... if that matters)

Chris

+4
source share
1 answer

This is the way to run the initialization code from EJB:

@Singleton @Startup public class MigrationManager { @PostConstruct public void migrate() { // do work } } 

For this you do not need a separate application (as indicated in the comment above).

An EntityManager is created by tape, because until you enter the EntityManager in some other startup code, this should enable you to update the database schema before the EntityManager actually enters the database.

By the way, for migrating a database schema, I would recommend Liquibase , which can be called by ServletContextListener .

0
source

All Articles