Enhance `DatabaseManager` with logging, validation, and cleanup

- Added detailed logging for database initialization and connection handling.
- Implemented configuration validation and sanity checks during initialization.
- Introduced `close()` method to properly release database resources.
- Updated `MediaManagerApplication` to invoke `DatabaseManager.close()` on shutdown.
- Updated `.gitignore` to exclude sensitive configuration files while allowing example files.
This commit is contained in:
Gustavo Henrique Santos Souza de Miranda 2025-11-13 04:43:05 -03:00
parent 22476abb27
commit 3bb0423167
4 changed files with 79 additions and 2 deletions

8
.gitignore vendored
View File

@ -44,3 +44,11 @@ config-local.properties
pipes/
*.log
/.idea/
Configuration files with sensitive credentials
# These files contain database passwords and API keys and should never be committed
src/main/resources/config.properties
src/main/resources/application.properties
# Allow example configuration files to be committed
!src/main/resources/*.properties.example

View File

@ -33,7 +33,9 @@ public class MediaManagerApplication {
// Keep application running
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
logger.info("Shutting down MediaManager Core...");
// TODO: Cleanup resources
if (databaseManager != null) {
databaseManager.close();
}
}));
} catch (Exception e) {

View File

@ -1,18 +1,28 @@
package com.mediamanager.service.database;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DatabaseManager {
private final Properties configuration;
private Connection connection;
private static final Logger logger = LogManager.getLogger(DatabaseManager.class);
public DatabaseManager(Properties config) {
this.configuration = config;
logger.debug("DatabaseManager created with configuration:");
}
public void init() throws Exception {
logger.info("Initializing database connection...");
validateConfiguration();
String databaseType = configuration.getProperty("database.type");
String databaseUrl = configuration.getProperty("database.url");
String databaseUsername = configuration.getProperty("database.username");
@ -22,11 +32,67 @@ public class DatabaseManager {
String connectionString = String.format("jdbc:postgresql://%s:%s/%s", databaseUrl, databasePort, databaseName);
logger.debug("Attempting to connect to: {}", connectionString);
try {
connection = DriverManager.getConnection(connectionString, databaseUsername, databasePassword);
logger.info("Database connection established successfully");
performSanityChecks();
logger.info("Database sanity checks passed successfully");
} catch (SQLException e) {
logger.error("Failed to connect to database", e);
throw new Exception("Database connection failed: " + e.getMessage(), e);
}
}
private void performSanityChecks() throws SQLException {
logger.debug("Performing sanity checks...");
try (Statement stmt = connection.createStatement()) {
stmt.execute("SELECT 1");
}
String databaseProductName = connection.getMetaData().getDatabaseProductName();
String databaseProductVersion = connection.getMetaData().getDatabaseProductVersion();
logger.info("Connected to database: {} v{}", databaseProductName, databaseProductVersion);
}
private void validateConfiguration() throws Exception {
String[] requiredProperties = {
"database.url",
"database.username",
"database.password",
"database.port",
"database.name"
};
for (String property : requiredProperties) {
if (configuration.getProperty(property) == null ||
configuration.getProperty(property).trim().isEmpty()) {
throw new Exception("Required database configuration missing: " + property);
}
}
logger.debug("Database configuration validated successfully");
}
public Connection getConnection() {
return connection;
}
public void close() {
if (connection != null) {
try {
connection.close();
logger.info("Database connection closed successfully");
} catch (SQLException e) {
logger.error("Failed to close database connection", e);
}
}
}
}

View File

@ -21,3 +21,4 @@ hibernate.format_sql=true
ipc.pipe.name=mediamanager-pipe
ipc.pipe.path=/tmp/mediamanager
ipc.buffer.size=8192