From 96a09fb3a399c9c71f982c2f1878e159b8de8fca Mon Sep 17 00:00:00 2001 From: harsh Date: Tue, 6 Jan 2026 18:38:51 +0530 Subject: [PATCH 1/2] Fix SDK-1: Add retry logic and fix IPv4 binding for MySQL on Linux --- .../maven/plugins/AbstractDockerMojo.java | 2 +- .../openmrs/maven/plugins/CreateMySql.java | 2 +- .../maven/plugins/utility/DBConnector.java | 39 +++++++++++++------ 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/docker-maven-plugin/src/main/java/org/openmrs/maven/plugins/AbstractDockerMojo.java b/docker-maven-plugin/src/main/java/org/openmrs/maven/plugins/AbstractDockerMojo.java index 7f992a21..2a6733be 100644 --- a/docker-maven-plugin/src/main/java/org/openmrs/maven/plugins/AbstractDockerMojo.java +++ b/docker-maven-plugin/src/main/java/org/openmrs/maven/plugins/AbstractDockerMojo.java @@ -26,7 +26,7 @@ abstract class AbstractDockerMojo extends AbstractMojo { protected static final String DEFAULT_MYSQL_EXPOSED_PORT = "3308"; protected static final String DEFAULT_MYSQL_PASSWORD = "Admin123"; protected static final String MYSQL_8_4_1 = "mysql:8.4.1"; - protected static final String DEFAULT_MYSQL_DB_URI = "jdbc:mysql://localhost:" + DEFAULT_MYSQL_EXPOSED_PORT + "/"; + protected static final String DEFAULT_MYSQL_DB_URI = "jdbc:mysql://127.0.0.1:" + DEFAULT_MYSQL_EXPOSED_PORT + "/"; private static final Logger logger = LoggerFactory.getLogger(AbstractDockerMojo.class); private static final String NOT_LINUX_UNABLE_TO_CONNECT_MESSAGE = "\n\n\nCould not connect to Docker at " + diff --git a/docker-maven-plugin/src/main/java/org/openmrs/maven/plugins/CreateMySql.java b/docker-maven-plugin/src/main/java/org/openmrs/maven/plugins/CreateMySql.java index d4fb8d06..9bb922e2 100644 --- a/docker-maven-plugin/src/main/java/org/openmrs/maven/plugins/CreateMySql.java +++ b/docker-maven-plugin/src/main/java/org/openmrs/maven/plugins/CreateMySql.java @@ -69,7 +69,7 @@ private boolean noMySqlImage(DockerClient docker) { private void createMysqlContainer(DockerClient docker) { if (container == null) container = DEFAULT_MYSQL_CONTAINER; - PortBinding portBinding = new PortBinding(new Ports.Binding("localhost", port), ExposedPort.tcp(3306)); + PortBinding portBinding = new PortBinding(new Ports.Binding("127.0.0.1", port), ExposedPort.tcp(3306)); Map labels = new HashMap<>(); labels.put(container, "true"); diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java index 274ad1c9..8bf7f5ba 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java @@ -7,31 +7,46 @@ import org.openmrs.maven.plugins.model.Server; +private static final int MAX_RETRIES = 15; public class DBConnector implements AutoCloseable { Connection connection; String dbName; + + public DBConnector(String url, String user, String pass, String dbName) throws SQLException { DriverManager.setLoginTimeout(60); - /* - * Connection attempts to a database in a newly created Docker container might fail on the first try due to the container not being fully ready - * This is to mitigate such errors. - */ - try { - this.connection = DriverManager.getConnection(url, user, pass); - } catch (SQLException e) { + this.dbName = dbName; + + // LOGIC: Retry up to 15 times (15 seconds) + + for (int i = 0; i < MAX_RETRIES; i++) { try { + // Try to connect this.connection = DriverManager.getConnection(url, user, pass); - } catch (SQLException e2) { - if (e2.getMessage().contains("Access denied")) { - throw new SQLException("Invalid database credentials. Please check your username and password.", e2); + + break; + + } catch (SQLException e) { + // If this was the last attempt, give up and crash + if (i == maxRetries - 1) { + if (e.getMessage().contains("Access denied")) { + throw new SQLException("Invalid database credentials. Please check your username and password.", e); + } + throw e; // Throw the actual error + } + + // If not the last attempt, Wait 1 second and loop again + try { + Thread.sleep(1000); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + throw new SQLException("Thread interrupted while waiting for database connection", ie); } - throw e2; } } - this.dbName = dbName; } /** From 7b3b09d99bb441d2ae1bef2b7aa5bf96c32734c7 Mon Sep 17 00:00:00 2001 From: harsh Date: Tue, 6 Jan 2026 19:48:41 +0530 Subject: [PATCH 2/2] Fix syntax error in DBConnector.java --- .../maven/plugins/utility/DBConnector.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java index 8bf7f5ba..151e283a 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java @@ -7,38 +7,30 @@ import org.openmrs.maven.plugins.model.Server; -private static final int MAX_RETRIES = 15; public class DBConnector implements AutoCloseable { Connection connection; String dbName; - - public DBConnector(String url, String user, String pass, String dbName) throws SQLException { DriverManager.setLoginTimeout(60); this.dbName = dbName; - // LOGIC: Retry up to 15 times (15 seconds) - - for (int i = 0; i < MAX_RETRIES; i++) { + // NEW LOGIC: Retry up to 15 times (15 seconds) + int maxRetries = 15; + for (int i = 0; i < maxRetries; i++) { try { - // Try to connect this.connection = DriverManager.getConnection(url, user, pass); - break; - } catch (SQLException e) { // If this was the last attempt, give up and crash if (i == maxRetries - 1) { if (e.getMessage().contains("Access denied")) { throw new SQLException("Invalid database credentials. Please check your username and password.", e); } - throw e; // Throw the actual error + throw e; } - - // If not the last attempt, Wait 1 second and loop again try { Thread.sleep(1000); } catch (InterruptedException ie) {