add hikari pool to avoid db connection timeout
This commit is contained in:
@ -1,9 +1,6 @@
|
|||||||
package fr.tetelie.crawler;
|
package fr.tetelie.crawler;
|
||||||
|
|
||||||
import io.github.cdimascio.dotenv.Dotenv;
|
import io.github.cdimascio.dotenv.Dotenv;
|
||||||
import org.jsoup.Jsoup;
|
|
||||||
import org.jsoup.nodes.Document;
|
|
||||||
import org.jsoup.nodes.Element;
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
@ -12,47 +9,40 @@ import org.springframework.scheduling.annotation.EnableScheduling;
|
|||||||
@EnableScheduling
|
@EnableScheduling
|
||||||
public class Crawler {
|
public class Crawler {
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
// Création des instances
|
// 1. Création des instances (Fidèle à ton code)
|
||||||
new WebScrapper();
|
new WebScrapper();
|
||||||
new DatabaseConfig();
|
new DatabaseConfig();
|
||||||
|
|
||||||
// Connexion à la base de donnée
|
// 2. Connexion initiale
|
||||||
boolean isConnected = DatabaseConfig.getInstance().connect();
|
boolean isConnected = DatabaseConfig.getInstance().connect();
|
||||||
|
if(!isConnected){
|
||||||
|
System.err.println("Impossible de démarrer : BDD injoignable.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Si pas de connexion à la base de donnée on s'arrête ici
|
// 3. Setup Environnement et Spring
|
||||||
if(!isConnected){return;};
|
|
||||||
|
|
||||||
|
|
||||||
Dotenv dotenv = Dotenv.configure().ignoreIfMissing().load();
|
Dotenv dotenv = Dotenv.configure().ignoreIfMissing().load();
|
||||||
dotenv.entries().forEach(entry -> System.setProperty(entry.getKey(), entry.getValue()));
|
dotenv.entries().forEach(entry -> System.setProperty(entry.getKey(), entry.getValue()));
|
||||||
SpringApplication.run(Crawler.class, args);
|
SpringApplication.run(Crawler.class, args);
|
||||||
|
|
||||||
|
// 4. Ta boucle infinie de 24h
|
||||||
|
|
||||||
// On request les prix de tous les produits et on les inject dans la bdd
|
|
||||||
//DatabaseConfig.getInstance().updateAllPrices();
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
System.out.println("Début d'un cycle de mise à jour...");
|
System.out.println("--- Début d'un cycle de mise à jour ---");
|
||||||
// On ajoute les images pour les nouveaux produits qui en ont pas encore
|
|
||||||
|
// Les méthodes récupèrent maintenant une connexion fraîche via le pool
|
||||||
DatabaseConfig.getInstance().updatesAllMissingImages();
|
DatabaseConfig.getInstance().updatesAllMissingImages();
|
||||||
|
|
||||||
|
|
||||||
// On request les prix de tous les produits et on les inject dans la bdd
|
|
||||||
DatabaseConfig.getInstance().updateAllPrices();
|
DatabaseConfig.getInstance().updateAllPrices();
|
||||||
|
|
||||||
System.out.println("Cycle terminé. Sommeil de 24 heures...");
|
System.out.println("Cycle terminé. Sommeil de 24 heures...");
|
||||||
try {
|
try {
|
||||||
// Attend 24 heures avant le prochain scan (en millisecondes)
|
// 24 heures de pause
|
||||||
Thread.sleep(24 * 60 * 60 * 1000);
|
Thread.sleep(24 * 60 * 60 * 1000);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
break;
|
System.err.println("Le programme a été interrompu.");
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,5 +1,7 @@
|
|||||||
package fr.tetelie.crawler;
|
package fr.tetelie.crawler;
|
||||||
|
|
||||||
|
import com.zaxxer.hikari.HikariConfig;
|
||||||
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
import io.github.cdimascio.dotenv.Dotenv;
|
import io.github.cdimascio.dotenv.Dotenv;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
@ -7,45 +9,50 @@ import java.sql.*;
|
|||||||
public class DatabaseConfig {
|
public class DatabaseConfig {
|
||||||
|
|
||||||
static DatabaseConfig instance;
|
static DatabaseConfig instance;
|
||||||
|
private HikariDataSource dataSource;
|
||||||
|
|
||||||
|
// On garde ces variables pour l'affichage/log si tu veux
|
||||||
private String dbUrl;
|
private String dbUrl;
|
||||||
private String dbUser;
|
private String dbUser;
|
||||||
private String dbPass;
|
|
||||||
|
|
||||||
public Connection connection;
|
|
||||||
|
|
||||||
public static DatabaseConfig getInstance() {
|
public static DatabaseConfig getInstance() {
|
||||||
return instance;
|
return instance;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DatabaseConfig() {
|
public DatabaseConfig() {
|
||||||
|
|
||||||
instance = this;
|
instance = this;
|
||||||
// Charge le fichier .env
|
|
||||||
Dotenv dotenv = Dotenv.load();
|
Dotenv dotenv = Dotenv.load();
|
||||||
|
|
||||||
// Récupère les variables
|
|
||||||
dbUrl = dotenv.get("DB_URL");
|
dbUrl = dotenv.get("DB_URL");
|
||||||
dbUser = dotenv.get("DB_USER");
|
dbUser = dotenv.get("DB_USER");
|
||||||
dbPass = dotenv.get("DB_PASS");
|
String dbPass = dotenv.get("DB_PASS");
|
||||||
|
|
||||||
|
// Configuration du Pool (HikariCP est inclus par défaut dans Spring Boot)
|
||||||
|
// Il gère la reconnexion automatique après les longues périodes d'inactivité
|
||||||
|
HikariConfig config = new HikariConfig();
|
||||||
|
config.setJdbcUrl(dbUrl);
|
||||||
|
config.setUsername(dbUser);
|
||||||
|
config.setPassword(dbPass);
|
||||||
|
|
||||||
|
config.setMaximumPoolSize(5);
|
||||||
|
config.setConnectionTimeout(30000);
|
||||||
|
config.setIdleTimeout(600000);
|
||||||
|
config.setMaxLifetime(1800000);
|
||||||
|
// Cette ligne est magique : elle teste la connexion avant de te la donner
|
||||||
|
config.setConnectionTestQuery("SELECT 1");
|
||||||
|
|
||||||
|
this.dataSource = new HikariDataSource(config);
|
||||||
|
|
||||||
System.out.println("Database URL: " + dbUrl);
|
System.out.println("Database URL: " + dbUrl);
|
||||||
System.out.println("Database User: " + dbUser);
|
System.out.println("Database User: " + dbUser);
|
||||||
|
System.out.println("Configuration et Pool chargés avec succès !");
|
||||||
// Maintenant tu peux utiliser dbPass dans ton DriverManager.getConnection()
|
|
||||||
System.out.println("Configuration chargée avec succès !");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean connect() {
|
public boolean connect() {
|
||||||
System.out.println("Tentative de connexion à la base de données...");
|
System.out.println("Vérification de la connectivité base de données...");
|
||||||
|
try (Connection conn = dataSource.getConnection()) {
|
||||||
try {
|
if (conn != null && !conn.isClosed()) {
|
||||||
// On assigne directement à la variable de classe
|
System.out.println("✅ SUCCÈS : La base de données est accessible !");
|
||||||
this.connection = DriverManager.getConnection(dbUrl, dbUser, dbPass);
|
|
||||||
|
|
||||||
if (this.connection != null && !this.connection.isClosed()) {
|
|
||||||
System.out.println("✅ SUCCÈS : Connexion établie avec brio !");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@ -53,43 +60,36 @@ public class DatabaseConfig {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateAllPrices() {
|
public void updateAllPrices() {
|
||||||
String selectQuery = "SELECT id, link FROM products";
|
String selectQuery = "SELECT id, link FROM products";
|
||||||
|
|
||||||
String insertQuery = "INSERT INTO price_history (product_id, price, checked_at) VALUES (?, ?, CURRENT_TIMESTAMP)";
|
String insertQuery = "INSERT INTO price_history (product_id, price, checked_at) VALUES (?, ?, CURRENT_TIMESTAMP)";
|
||||||
|
|
||||||
try (PreparedStatement selectStmt = connection.prepareStatement(selectQuery);
|
// On demande une connexion au pool AU DÉBUT de la méthode
|
||||||
|
// Le try-with-resources la fermera (la rendra au pool) à la fin automatiquement
|
||||||
|
try (Connection connection = dataSource.getConnection();
|
||||||
|
PreparedStatement selectStmt = connection.prepareStatement(selectQuery);
|
||||||
ResultSet rs = selectStmt.executeQuery()) {
|
ResultSet rs = selectStmt.executeQuery()) {
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
int id = rs.getInt("id");
|
int id = rs.getInt("id");
|
||||||
String url = rs.getString("link");
|
String url = rs.getString("link");
|
||||||
|
|
||||||
System.out.println("Analyse du prix pour l'ID : " + id + "...");
|
System.out.println("Analyse du prix pour l'ID : " + id + "...");
|
||||||
|
|
||||||
// 3. Appel de ta fonction de scraping
|
|
||||||
float currentPrice = WebScrapper.getInstance().requestPrice(url);
|
float currentPrice = WebScrapper.getInstance().requestPrice(url);
|
||||||
|
|
||||||
// On vérifie que le prix est valide (pas d'erreur de scraping)
|
|
||||||
if (currentPrice > 0) {
|
if (currentPrice > 0) {
|
||||||
try (PreparedStatement insertStmt = connection.prepareStatement(insertQuery)) {
|
try (PreparedStatement insertStmt = connection.prepareStatement(insertQuery)) {
|
||||||
insertStmt.setInt(1, id);
|
insertStmt.setInt(1, id);
|
||||||
insertStmt.setFloat(2, currentPrice);
|
insertStmt.setFloat(2, currentPrice);
|
||||||
insertStmt.executeUpdate();
|
insertStmt.executeUpdate();
|
||||||
|
|
||||||
System.out.println("✅ Nouveau prix enregistré : " + currentPrice + "€ pour l'ID " + id);
|
System.out.println("✅ Nouveau prix enregistré : " + currentPrice + "€ pour l'ID " + id);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
System.err.println("⚠️ Impossible de récupérer le prix pour l'ID " + id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Un crawler poli attend toujours un peu entre deux requêtes
|
|
||||||
Thread.sleep(2000);
|
Thread.sleep(2000);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (SQLException | InterruptedException e) {
|
} catch (SQLException | InterruptedException e) {
|
||||||
System.err.println("❌ Erreur lors de l'historisation des prix : " + e.getMessage());
|
System.err.println("❌ Erreur lors de l'historisation des prix : " + e.getMessage());
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,15 +97,14 @@ public class DatabaseConfig {
|
|||||||
String selectQuery = "SELECT id, link FROM products WHERE image_url IS NULL OR image_url = ''";
|
String selectQuery = "SELECT id, link FROM products WHERE image_url IS NULL OR image_url = ''";
|
||||||
String updateQuery = "UPDATE products SET image_url = ? WHERE id = ?";
|
String updateQuery = "UPDATE products SET image_url = ? WHERE id = ?";
|
||||||
|
|
||||||
|
try (Connection connection = dataSource.getConnection();
|
||||||
try (PreparedStatement selectStmt = connection.prepareStatement(selectQuery);
|
PreparedStatement selectStmt = connection.prepareStatement(selectQuery);
|
||||||
ResultSet rs = selectStmt.executeQuery()) {
|
ResultSet rs = selectStmt.executeQuery()) {
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
int id = rs.getInt("id");
|
int id = rs.getInt("id");
|
||||||
String urlProduit = rs.getString("link");
|
String urlProduit = rs.getString("link");
|
||||||
|
System.out.println("Traitement image pour l'ID : " + id);
|
||||||
System.out.println("Traitement de l'ID : " + id);
|
|
||||||
|
|
||||||
String imageUrl = WebScrapper.getInstance().requestImage(urlProduit);
|
String imageUrl = WebScrapper.getInstance().requestImage(urlProduit);
|
||||||
|
|
||||||
@ -117,19 +116,10 @@ public class DatabaseConfig {
|
|||||||
System.out.println("Image mise à jour pour ID " + id);
|
System.out.println("Image mise à jour pour ID " + id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Petit délai pour ne pas saturer le serveur cible
|
|
||||||
Thread.sleep(1500);
|
Thread.sleep(1500);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (SQLException | InterruptedException e) {
|
} catch (SQLException | InterruptedException e) {
|
||||||
e.printStackTrace();
|
System.err.println("❌ Erreur lors de la mise à jour des images : " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -3,7 +3,7 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>🧊 Iceberg Price Tracker</title>
|
<title>Iceberg Price Tracker</title>
|
||||||
<script src="https://cdn.tailwindcss.com"></script>
|
<script src="https://cdn.tailwindcss.com"></script>
|
||||||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
Reference in New Issue
Block a user