From 9576763c675e1d3fbcf554006cc56b1bda7d1c17 Mon Sep 17 00:00:00 2001 From: olivier lamy Date: Thu, 28 Nov 2019 11:15:48 +1000 Subject: [PATCH 1/4] avoid non necessary objects allocation if session do not have any attributes data Signed-off-by: olivier lamy --- .../server/session/JDBCSessionDataStore.java | 53 ++++++++++++------- .../jetty/server/session/SessionData.java | 2 +- .../jetty/server/session/JdbcTestHelper.java | 33 ++++++------ 3 files changed, 52 insertions(+), 36 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionDataStore.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionDataStore.java index 155ada9009e..dd08792d54c 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionDataStore.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionDataStore.java @@ -28,6 +28,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.sql.Types; import java.util.HashSet; import java.util.Set; @@ -58,6 +59,8 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore protected SessionTableSchema _sessionTableSchema; protected boolean _schemaProvided; + private static final ByteArrayInputStream EMPTY = new ByteArrayInputStream(new byte[0]); + /** * SessionTableSchema */ @@ -707,17 +710,23 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore statement.setLong(10, data.getExpiry()); statement.setLong(11, data.getMaxInactiveMs()); - try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos)) + if(!data.getAllAttributes().isEmpty()) { - SessionData.serializeAttributes(data, oos); - byte[] bytes = baos.toByteArray(); - ByteArrayInputStream bais = new ByteArrayInputStream(bytes); - statement.setBinaryStream(12, bais, bytes.length);//attribute map as blob - statement.executeUpdate(); - if (LOG.isDebugEnabled()) - LOG.debug("Inserted session " + data); + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos)) + { + SessionData.serializeAttributes( data, oos ); + byte[] bytes = baos.toByteArray(); + ByteArrayInputStream bais = new ByteArrayInputStream( bytes ); + statement.setBinaryStream( 12, bais, bytes.length );//attribute map as blob + } } + else + { + statement.setBinaryStream( 12, EMPTY, 0); + } + statement.executeUpdate(); + if ( LOG.isDebugEnabled() ) LOG.debug( "Inserted session " + data ); } } } @@ -737,20 +746,26 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore statement.setLong(5, data.getExpiry()); statement.setLong(6, data.getMaxInactiveMs()); - try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos)) + if(!data.getAllAttributes().isEmpty()) { - SessionData.serializeAttributes(data, oos); - byte[] bytes = baos.toByteArray(); - try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos)) { - statement.setBinaryStream(7, bais, bytes.length);//attribute map as blob - statement.executeUpdate(); - - if (LOG.isDebugEnabled()) - LOG.debug("Updated session " + data); + SessionData.serializeAttributes(data, oos); + byte[] bytes = baos.toByteArray(); + try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) + { + statement.setBinaryStream( 7, bais, bytes.length );//attribute map as blob + } } } + else + { + statement.setBinaryStream( 7, EMPTY, 0); + } + statement.executeUpdate(); + + if ( LOG.isDebugEnabled() ) LOG.debug( "Updated session " + data ); } } } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionData.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionData.java index bd2cb95d4dd..b93c0d04140 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionData.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionData.java @@ -156,7 +156,7 @@ public class SessionData implements Serializable LOG.info("Legacy serialization detected for {}", data.getId()); //legacy serialization was used, we have just deserialized the //entire attribute map - data._attributes = new ConcurrentHashMap(); + data._attributes = new ConcurrentHashMap<>(); data.putAllAttributes((Map)o); } } diff --git a/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/JdbcTestHelper.java b/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/JdbcTestHelper.java index 6a66daadf65..25e88876e6a 100644 --- a/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/JdbcTestHelper.java +++ b/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/JdbcTestHelper.java @@ -173,17 +173,15 @@ public class JdbcTestHelper ResultSet result = null; try (Connection con = DriverManager.getConnection(DEFAULT_CONNECTION_URL);) { - statement = con.prepareStatement("select * from " + TABLE + - " where " + ID_COL + " = ? and " + CONTEXT_COL + - " = ? and virtualHost = ?"); - statement.setString(1, data.getId()); - statement.setString(2, data.getContextPath()); - statement.setString(3, data.getVhost()); + statement = con.prepareStatement( + "select * from " + TABLE + " where " + ID_COL + " = ? and " + CONTEXT_COL + " = ? and virtualHost = ?" ); + statement.setString( 1, data.getId() ); + statement.setString( 2, data.getContextPath() ); + statement.setString( 3, data.getVhost() ); result = statement.executeQuery(); - if (!result.next()) - return false; + if ( !result.next() ) return false; assertEquals(data.getCreated(), result.getLong(CREATE_COL)); assertEquals(data.getAccessed(), result.getLong(ACCESS_COL)); @@ -197,18 +195,21 @@ public class JdbcTestHelper assertEquals(data.getContextPath(), result.getString(CONTEXT_COL)); assertEquals(data.getVhost(), result.getString("virtualHost")); - Blob blob = result.getBlob(MAP_COL); + Blob blob = result.getBlob( MAP_COL ); - SessionData tmp = new SessionData(data.getId(), data.getContextPath(), data.getVhost(), result.getLong(CREATE_COL), - result.getLong(ACCESS_COL), result.getLong(LAST_ACCESS_COL), result.getLong(MAX_IDLE_COL)); + SessionData tmp = + new SessionData( data.getId(), data.getContextPath(), data.getVhost(), result.getLong( CREATE_COL ), + result.getLong( ACCESS_COL ), result.getLong( LAST_ACCESS_COL ), + result.getLong( MAX_IDLE_COL ) ); - try (InputStream is = blob.getBinaryStream(); - ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(is)) + if (blob.length() > 0) { - - SessionData.deserializeAttributes(tmp, ois); + try (InputStream is = blob.getBinaryStream(); + ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(is)) + { + SessionData.deserializeAttributes(tmp, ois); + } } - //same number of attributes assertEquals(data.getAllAttributes().size(), tmp.getAllAttributes().size()); //same keys From e58f570266a6d966442f52d1e5a312501f0eecce Mon Sep 17 00:00:00 2001 From: olivier lamy Date: Thu, 28 Nov 2019 13:42:52 +1000 Subject: [PATCH 2/4] remove unused import Signed-off-by: olivier lamy --- .../org/eclipse/jetty/server/session/JDBCSessionDataStore.java | 1 - 1 file changed, 1 deletion(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionDataStore.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionDataStore.java index dd08792d54c..6b2aa59ed33 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionDataStore.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/JDBCSessionDataStore.java @@ -28,7 +28,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.sql.Types; import java.util.HashSet; import java.util.Set; From 1b49e72d596a5d254fd7739d5c8c083abd02fdb9 Mon Sep 17 00:00:00 2001 From: olivier lamy Date: Thu, 28 Nov 2019 13:44:49 +1000 Subject: [PATCH 3/4] formatting Signed-off-by: olivier lamy --- .../jetty/server/session/JdbcTestHelper.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/JdbcTestHelper.java b/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/JdbcTestHelper.java index 25e88876e6a..cc44dc743cf 100644 --- a/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/JdbcTestHelper.java +++ b/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/JdbcTestHelper.java @@ -174,14 +174,16 @@ public class JdbcTestHelper try (Connection con = DriverManager.getConnection(DEFAULT_CONNECTION_URL);) { statement = con.prepareStatement( - "select * from " + TABLE + " where " + ID_COL + " = ? and " + CONTEXT_COL + " = ? and virtualHost = ?" ); - statement.setString( 1, data.getId() ); - statement.setString( 2, data.getContextPath() ); - statement.setString( 3, data.getVhost() ); + "select * from " + TABLE + + " where " + ID_COL + " = ? and " + CONTEXT_COL + + " = ? and virtualHost = ?" ); + statement.setString(1, data.getId() ); + statement.setString(2, data.getContextPath() ); + statement.setString(3, data.getVhost() ); result = statement.executeQuery(); - if ( !result.next() ) return false; + if (!result.next()) return false; assertEquals(data.getCreated(), result.getLong(CREATE_COL)); assertEquals(data.getAccessed(), result.getLong(ACCESS_COL)); @@ -195,7 +197,7 @@ public class JdbcTestHelper assertEquals(data.getContextPath(), result.getString(CONTEXT_COL)); assertEquals(data.getVhost(), result.getString("virtualHost")); - Blob blob = result.getBlob( MAP_COL ); + Blob blob = result.getBlob(MAP_COL); SessionData tmp = new SessionData( data.getId(), data.getContextPath(), data.getVhost(), result.getLong( CREATE_COL ), From d321c2f0341f3e093f58f4624ec7c44ad272fcf6 Mon Sep 17 00:00:00 2001 From: olivier lamy Date: Thu, 28 Nov 2019 14:15:54 +1000 Subject: [PATCH 4/4] checkstyle Signed-off-by: olivier lamy --- .../eclipse/jetty/server/session/JdbcTestHelper.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/JdbcTestHelper.java b/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/JdbcTestHelper.java index cc44dc743cf..43c865aa537 100644 --- a/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/JdbcTestHelper.java +++ b/tests/test-sessions/test-jdbc-sessions/src/test/java/org/eclipse/jetty/server/session/JdbcTestHelper.java @@ -177,9 +177,9 @@ public class JdbcTestHelper "select * from " + TABLE + " where " + ID_COL + " = ? and " + CONTEXT_COL + " = ? and virtualHost = ?" ); - statement.setString(1, data.getId() ); - statement.setString(2, data.getContextPath() ); - statement.setString(3, data.getVhost() ); + statement.setString(1, data.getId()); + statement.setString(2, data.getContextPath()); + statement.setString(3, data.getVhost()); result = statement.executeQuery(); @@ -200,9 +200,9 @@ public class JdbcTestHelper Blob blob = result.getBlob(MAP_COL); SessionData tmp = - new SessionData( data.getId(), data.getContextPath(), data.getVhost(), result.getLong( CREATE_COL ), - result.getLong( ACCESS_COL ), result.getLong( LAST_ACCESS_COL ), - result.getLong( MAX_IDLE_COL ) ); + new SessionData( data.getId(), data.getContextPath(), data.getVhost(), result.getLong(CREATE_COL), + result.getLong(ACCESS_COL), result.getLong(LAST_ACCESS_COL), + result.getLong(MAX_IDLE_COL)); if (blob.length() > 0) {