Skip to content

[WFLY-22003] Replace JPA initialization with JDBC CDI bean - #1197

Open
Riovo wants to merge 1 commit into
wildfly:mainfrom
Riovo:WFLY-22003
Open

[WFLY-22003] Replace JPA initialization with JDBC CDI bean#1197
Riovo wants to merge 1 commit into
wildfly:mainfrom
Riovo:WFLY-22003

Conversation

@Riovo

@Riovo Riovo commented Aug 11, 2026

Copy link
Copy Markdown

Fixes: https://redhat.atlassian.net/browse/WFLY-22003

Replaces the current JPA-based database initialization with a CDI bean using direct JDBC. This eliminates the WildFly Glow unbound datasource warning that popped up because persistence.xml referenced a CLI-defined datasource.

The CDI bean observes application startup (@observes @initialized(ApplicationScoped.class)) to initialize the H2 database schema and test users for the Elytron JDBC realm.

@Riovo
Riovo requested a review from emmartins as a code owner August 11, 2026 23:53
@Riovo
Riovo marked this pull request as draft August 12, 2026 00:26
@Riovo
Riovo marked this pull request as ready for review August 12, 2026 11:00
@Riovo
Riovo requested a review from luck3y August 13, 2026 14:04

@luck3y luck3y left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I added some suggestions, feel free to disagree :)

@emmartins

Copy link
Copy Markdown
Contributor

Personally I dislike the usage of an EJB for this, I know this was suggested in the original JIRA but EJB is kind of legacy and considered an expensive resource, I suggest instead either:

  1. In case DB initialization separated from the Servlet is prefered, to turn DatabaseInitializer into a CDI bean that observes app initialization:
@ApplicationScoped
public class DatabaseInitializer {
    ...
    public void initializeDatabase(@Observes @Initialized(ApplicationScoped.class) Object init) {
       ...
    }
    ...
}
  1. Use nothing else than the existing Servlet, overriding its init() method, where you will invoke DatabaseInitializer, and changing the WebServlet annotation to load-init on startup:
@WebServlet(value = "/SecuredServlet", loadOnStartup = 1)
@ServletSecurity(@HttpConstraint(rolesAllowed = { "quickstarts" }))
public class SecuredServlet extends HttpServlet {
    ...
    @Resource(lookup = "java:jboss/datasources/ServletSecurityDS")
    private DataSource dataSource;

    @Override
    public void init() throws ServletException {
        new DatabaseInitializer(dataSource).initializeDatabase();
    }
    ...
}

Either requires no changes to the CLI script, or aditional dependencies.

PS: Ken remarks would still need to be considered.

@Riovo Riovo changed the title [WFLY-22003] Replace JPA initialization with JDBC startup bean [WFLY-22003] Replace JPA initialization with JDBC CDI bean Aug 17, 2026
@Riovo
Riovo marked this pull request as draft August 17, 2026 09:24
@Riovo
Riovo marked this pull request as ready for review August 17, 2026 10:28
@Riovo

Riovo commented Aug 17, 2026

Copy link
Copy Markdown
Author

Thank you @emmartins and @luck3y for your comments. I have since implemented everything, please let me know if everything is good or if changes need to be done.

@Riovo
Riovo requested review from emmartins and luck3y August 17, 2026 10:28
Comment thread servlet-security/pom.xml Outdated
@emmartins

Copy link
Copy Markdown
Contributor

@Riovo please squash all commits into one too :)

@Riovo

Riovo commented Aug 17, 2026

Copy link
Copy Markdown
Author

@Riovo please squash all commits into one too :)

Hehe will do, thank you for you time :)

@Riovo

Riovo commented Aug 19, 2026

Copy link
Copy Markdown
Author

@darranl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants