This is an automated email from the ASF dual-hosted git repository.

rmaucher pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/10.1.x by this push:
     new b8e834dcf5 Refactor using Mark's template
b8e834dcf5 is described below

commit b8e834dcf50449d5ef42881ffac7efca7eb427d5
Author: remm <[email protected]>
AuthorDate: Fri Jul 10 22:34:22 2026 +0200

    Refactor using Mark's template
---
 .../catalina/users/DataSourceUserDatabase.java     | 123 ++++++---------------
 1 file changed, 34 insertions(+), 89 deletions(-)

diff --git a/java/org/apache/catalina/users/DataSourceUserDatabase.java 
b/java/org/apache/catalina/users/DataSourceUserDatabase.java
index d90eee2a17..1b2960eadb 100644
--- a/java/org/apache/catalina/users/DataSourceUserDatabase.java
+++ b/java/org/apache/catalina/users/DataSourceUserDatabase.java
@@ -34,6 +34,7 @@ import org.apache.catalina.Role;
 import org.apache.catalina.User;
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
+import org.apache.tomcat.util.ExceptionUtils;
 import org.apache.tomcat.util.res.StringManager;
 
 /**
@@ -1241,45 +1242,43 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
 
         dbWriteLock.lock();
         try {
+            boolean success = false;
             try {
                 dbConnection.setAutoCommit(false);
-                Exception exception = saveInternal(dbConnection);
+                saveInternal(dbConnection);
                 // Commit all changes to the database
-                if (exception.getSuppressed().length == 0) {
-                    try {
-                        dbConnection.commit();
-                    } catch (SQLException e) {
-                        exception.addSuppressed(e);
-                    }
-                }
-                if (exception.getSuppressed().length > 0) {
-                    // Some exception occurred so rollback everything
-                    try {
-                        dbConnection.rollback();
-                    } catch (SQLException e) {
-                        exception.addSuppressed(e);
-                    }
-                    
log.error(sm.getString("dataSourceUserDatabase.exception"), exception);
-                    throw exception;
-                }
-                // Everything is committed successfully, we can now clear all 
un-persisted changes
-                if (isUserRoleStoreDefined()) {
-                    removedRoles.clear();
-                    if (isRoleStoreDefined()) {
-                        createdRoles.clear();
-                        modifiedRoles.clear();
-                    }
-                }
-                if (isGroupStoreDefined()) {
-                    removedGroups.clear();
-                    createdGroups.clear();
-                    modifiedGroups.clear();
+                dbConnection.commit();
+                success = true;
+            } catch (Throwable t) {
+                log.error(sm.getString("dataSourceUserDatabase.exception"), t);
+                ExceptionUtils.handleThrowable(t);
+                // Some exception occurred so rollback everything
+                try {
+                    dbConnection.rollback();
+                } catch (SQLException e) {
+                    t.addSuppressed(e);
                 }
-                removedUsers.clear();
-                createdUsers.clear();
-                modifiedUsers.clear();
+                throw t;
             } finally {
                 try {
+                    // Everything is committed successfully, we can now clear 
all un-persisted changes
+                    if (success) {
+                        if (isUserRoleStoreDefined()) {
+                            removedRoles.clear();
+                            if (isRoleStoreDefined()) {
+                                createdRoles.clear();
+                                modifiedRoles.clear();
+                            }
+                        }
+                        if (isGroupStoreDefined()) {
+                            removedGroups.clear();
+                            createdGroups.clear();
+                            modifiedGroups.clear();
+                        }
+                        removedUsers.clear();
+                        createdUsers.clear();
+                        modifiedUsers.clear();
+                    }
                     dbConnection.setAutoCommit(true);
                 } catch (SQLException e) {
                     // Ignore, this is for cleanup
@@ -1296,11 +1295,9 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
      * Save all pending changes to the database using the provided connection.
      *
      * @param dbConnection The database connection to use
-     * @return an Exception containing any error that occurred while saving 
the database as suppressed exception
+     * @throws SQLException when a database error occurs
      */
