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

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


The following commit(s) were added to refs/heads/9.0.x by this push:
     new 1a8ccd0361 Align exception behavior with FileStore
1a8ccd0361 is described below

commit 1a8ccd0361f521728d88fd3706b62862bfde0ad3
Author: remm <[email protected]>
AuthorDate: Thu May 21 14:45:35 2026 +0200

    Align exception behavior with FileStore
    
    Swallow exceptions could seem more robust but actually leads to
    immediate session loss. FileStore reports using IOException on
    save/remove, so do the same.
---
 .../apache/catalina/session/DataSourceStore.java   | 37 ++++++++++++++++------
 webapps/docs/changelog.xml                         |  4 +++
 2 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/java/org/apache/catalina/session/DataSourceStore.java 
b/java/org/apache/catalina/session/DataSourceStore.java
index fc23b58c12..7d0b76be7b 100644
--- a/java/org/apache/catalina/session/DataSourceStore.java
+++ b/java/org/apache/catalina/session/DataSourceStore.java
@@ -198,6 +198,8 @@ public class DataSourceStore extends JDBCStore {
     @Override
     public void remove(String id) throws IOException {
 
+        SQLException sqlException = null;
+
         int numberOfTries = 2;
         while (numberOfTries > 0) {
             Connection _conn = getConnection();
@@ -211,13 +213,20 @@ public class DataSourceStore extends JDBCStore {
                 // Break out after the finally block
                 numberOfTries = 0;
             } catch (SQLException e) {
-                
manager.getContext().getLogger().error(sm.getString(getStoreName() + 
".SQLException"), e);
+                // Keep the first exception just in case
+                if (sqlException == null) {
+                    sqlException = e;
+                }
             } finally {
                 release(_conn);
             }
             numberOfTries--;
         }
 
+        if (sqlException != null) {
+            throw new IOException(sm.getString(getStoreName() + 
".SQLException"), sqlException);
+        }
+
         if (manager.getContext().getLogger().isTraceEnabled()) {
             manager.getContext().getLogger().trace(sm.getString(getStoreName() 
+ ".removing", id, sessionTable));
         }
@@ -272,8 +281,17 @@ public class DataSourceStore extends JDBCStore {
         String saveSql = "INSERT INTO " + sessionTable + " (" + sessionIdCol + 
", " + sessionAppCol + ", " +
                 sessionDataCol + ", " + sessionValidCol + ", " + 
sessionMaxInactiveCol + ", " + sessionLastAccessedCol +
                 ") VALUES (?, ?, ?, ?, ?, ?)";
+        SQLException sqlException = null;
 
         synchronized (session) {
+
+            // First serialize session
+            ByteArrayOutputStream bos = new ByteArrayOutputStream();
+            try (ObjectOutputStream oos = new ObjectOutputStream(new 
BufferedOutputStream(bos))) {
+                ((StandardSession) session).writeObjectData(oos);
+            }
+            byte[] obs = bos.toByteArray();
+
             int numberOfTries = 2;
             while (numberOfTries > 0) {
                 Connection _conn = getConnection();
@@ -285,11 +303,6 @@ public class DataSourceStore extends JDBCStore {
                     // Remove session if it exists and insert again.
                     remove(session.getIdInternal(), _conn);
 
-                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
-                    try (ObjectOutputStream oos = new ObjectOutputStream(new 
BufferedOutputStream(bos))) {
-                        ((StandardSession) session).writeObjectData(oos);
-                    }
-                    byte[] obs = bos.toByteArray();
                     int size = obs.length;
                     try (ByteArrayInputStream bis = new 
ByteArrayInputStream(obs, 0, size);
                             InputStream in = new BufferedInputStream(bis, 
size);
@@ -303,11 +316,13 @@ public class DataSourceStore extends JDBCStore {
                         preparedSaveSql.execute();
                         // Break out after the finally block
                         numberOfTries = 0;
+                        sqlException = null;
                     }
                 } catch (SQLException e) {
-                    
manager.getContext().getLogger().error(sm.getString(getStoreName() + 
".SQLException"), e);
-                } catch (IOException ignore) {
-                    // Ignore
+                    // Keep the first exception just in case
+                    if (sqlException == null) {
+                        sqlException = e;
+                    }
                 } finally {
                     release(_conn);
                 }
@@ -315,6 +330,10 @@ public class DataSourceStore extends JDBCStore {
             }
         }
 
+        if (sqlException != null) {
+            throw new IOException(sm.getString(getStoreName() + 
".SQLException"), sqlException);
+        }
+
         if (manager.getContext().getLogger().isTraceEnabled()) {
             manager.getContext().getLogger()
                     .trace(sm.getString(getStoreName() + ".saving", 
session.getIdInternal(), sessionTable));
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index e7384b91c0..f9555524cd 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -152,6 +152,10 @@
       <fix>
         Fix hour unit used by <code>ExpiresFilter</code>. (remm)
       </fix>
+      <fix>
+        Remove exception swallowing in <code>DataSourceStore</code> to align
+        it with <code>FileStore</code> and avoid session loss on errors. (remm)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">


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

Reply via email to