tokens are now cleared on startup

This commit is contained in:
Rohan Sircar 2020-01-15 15:57:09 +05:30
parent 40ff3ca9bf
commit 7e67c60a90
3 changed files with 17 additions and 33 deletions

View File

@ -19,11 +19,15 @@ public class DBInitializerConfig {
private final String resetSessionsQuery;
private final String clearTokensQuery;
public DBInitializerConfig(@Value("${chatto.datasource.database-name}") String dbName,
@Value("${num-tables}") String numTablesQuery, @Value("${reset-sessions}") String resetSessionsQuery) {
@Value("${num-tables}") String numTablesQuery, @Value("${reset-sessions}") String resetSessionsQuery,
@Value("${clear-tokens}") String clearTokensQuery) {
this.dbName = dbName;
this.numTablesQuery = numTablesQuery;
this.resetSessionsQuery = resetSessionsQuery;
this.clearTokensQuery = clearTokensQuery;
}
}

View File

@ -1,7 +1,6 @@
package org.ros.chatto.service;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -15,9 +14,6 @@ import org.hibernate.Session;
import org.ros.chatto.config.DBInitializerConfig;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
@ -33,16 +29,6 @@ public class DBInitializerService {
@PersistenceContext
private EntityManager entityManager;
public int getNumTables(final Connection connection) throws SQLException {
final PreparedStatement preparedStatement = connection.prepareStatement(dbInitializerConfig.getNumTablesQuery());
preparedStatement.setString(1, dbInitializerConfig.getDbName());
final ResultSet resultSet = preparedStatement.executeQuery();
resultSet.next();
final int numTables = resultSet.getInt("num_tables");
return numTables;
}
@EventListener(ApplicationReadyEvent.class)
@Transactional
public void doSomethingAfterStartup() throws SQLException, IOException {
@ -50,16 +36,10 @@ public class DBInitializerService {
final Session session = entityManager.unwrap(Session.class);
// session.doWork(connection -> {
// if (getNumTables(connection) == 0) {
// log.info("Database is empty. Populating tables and roles");
// try {
// populateDB(connection);
// } catch (final IOException e) {
// log.error("IO error", e);
// }
// }
// });
session.doWork(connection -> {
log.info("Clearing tokens");
clearAllTokens(connection);
});
session.doWork(connection -> {
log.info("Resetting all user sessions");
@ -67,16 +47,15 @@ public class DBInitializerService {
});
}
private void populateDB(final Connection connection) throws SQLException, IOException {
ScriptUtils.executeSqlScript(connection,
new EncodedResource(new ClassPathResource("scheme.sql"), StandardCharsets.UTF_8));
ScriptUtils.executeSqlScript(connection,
new EncodedResource(new ClassPathResource("datae.sql"), StandardCharsets.UTF_8));
private void resetAllUserSessions(final Connection connection) throws SQLException {
final PreparedStatement preparedStatement = connection
.prepareStatement(dbInitializerConfig.getResetSessionsQuery());
preparedStatement.executeUpdate();
}
private void resetAllUserSessions(final Connection connection) throws SQLException {
final PreparedStatement preparedStatement = connection.prepareStatement(dbInitializerConfig.getResetSessionsQuery());
private void clearAllTokens(final Connection connection) throws SQLException {
final PreparedStatement preparedStatement = connection
.prepareStatement(dbInitializerConfig.getClearTokensQuery());
preparedStatement.executeUpdate();
}

View File

@ -1,3 +1,4 @@
num-tables = SELECT COUNT(*) as num_tables FROM information_schema.tables WHERE table_schema = ? and TABLE_TYPE='BASE TABLE'
reset-sessions = update user_sessions set online=0, num_sessions=0
clear-tokens = truncate table tokens
#tables-created =