package dev.peerat.backend.repository;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import dev.peerat.backend.Configuration;

public class ConnectionManager {
	
	private Connection con;
	private Configuration config;
	
	public ConnectionManager(Configuration config){
		this.config = config;
	}

	public Connection ensureConnection() throws SQLException {
		if (con == null || (!con.isValid(5))) {
			this.con = DriverManager.getConnection(
					"jdbc:mysql://" + config.getDbHost() + ":" + config.getDbPort() + "/" + config.getDbDatabase() + "",
					config.getDbUser(), config.getDbPassword());
		}
		return this.con;
	}
}
