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


##########
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:
   Is it possible to put all the handlers in the same block [1]?  e.g.
   
   ```
   } catch (SQLException | RuntimeException | Error e) {
       // looks like mostly the same block to me
   }
   ```
   
   We don't need to support Java 6
   
   [1] 
https://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html



-- 
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