Skip to content

Commit

Permalink
Use scheduled CRON job instead of PostConstruct
Browse files Browse the repository at this point in the history
  • Loading branch information
milanmajchrak committed Nov 21, 2024
1 parent 44c7169 commit ac6051e
Showing 1 changed file with 7 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.apache.logging.log4j.LogManager;
Expand All @@ -36,6 +35,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate5.SessionFactoryUtils;
import org.springframework.scheduling.annotation.Scheduled;

/**
* Hibernate implementation of the DBConnection.
Expand Down Expand Up @@ -108,22 +108,12 @@ protected Transaction getTransaction() {
return sessionFactory.getCurrentSession().getTransaction();
}

// @PostConstruct
// public void startConnectionLogging() {
// // Start a thread to log active connection metrics periodically
// new Thread(() -> {
// while (true) {
// try {
// logHibernateStatistics();
// logDatabaseMetaData();
// Thread.sleep(10000); // Log every 10 seconds
// } catch (InterruptedException e) {
// Thread.currentThread().interrupt();
// break;
// }
// }
// }).start();
// }
// This method will run every 10 seconds
@Scheduled(fixedRate = 10000) // Fixed rate in milliseconds
public void logConnectionMetrics() {
logHibernateStatistics();
logDatabaseMetaData();
}

/**
* Check if Hibernate Session is still "alive" / open. An open Session may or may not have an open Transaction
Expand Down

0 comments on commit ac6051e

Please sign in to comment.