Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> labels = new HashMap<>();
labels.put(container, "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,30 @@ public class DBConnector implements AutoCloseable {

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;

// NEW LOGIC: Retry up to 15 times (15 seconds)
int maxRetries = 15;
for (int i = 0; i < maxRetries; i++) {
try {
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;
}
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;
}

/**
Expand Down