@@ -429,12 +429,37 @@ private void setupDatabaseForServer(Server server) throws MojoExecutionException
429
429
430
430
if (server .isMySqlDb () || server .isPostgreSqlDb ()) {
431
431
String uri = getUriWithoutDb (server );
432
- try (DBConnector connector = new DBConnector (uri , server .getDbUser (), server .getDbPassword (), server .getDbName ())) {
433
- connector .checkAndCreate (server );
434
- wizard .showMessage ("Connected to the database." );
435
- }
436
- catch (SQLException e ) {
437
- throw new MojoExecutionException ("Failed to connect to the specified database " + server .getDbUri (), e );
432
+ boolean connectionEstablished = false ;
433
+ int maxAttempts = 3 ;
434
+ int attempts = 0 ;
435
+
436
+ while (!connectionEstablished && attempts < maxAttempts ) {
437
+ attempts ++;
438
+ try (DBConnector connector = new DBConnector (uri , server .getDbUser (), server .getDbPassword (), server .getDbName ())) {
439
+ connector .checkAndCreate (server );
440
+ wizard .showMessage ("Connected to the database." );
441
+ connectionEstablished = true ;
442
+ }
443
+ catch (SQLException e ) {
444
+ if (e .getMessage ().contains ("Invalid database credentials" )) {
445
+ if (attempts == maxAttempts ) {
446
+ throw new MojoExecutionException (
447
+ String .format ("Failed to connect to database after %d attempts. Please verify your credentials and try again." , maxAttempts ),
448
+ e
449
+ );
450
+ }
451
+
452
+ wizard .showMessage (String .format ("Database connection failed (attempt %d of %d): %s" , attempts , maxAttempts , e .getMessage ()));
453
+ String newUser = wizard .promptForValueIfMissingWithDefault ("Please specify correct database username (-D%s)" , dbUser , "dbUser" , "root" );
454
+ String newPassword = wizard .promptForPasswordIfMissingWithDefault ("Please specify correct database password (-D%s)" , dbPassword , "dbPassword" , "" );
455
+
456
+ server .setDbUser (newUser );
457
+ server .setDbPassword (newPassword );
458
+
459
+ continue ;
460
+ }
461
+ throw new MojoExecutionException ("Failed to connect to the specified database " + server .getDbUri (), e );
462
+ }
438
463
}
439
464
440
465
if (hasDbTables (server )) {
0 commit comments