#477 Make updateSession() timestamp-aware; fetch timestamp safely

This commit is contained in:
ljacqu 2016-02-14 09:51:13 +01:00
parent 7b26dd25a0
commit 6e2528278a
2 changed files with 21 additions and 6 deletions

View File

@ -587,14 +587,14 @@ public class MySQL implements DataSource {
+ columnIp + "=?, " + columnLastLogin + "=?, " + columnRealName + "=? WHERE " + columnName + "=?;"; + columnIp + "=?, " + columnLastLogin + "=?, " + columnRealName + "=? WHERE " + columnName + "=?;";
PreparedStatement pst = con.prepareStatement(sql); PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, auth.getIp()); pst.setString(1, auth.getIp());
pst.setLong(2, auth.getLastLogin()); pst.setTimestamp(2, new Timestamp(auth.getLastLogin()));
pst.setString(3, auth.getRealName()); pst.setString(3, auth.getRealName());
pst.setString(4, auth.getNickname()); pst.setString(4, auth.getNickname());
pst.executeUpdate(); pst.executeUpdate();
pst.close(); pst.close();
return true; return true;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); logSqlException(ex);
} }
return false; return false;
} }
@ -985,7 +985,7 @@ public class MySQL implements DataSource {
.name(row.getString(columnName)) .name(row.getString(columnName))
.realName(row.getString(columnRealName)) .realName(row.getString(columnRealName))
.password(row.getString(columnPassword), salt) .password(row.getString(columnPassword), salt)
.lastLogin(row.getTimestamp(columnLastLogin).getTime()) .lastLogin(safeGetTimestamp(row))
.ip(row.getString(columnIp)) .ip(row.getString(columnIp))
.locWorld(row.getString(lastlocWorld)) .locWorld(row.getString(lastlocWorld))
.locX(row.getDouble(lastlocX)) .locX(row.getDouble(lastlocX))
@ -996,6 +996,21 @@ public class MySQL implements DataSource {
.build(); .build();
} }
/**
* Retrieve the last login timestamp in a safe way.
*
* @param row The ResultSet to read
* @return The timestamp (as number of milliseconds since 1970-01-01 00:00:00 GMT)
*/
private long safeGetTimestamp(ResultSet row) {
try {
return row.getTimestamp(columnLastLogin).getTime();
} catch (SQLException e) {
ConsoleLogger.logException("Could not get timestamp from resultSet. Defaulting to current time", e);
}
return System.currentTimeMillis();
}
private void migrateLastLoginColumnToTimestamp(Connection con, ResultSet rs) throws SQLException { private void migrateLastLoginColumnToTimestamp(Connection con, ResultSet rs) throws SQLException {
final int columnType = rs.getInt("DATA_TYPE"); final int columnType = rs.getInt("DATA_TYPE");
if (columnType == Types.BIGINT) { if (columnType == Types.BIGINT) {

View File

@ -19,9 +19,9 @@ public final class SettingsFieldRetriever {
/** The classes to scan for properties. */ /** The classes to scan for properties. */
private static final List<Class<? extends SettingsClass>> CONFIGURATION_CLASSES = Arrays.asList( private static final List<Class<? extends SettingsClass>> CONFIGURATION_CLASSES = Arrays.asList(
ConverterSettings.class, PluginSettings.class, RestrictionSettings.class, DatabaseSettings.class, ConverterSettings.class, PluginSettings.class,
DatabaseSettings.class, EmailSettings.class, HooksSettings.class, RestrictionSettings.class, EmailSettings.class, HooksSettings.class,
ProtectionSettings.class, PurgeSettings.class, SecuritySettings.class, ProtectionSettings.class, PurgeSettings.class, SecuritySettings.class,
RegistrationSettings.class, BackupSettings.class); RegistrationSettings.class, BackupSettings.class);
private SettingsFieldRetriever() { private SettingsFieldRetriever() {