Chatto/src/main/java/org/ros/chatto/service/DBInitializerService.java

61 lines
1.8 KiB
Java
Raw Normal View History

2019-10-04 10:10:26 +00:00
package org.ros.chatto.service;
2019-10-15 05:28:56 +00:00
import java.io.IOException;
2019-10-04 10:10:26 +00:00
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import org.hibernate.Session;
import org.ros.chatto.config.DBInitializerConfig;
2019-10-15 05:28:56 +00:00
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
2019-10-04 10:10:26 +00:00
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
2019-10-04 10:10:26 +00:00
@Service
@Slf4j
@RequiredArgsConstructor
2019-10-04 10:10:26 +00:00
public class DBInitializerService {
private final DBInitializerConfig dbInitializerConfig;
@PersistenceContext
private EntityManager entityManager;
2019-10-04 10:10:26 +00:00
2019-10-15 05:28:56 +00:00
@EventListener(ApplicationReadyEvent.class)
@Transactional
2019-10-15 05:28:56 +00:00
public void doSomethingAfterStartup() throws SQLException, IOException {
log.info("Application Started - running initializer service");
final Session session = entityManager.unwrap(Session.class);
2020-01-15 10:27:09 +00:00
session.doWork(connection -> {
log.info("Clearing tokens");
clearAllTokens(connection);
});
session.doWork(connection -> {
log.info("Resetting all user sessions");
resetAllUserSessions(connection);
});
}
2020-01-15 10:27:09 +00:00
private void resetAllUserSessions(final Connection connection) throws SQLException {
final PreparedStatement preparedStatement = connection
.prepareStatement(dbInitializerConfig.getResetSessionsQuery());
preparedStatement.executeUpdate();
2019-10-04 10:10:26 +00:00
}
2020-01-15 10:27:09 +00:00
private void clearAllTokens(final Connection connection) throws SQLException {
final PreparedStatement preparedStatement = connection
.prepareStatement(dbInitializerConfig.getClearTokensQuery());
preparedStatement.executeUpdate();
}
2019-10-04 10:10:26 +00:00
}