-    protected Exception saveInternal(Connection dbConnection) {
-
-        Exception mainException = new 
Exception(sm.getString("dataSourceUserDatabase.exception"));
+    protected void saveInternal(Connection dbConnection) throws SQLException {
         StringBuilder temp = null;
         StringBuilder tempRelation;
         StringBuilder tempRelationDelete = null;
@@ -1330,21 +1327,15 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                         try (PreparedStatement stmt = 
dbConnection.prepareStatement(tempRelationDelete.toString())) {
                             stmt.setString(1, role.getRolename());
                             stmt.executeUpdate();
-                        } catch (SQLException e) {
-                            mainException.addSuppressed(e);
                         }
                     }
                     try (PreparedStatement stmt = 
dbConnection.prepareStatement(tempRelationDelete2.toString())) {
                         stmt.setString(1, role.getRolename());
                         stmt.executeUpdate();
-                    } catch (SQLException e) {
-                        mainException.addSuppressed(e);
                     }
                     try (PreparedStatement stmt = 
dbConnection.prepareStatement(temp.toString())) {
                         stmt.setString(1, role.getRolename());
                         stmt.executeUpdate();
-                    } catch (SQLException e) {
-                        mainException.addSuppressed(e);
                     }
                 }
             }
@@ -1369,8 +1360,6 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                             stmt.setString(2, role.getDescription());
                         }
                         stmt.executeUpdate();
-                    } catch (SQLException e) {
-                        mainException.addSuppressed(e);
                     }
                 }
             }
@@ -1387,8 +1376,6 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                         stmt.setString(1, role.getDescription());
                         stmt.setString(2, role.getRolename());
                         stmt.executeUpdate();
-                    } catch (SQLException e) {
-                        mainException.addSuppressed(e);
                     }
                 }
             }
@@ -1404,8 +1391,6 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                 try (PreparedStatement stmt = 
dbConnection.prepareStatement(tempRelationDelete.toString())) {
                     stmt.setString(1, role.getRolename());
                     stmt.executeUpdate();
-                } catch (SQLException e) {
-                    mainException.addSuppressed(e);
                 }
             }
         }
@@ -1441,20 +1426,14 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                     try (PreparedStatement stmt = 
dbConnection.prepareStatement(groupRoleRelationDelete)) {
                         stmt.setString(1, group.getGroupname());
                         stmt.executeUpdate();
-                    } catch (SQLException e) {
-                        mainException.addSuppressed(e);
                     }
                     try (PreparedStatement stmt = 
dbConnection.prepareStatement(tempRelationDelete2.toString())) {
                         stmt.setString(1, group.getGroupname());
                         stmt.executeUpdate();
-                    } catch (SQLException e) {
-                        mainException.addSuppressed(e);
                     }
                     try (PreparedStatement stmt = 
dbConnection.prepareStatement(temp.toString())) {
                         stmt.setString(1, group.getGroupname());
                         stmt.executeUpdate();
-                    } catch (SQLException e) {
-                        mainException.addSuppressed(e);
                     }
                 }
             }
@@ -1479,8 +1458,6 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                             stmt.setString(2, group.getDescription());
                         }
                         stmt.executeUpdate();
-                    } catch (SQLException e) {
-                        mainException.addSuppressed(e);
                     }
                     Iterator<Role> roles = group.getRoles();
                     while (roles.hasNext()) {
@@ -1489,8 +1466,6 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                             stmt.setString(1, group.getGroupname());
                             stmt.setString(2, role.getRolename());
                             stmt.executeUpdate();
-                        } catch (SQLException e) {
-                            mainException.addSuppressed(e);
                         }
                     }
                 }
@@ -1511,15 +1486,11 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                             stmt.setString(1, group.getDescription());
                             stmt.setString(2, group.getGroupname());
                             stmt.executeUpdate();
