db init service now uses hibernate connection instead of separate connection. Also, user sessions are now reset using jdbc as well
This commit is contained in:
parent
bee90dcef0
commit
0ecfda9980
@ -12,6 +12,11 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
import javax.transaction.Transactional;
|
||||||
|
|
||||||
|
import org.hibernate.Session;
|
||||||
import org.ros.chatto.model.ApplicationStatus;
|
import org.ros.chatto.model.ApplicationStatus;
|
||||||
import org.ros.chatto.model.UserSession;
|
import org.ros.chatto.model.UserSession;
|
||||||
import org.ros.chatto.repository.UserSessionRepository;
|
import org.ros.chatto.repository.UserSessionRepository;
|
||||||
@ -54,9 +59,11 @@ public class DBInitializerService {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserSessionRepository userSessionRepository;
|
private UserSessionRepository userSessionRepository;
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(DBInitializerService.class);
|
private final Logger logger = LoggerFactory.getLogger(DBInitializerService.class);
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager entityManager;
|
||||||
|
|
||||||
public void connectDB() throws SQLException {
|
public void connectDB() throws SQLException {
|
||||||
connection = DriverManager.getConnection(url, userName, password);
|
connection = DriverManager.getConnection(url, userName, password);
|
||||||
@ -70,6 +77,21 @@ public class DBInitializerService {
|
|||||||
// while(resultSet.next())
|
// while(resultSet.next())
|
||||||
// {
|
// {
|
||||||
// System.out.println(resultSet.get);
|
// System.out.println(resultSet.get);
|
||||||
|
// }
|
||||||
|
resultSet.next();
|
||||||
|
int numTables = resultSet.getInt("num_tables");
|
||||||
|
// System.out.println(numTables);
|
||||||
|
return numTables;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNumTables(Connection connection) throws SQLException {
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(numTablesQuery);
|
||||||
|
// preparedStatement.get
|
||||||
|
preparedStatement.setString(1, dbName);
|
||||||
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
// while(resultSet.next())
|
||||||
|
// {
|
||||||
|
// System.out.println(resultSet.get);
|
||||||
// }
|
// }
|
||||||
resultSet.next();
|
resultSet.next();
|
||||||
int numTables = resultSet.getInt("num_tables");
|
int numTables = resultSet.getInt("num_tables");
|
||||||
@ -78,25 +100,51 @@ public class DBInitializerService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@EventListener(ApplicationReadyEvent.class)
|
@EventListener(ApplicationReadyEvent.class)
|
||||||
|
@Transactional
|
||||||
public void doSomethingAfterStartup() throws SQLException, IOException {
|
public void doSomethingAfterStartup() throws SQLException, IOException {
|
||||||
// setProperties();
|
// setProperties();
|
||||||
|
|
||||||
connectDB();
|
// connectDB();
|
||||||
|
|
||||||
logger.info("Application Started - running initializer service");
|
logger.info("Application Started - running initializer service");
|
||||||
// System.out.println("Checking database and application state");
|
// System.out.println("Checking database and application state");
|
||||||
//
|
|
||||||
|
|
||||||
if (getNumTables() == 0) {
|
Session session = entityManager.unwrap(Session.class);
|
||||||
logger.info("Database is empty. Populating tables and roles");
|
|
||||||
populateDB();
|
session.doWork(connection -> {
|
||||||
}
|
if (getNumTables(connection) == 0) {
|
||||||
closeConnection();
|
logger.info("Database is empty. Populating tables and roles");
|
||||||
|
try {
|
||||||
|
populateDB(connection);
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// if (getNumTables() == 0) {
|
||||||
|
// logger.info("Database is empty. Populating tables and roles");
|
||||||
|
// populateDB();
|
||||||
|
// }
|
||||||
|
// closeConnection();
|
||||||
|
|
||||||
|
session.doWork(connection -> {
|
||||||
|
resetAllUserSessions(connection);
|
||||||
|
});
|
||||||
|
|
||||||
resetAllUserSessions(userSessionRepository.findAll());
|
resetAllUserSessions(userSessionRepository.findAll());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void populateDB() throws SQLException, IOException {
|
private void populateDB() 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 populateDB(Connection connection) throws SQLException, IOException {
|
||||||
ScriptUtils.executeSqlScript(connection,
|
ScriptUtils.executeSqlScript(connection,
|
||||||
new EncodedResource(new ClassPathResource("scheme.sql"), StandardCharsets.UTF_8));
|
new EncodedResource(new ClassPathResource("scheme.sql"), StandardCharsets.UTF_8));
|
||||||
ScriptUtils.executeSqlScript(connection,
|
ScriptUtils.executeSqlScript(connection,
|
||||||
@ -114,7 +162,6 @@ public class DBInitializerService {
|
|||||||
new EncodedResource(new ClassPathResource("datae.sql"), StandardCharsets.UTF_8));
|
new EncodedResource(new ClassPathResource("datae.sql"), StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Map<String, Boolean> listToMap(List<ApplicationStatus> applicationStatusList) {
|
Map<String, Boolean> listToMap(List<ApplicationStatus> applicationStatusList) {
|
||||||
Map<String, Boolean> statusMap = new HashMap<>();
|
Map<String, Boolean> statusMap = new HashMap<>();
|
||||||
for (ApplicationStatus applicationStatus : applicationStatusList) {
|
for (ApplicationStatus applicationStatus : applicationStatusList) {
|
||||||
@ -135,5 +182,14 @@ public class DBInitializerService {
|
|||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
userSessionRepository.saveAll(userSessionsResetList);
|
userSessionRepository.saveAll(userSessionsResetList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void resetAllUserSessions(Connection connection) throws SQLException
|
||||||
|
{
|
||||||
|
String sql = "update user_sessions set online=0, num_sessions=0";
|
||||||
|
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
||||||
|
|
||||||
|
preparedStatement.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user