Repository navigation
Conversation
luck3y
left a comment
There was a problem hiding this comment.
LGTM, I added some suggestions, feel free to disagree :)
|
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:
Either requires no changes to the CLI script, or aditional dependencies. PS: Ken remarks would still need to be considered. |
|
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 please squash all commits into one too :) |
Hehe will do, thank you for you time :) |
|
LGTM overall, one non-blocking nit: Low risk given the small dataset, but |
@kstekovi Since the datasource uses default autocommit and the CDI Bean is not transactional, I think it will autocommit when every connection instance is closed, and if that is the case all needed to enforce your enhancement would be to create the connection at the event handler and, pass it along the 2 methods in it. @Riovo can you please investigate and optimize this code if needed? |
|
Firstly sorry for the long wait, I was on rotation working on Lightwell. Thanks @emmartins and @kstekovi for the feedback. I've updated the code:
|
emmartins
left a comment
There was a problem hiding this comment.
I think you should start by dropping the tables if those exist, this would allow you to not have to worry if user exists, which as it stands now does not guarantee test data is correct, and would also allow you to wrap all updates in a single commit... Currently table creation is is done with autocommit. My suggestion would be something like this:
public void initializeDatabase(@Observes @Initialized(ApplicationScoped.class) Object init) {
LOGGER.info("Initializing database schema for servlet-security...");
try (Connection conn = dataSource.getConnection()) {
conn.setAutoCommit(false);
createTables(conn);
insertTestData(conn);
conn.commit();
LOGGER.info("Database initialization completed successfully.");
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, "Failed to initialize database", e);
conn.rollback();
throw new RuntimeException("Database initialization failed", e);
}
}
private void createTables(Connection conn) throws SQLException {
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate("DROP TABLE IF EXISTS USERS");
stmt.executeUpdate(
"CREATE TABLE USERS (" +
"ID INT, " +
"USERNAME VARCHAR(20), " +
"PASSWORD VARCHAR(20))"
);
stmt.executeUpdate("DROP TABLE IF EXISTS ROLES");
stmt.executeUpdate(
"CREATE TABLE ROLES (" +
"ID INT, " +
"NAME VARCHAR(20))"
);
stmt.executeUpdate("DROP TABLE IF EXISTS USERS_ROLES");
stmt.executeUpdate(
"CREATE TABLE USERS_ROLES (" +
"USER_ID INT, " +
"ROLE_ID INT)"
);
LOGGER.info("Database tables created");
}
}
private void insertTestData(Connection conn) throws SQLException {
try (PreparedStatement insertStmt = conn.prepareStatement(
"INSERT INTO USERS (ID, USERNAME, PASSWORD) VALUES (?, ?, ?)")) {
insertStmt.setInt(1, 1);
insertStmt.setString(2, "quickstartUser");
insertStmt.setString(3, "quickstartPwd1!");
insertStmt.executeUpdate();
insertStmt.setInt(1, 2);
insertStmt.setString(2, "guest");
insertStmt.setString(3, "guestPwd1!");
insertStmt.executeUpdate();
}
try (PreparedStatement insertStmt = conn.prepareStatement(
"INSERT INTO ROLES (ID, NAME) VALUES (?, ?)")) {
insertStmt.setInt(1, 1);
insertStmt.setString(2, "quickstarts");
insertStmt.executeUpdate();
insertStmt.setInt(1, 2);
insertStmt.setString(2, "guest");
insertStmt.executeUpdate();
}
try (PreparedStatement insertStmt = conn.prepareStatement(
"INSERT INTO USERS_ROLES (USER_ID, ROLE_ID) VALUES (?, ?)")) {
insertStmt.setInt(1, 1);
insertStmt.setInt(2, 1);
insertStmt.executeUpdate();
insertStmt.setInt(1, 2);
insertStmt.setInt(2, 2);
insertStmt.executeUpdate();
}
LOGGER.info("Test data inserted successfully");
}
Drop and recreate tables on startup instead of using CREATE IF NOT EXISTS, ensuring clean test data. All schema creation and data insertion run in a single transaction for atomicity.
|
@kstekovi IMHO this is now good to merge, can you please re-review? |
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.