avoid non necessary objects allocation if session do not have any attributes data

Signed-off-by: olivier lamy <oliver.lamy@gmail.com>
This commit is contained in:
olivier lamy 2019-11-28 11:15:48 +10:00
parent dc1a3384b0
commit 9576763c67
3 changed files with 52 additions and 36 deletions

View File

@ -28,6 +28,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Types;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -58,6 +59,8 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
protected SessionTableSchema _sessionTableSchema; protected SessionTableSchema _sessionTableSchema;
protected boolean _schemaProvided; protected boolean _schemaProvided;
private static final ByteArrayInputStream EMPTY = new ByteArrayInputStream(new byte[0]);
/** /**
* SessionTableSchema * SessionTableSchema
*/ */
@ -707,17 +710,23 @@ public class JDBCSessionDataStore extends AbstractSessionDataStore
statement.setLong(10, data.getExpiry()); statement.setLong(10, data.getExpiry());
statement.setLong(11, data.getMaxInactiveMs()); statement.setLong(11, data.getMaxInactiveMs());
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); if(!data.getAllAttributes().isEmpty())
ObjectOutputStream oos = new ObjectOutputStream(baos))
{ {
SessionData.serializeAttributes(data, oos); try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes = baos.toByteArray(); ObjectOutputStream oos = new ObjectOutputStream(baos))
ByteArrayInputStream bais = new ByteArrayInputStream(bytes); {
statement.setBinaryStream(12, bais, bytes.length);//attribute map as blob SessionData.serializeAttributes( data, oos );
statement.executeUpdate(); byte[] bytes = baos.toByteArray();
if (LOG.isDebugEnabled()) ByteArrayInputStream bais = new ByteArrayInputStream( bytes );
LOG.debug("Inserted session " + data); 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(5, data.getExpiry());
statement.setLong(6, data.getMaxInactiveMs()); statement.setLong(6, data.getMaxInactiveMs());
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); if(!data.getAllAttributes().isEmpty())
ObjectOutputStream oos = new ObjectOutputStream(baos))
{ {
SessionData.serializeAttributes(data, oos); try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes = baos.toByteArray(); ObjectOutputStream oos = new ObjectOutputStream(baos))
try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes))
{ {
statement.setBinaryStream(7, bais, bytes.length);//attribute map as blob SessionData.serializeAttributes(data, oos);
statement.executeUpdate(); byte[] bytes = baos.toByteArray();
try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes))
if (LOG.isDebugEnabled()) {
LOG.debug("Updated session " + data); 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 );
} }
} }
} }

View File

@ -156,7 +156,7 @@ public class SessionData implements Serializable
LOG.info("Legacy serialization detected for {}", data.getId()); LOG.info("Legacy serialization detected for {}", data.getId());
//legacy serialization was used, we have just deserialized the //legacy serialization was used, we have just deserialized the
//entire attribute map //entire attribute map
data._attributes = new ConcurrentHashMap<String, Object>(); data._attributes = new ConcurrentHashMap<>();
data.putAllAttributes((Map<String, Object>)o); data.putAllAttributes((Map<String, Object>)o);
} }
} }

View File

@ -173,17 +173,15 @@ public class JdbcTestHelper
ResultSet result = null; ResultSet result = null;
try (Connection con = DriverManager.getConnection(DEFAULT_CONNECTION_URL);) try (Connection con = DriverManager.getConnection(DEFAULT_CONNECTION_URL);)
{ {
statement = con.prepareStatement("select * from " + TABLE + statement = con.prepareStatement(
" where " + ID_COL + " = ? and " + CONTEXT_COL + "select * from " + TABLE + " where " + ID_COL + " = ? and " + CONTEXT_COL + " = ? and virtualHost = ?" );
" = ? and virtualHost = ?"); statement.setString( 1, data.getId() );
statement.setString(1, data.getId()); statement.setString( 2, data.getContextPath() );
statement.setString(2, data.getContextPath()); statement.setString( 3, data.getVhost() );
statement.setString(3, data.getVhost());
result = statement.executeQuery(); result = statement.executeQuery();
if (!result.next()) if ( !result.next() ) return false;
return false;
assertEquals(data.getCreated(), result.getLong(CREATE_COL)); assertEquals(data.getCreated(), result.getLong(CREATE_COL));
assertEquals(data.getAccessed(), result.getLong(ACCESS_COL)); assertEquals(data.getAccessed(), result.getLong(ACCESS_COL));
@ -197,18 +195,21 @@ public class JdbcTestHelper
assertEquals(data.getContextPath(), result.getString(CONTEXT_COL)); assertEquals(data.getContextPath(), result.getString(CONTEXT_COL));
assertEquals(data.getVhost(), result.getString("virtualHost")); 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), SessionData tmp =
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 ) );
try (InputStream is = blob.getBinaryStream(); if (blob.length() > 0)
ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(is))
{ {
try (InputStream is = blob.getBinaryStream();
SessionData.deserializeAttributes(tmp, ois); ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(is))
{
SessionData.deserializeAttributes(tmp, ois);
}
} }
//same number of attributes //same number of attributes
assertEquals(data.getAllAttributes().size(), tmp.getAllAttributes().size()); assertEquals(data.getAllAttributes().size(), tmp.getAllAttributes().size());
//same keys //same keys