Merge pull request #4375 from eclipse/jetty-9.4.x_small_improvment_jdbc
avoid non necessary objects allocation if session do not have any attributes data
This commit is contained in:
commit
ccf04a47cd
|
@ -58,6 +58,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 +709,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 +745,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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String, Object>();
|
||||
data._attributes = new ConcurrentHashMap<>();
|
||||
data.putAllAttributes((Map<String, Object>)o);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -173,17 +173,17 @@ 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 = 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));
|
||||
|
@ -199,16 +199,19 @@ 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));
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue