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 21469a10a3 Align exception behavior with FileStore
21469a10a3 is described below
commit 21469a10a37458612e760075d93b5715ce2d666c
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 1b0b746516..24436af58b 100644
--- a/java/org/apache/catalina/session/DataSourceStore.java
+++ b/java/org/apache/catalina/session/DataSourceStore.java
@@ -481,6 +481,8 @@ public class DataSourceStore extends StoreBase {
@Override
public void remove(String id) throws IOException {
+ SQLException sqlException = null;
+
int numberOfTries = 2;
while (numberOfTries > 0) {
Connection _conn = getConnection();
@@ -494,13 +496,20 @@ public class DataSourceStore extends StoreBase {
// Break out after the finally block
numberOfTries = 0;
} catch (SQLException e) {
-
manager.getContext().getLogger().error(sm.getString("dataSourceStore.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("dataSourceStore.SQLException"), sqlException);
+ }
+
if (manager.getContext().getLogger().isTraceEnabled()) {
manager.getContext().getLogger().trace(sm.getString("dataSourceStore.removing",
id, sessionTable));
}
@@ -555,8 +564,17 @@ public class DataSourceStore extends StoreBase {
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();
@@ -568,11 +586,6 @@ public class DataSourceStore extends StoreBase {
// 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);
@@ -586,11 +599,13 @@ public class DataSourceStore extends StoreBase {
preparedSaveSql.execute();
// Break out after the finally block
numberOfTries = 0;
+ sqlException = null;
}
} catch (SQLException e) {
-
manager.getContext().getLogger().error(sm.getString("dataSourceStore.SQLException"),
e);
- } catch (IOException ioe) {
- // Ignore
+ // Keep the first exception just in case
+ if (sqlException == null) {
+ sqlException = e;
+ }
} finally {
release(_conn);
}
@@ -598,6 +613,10 @@ public class DataSourceStore extends StoreBase {
}
}
+ if (sqlException != null) {
+ throw new
IOException(sm.getString("dataSourceStore.SQLException"), sqlException);
+ }
+
if (manager.getContext().getLogger().isTraceEnabled()) {
manager.getContext().getLogger()
.trace(sm.getString("dataSourceStore.saving",
session.getIdInternal(), sessionTable));
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index e50820a6b7..2971b3ac6d 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -156,6 +156,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]