ChristopherSchultz commented on code in PR #596:
URL: https://github.com/apache/tomcat/pull/596#discussion_r1131033698


##########
java/org/apache/catalina/session/DataSourceStore.java:
##########
@@ -626,15 +626,77 @@ public void save(Session session) throws IOException {
                     byte[] obs = bos.toByteArray();
                     int size = obs.length;
                     try (ByteArrayInputStream bis = new 
ByteArrayInputStream(obs, 0, size);
-                            InputStream in = new BufferedInputStream(bis, 
size);
-                            PreparedStatement preparedSaveSql = 
_conn.prepareStatement(saveSql)) {
-                        preparedSaveSql.setString(1, session.getIdInternal());
-                        preparedSaveSql.setString(2, getName());
-                        preparedSaveSql.setBinaryStream(3, in, size);
-                        preparedSaveSql.setString(4, session.isValid() ? "1" : 
"0");
-                        preparedSaveSql.setInt(5, 
session.getMaxInactiveInterval());
-                        preparedSaveSql.setLong(6, 
session.getLastAccessedTime());
-                        preparedSaveSql.execute();
+                         InputStream in = new BufferedInputStream(bis, size);
+                         PreparedStatement preparedSaveSql = 
_conn.prepareStatement(saveSql, ResultSet.TYPE_FORWARD_ONLY, 
ResultSet.CONCUR_UPDATABLE)) {
+
+                        // Store auto-commit state
+                        boolean autoCommit = _conn.getAutoCommit();
+
+                        try {
+                            if(autoCommit) {
+                                _conn.setAutoCommit(false); // BEGIN 
TRANSACTION
+                            }
+
+                            preparedSaveSql.setString(1, getName());
+                            preparedSaveSql.setString(2, 
session.getIdInternal());
+
+                            ResultSet rs = preparedSaveSql.executeQuery();
+
+                            if(rs.next()) {
+                                // Session already exists in the db; update 
the various fields
+                                rs.updateBinaryStream(sessionDataCol, in, 
size);
+                                rs.updateString(sessionValidCol, 
session.isValid() ? "1" : "0");
+                                rs.updateInt(sessionMaxInactiveCol, 
session.getMaxInactiveInterval());
+                                rs.updateLong(sessionLastAccessedCol, 
session.getLastAccessedTime());
+
+                                rs.updateRow();
+                            } else {
+                                // Session does not exist. Insert.
+                                rs.moveToInsertRow();
+
+                                rs.updateString(sessionAppCol, getName());
+                                rs.updateString(sessionIdCol, 
session.getIdInternal());
+                                rs.updateBinaryStream(sessionIdCol, in, size);
+                                rs.updateString(sessionValidCol, 
session.isValid() ? "1" : "0");
+                                rs.updateInt(sessionMaxInactiveCol, 
session.getMaxInactiveInterval());
+                                rs.updateLong(sessionLastAccessedCol, 
session.getLastAccessedTime());
+
+                                rs.updateRow();
+                            }
+
+                            _conn.commit();
+                        } catch (SQLException sqle) {

Review Comment:
   I would be okay merging them together, but we might want to use different 
error messages. This is one of the things I wanted to get feedback on: how 
important is it to say "we got an Error" vs "we got an SQLException", etc. when 
the actual exception and stack trace will likely be logged? I think it's 
probably okay to merge these exception handlers together but wanted some 
feedback about the logging.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to