-                        } catch (SQLException e) {
-                            mainException.addSuppressed(e);
                         }
                     }
                     try (PreparedStatement stmt = 
dbConnection.prepareStatement(groupRoleRelationDelete)) {
                         stmt.setString(1, group.getGroupname());
                         stmt.executeUpdate();
-                    } catch (SQLException e) {
-                        mainException.addSuppressed(e);
                     }
                     Iterator<Role> roles = group.getRoles();
                     while (roles.hasNext()) {
@@ -1528,8 +1499,6 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                             stmt.setString(1, group.getGroupname());
                             stmt.setString(2, role.getRolename());
                             stmt.executeUpdate();
-                        } catch (SQLException e) {
-                            mainException.addSuppressed(e);
                         }
                     }
                 }
@@ -1583,23 +1552,17 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                     try (PreparedStatement stmt = 
dbConnection.prepareStatement(userRoleRelationDelete)) {
                         stmt.setString(1, user.getUsername());
                         stmt.executeUpdate();
-                    } catch (SQLException e) {
-                        mainException.addSuppressed(e);
                     }
                 }
                 if (userGroupRelationDelete != null) {
                     try (PreparedStatement stmt = 
dbConnection.prepareStatement(userGroupRelationDelete)) {
                         stmt.setString(1, user.getUsername());
                         stmt.executeUpdate();
-                    } catch (SQLException e) {
-                        mainException.addSuppressed(e);
                     }
                 }
                 try (PreparedStatement stmt = 
dbConnection.prepareStatement(temp.toString())) {
                     stmt.setString(1, user.getUsername());
                     stmt.executeUpdate();
-                } catch (SQLException e) {
-                    mainException.addSuppressed(e);
                 }
             }
         }
@@ -1626,8 +1589,6 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                         stmt.setString(3, user.getFullName());
                     }
                     stmt.executeUpdate();
-                } catch (SQLException e) {
-                    mainException.addSuppressed(e);
                 }
                 if (userRoleRelation != null) {
                     Iterator<Role> roles = user.getRoles();
@@ -1637,8 +1598,6 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                             stmt.setString(1, user.getUsername());
                             stmt.setString(2, role.getRolename());
                             stmt.executeUpdate();
-                        } catch (SQLException e) {
-                            mainException.addSuppressed(e);
                         }
                     }
                 }
@@ -1650,8 +1609,6 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                             stmt.setString(1, user.getUsername());
                             stmt.setString(2, group.getGroupname());
                             stmt.executeUpdate();
-                        } catch (SQLException e) {
-                            mainException.addSuppressed(e);
                         }
                     }
                 }
@@ -1679,23 +1636,17 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                         stmt.setString(2, user.getUsername());
                     }
                     stmt.executeUpdate();
-                } catch (SQLException e) {
-                    mainException.addSuppressed(e);
                 }
                 if (userRoleRelationDelete != null) {
                     try (PreparedStatement stmt = 
dbConnection.prepareStatement(userRoleRelationDelete)) {
                         stmt.setString(1, user.getUsername());
                         stmt.executeUpdate();
-                    } catch (SQLException e) {
-                        mainException.addSuppressed(e);
                     }
                 }
                 if (userGroupRelationDelete != null) {
                     try (PreparedStatement stmt = 
dbConnection.prepareStatement(userGroupRelationDelete)) {
                         stmt.setString(1, user.getUsername());
                         stmt.executeUpdate();
-                    } catch (SQLException e) {
-                        mainException.addSuppressed(e);
                     }
                 }
                 if (userRoleRelation != null) {
@@ -1706,8 +1657,6 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                             stmt.setString(1, user.getUsername());
                             stmt.setString(2, role.getRolename());
                             stmt.executeUpdate();
-                        } catch (SQLException e) {
-                            mainException.addSuppressed(e);
                         }
                     }
                 }
@@ -1719,15 +1668,11 @@ public class DataSourceUserDatabase extends 
SparseUserDatabase {
                             stmt.setString(1, user.getUsername());
                             stmt.setString(2, group.getGroupname());
                             stmt.executeUpdate();
-                        } catch (SQLException e) {
-                            mainException.addSuppressed(e);
                         }
                     }
                 }
             }
         }
-
-        return mainException;
     }
 
     @Override


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to