Database connection instance

Galley, I have a problem which is as follows, my database connection class is a Singleton. It happens that if the server falls the instance of the class does not know that the server fell, only know when, for example, I try to run a resultset thence gives exception. How can I make the connection class instance know that the server has crashed?

Author: Dener Alencar, 2020-03-10

1 answers

I didn't quite understand your question, but I think I can help you by showing you how I made my connection to my database via JDBC Java.

package com.mypet.MyPet.persistence;

import com.mysql.jdbc.Connection;

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

public class ConectionMySql {

    private static final String URL = "jdbc:mysql://127.0.0.1:3306/MyPet?autoReconnect=true&useSSL=false";
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String LOGIN = "root";
    private static final String PASSWORD = "toor";
    public static Connection connection;

    private ConectionMySql(){}

    public static void openConection(){
        try{
            Class.forName(DRIVER);
            ConectionMySql.connection = (Connection) DriverManager.getConnection(URL, LOGIN, PASSWORD);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

    public static void closeConection(){
        try {
            if(!ConectionMySql.connection.isClosed()){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

This is my class that I make the connection to the MySQL Bank, I have a method of opening and closing the connection to the bank, giving a try catch.

Always when I'm going to make some call in the bank via Java, I open the connection with the method openConection and close with the closeConection, so in case of any error in the connection the application will be aware, therefore, always opens and closes before each call.

public T insert(T object) {
        ConectionMySql.openConection();
        try {
            PreparedStatement preparedStatement = (PreparedStatement) ConectionMySql.connection.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS);
            this.setStatementValuesToInsert(preparedStatement, object);
            preparedStatement.executeUpdate();
            ResultSet resultSet = preparedStatement.getGeneratedKeys();
            if (resultSet.next()){
                object = this.prepareObjectToResponse(resultSet.getLong(1), object);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            ConectionMySql.closeConection();
        }
        return object;
    }

Where you can see the ConectionMySql.openConection(); and finally the ConectionMySql.closeConection();.

I hope I helped in some way, if you want, you can access the above code in my GitHub in the school project MyPet, done the back in Java with Spring and JDBC.

 0
Author: rhian lopes, 2020-03-11 01:02:00