I am working on a school project in java and mssql where i have some trouble with the database connection.
Is necessary to close the database connection in a situation like this?
I want to build an Animal object which consist of a Person object.
this is the code from DBLayer/DBAnimal:
First we have the method where i connect to the database and get the resultset.
public Animal findAnimal(int id) 
    {
        con = DBConnection.getInstance().getDBcon();
        PreparedStatement preStmnt = null;
        String query = "select * from animal where id = ?";
        Animal animalObj = null;
        ResultSet results;
        try 
        {
            preStmnt = con.prepareStatement(query);
            preStmnt.setInt(1, id);
            results = preStmnt.executeQuery();
            if (results.next()) 
            {
                animalObj = buildAnimal(results);
            }
        } catch (SQLException SQLe) {
            System.out.println("SQL Exception i findAnimal: " + SQLe);
        } catch (Exception e) {
            System.out.println("Exception i findAnimal() " + e.getMessage());
        }
        DBConnection.closeConnection(); 
        return animalObj;
    }
then where i build the Animal object and call the FindPersonOnId method:
public Animal buildAnimal(ResultSet results) throws SQLException 
    {
        Animal animalObj = new Animal();
        Person personObj = new Person();
        try 
        {
            animalObj.setId(results.getInt("id"));
            animalObj.setName(results.getString("name"));           
            animalObj.setRace(results.getInt("raceid"));
            animalObj.setSpecie(findSpecieId(results.getInt("raceid")));
            animalObj.setSex(results.getString("sex").charAt(0));
            animalObj.setBorn(results.getDate("born"));
            animalObj.setAlive(results.getBoolean("alive"));
            animalObj.setPerson(dbPerson.findPersonOnId(results.getInt("personId")));
        }
        catch (SQLException e) 
        {
            System.out.println("Exception i buildAnimal" + e.getMessage());
        }
        return animalObj;
    }
This is from the DBLayer/DBPerson:
public Person findPersonOnId(int id) 
    {
        con = DBConnection.getInstance().getDBcon();
        PreparedStatement preStmnt = null;
        String query = "SELECT * FROM " + TABLE_NAME + " WHERE id = ?";
        Person personObj = null;
        ResultSet results;
        try 
        {
            preStmnt = con.prepareStatement(query);
            preStmnt.setInt(1, id);
            results = preStmnt.executeQuery();
            if (results.next()) 
            {
                personObj = buildPerson(results);
            }
        } 
        catch(SQLException SQLe) 
        {
            System.out.println("SQLException i findPersonOnId(id): " + SQLe);
        } 
        catch (Exception e)
        {
            System.out.println("Fejl i findPersonOnId(id): " + e.getMessage());
        }
        DBConnection.getInstance().closeConnection();
        return personObj;
    }
as you can see i am using the closeConnection both times and this is causing some problems since it is very slow. And i was wandering wheater it is necessary to close the connection at all in this situation.
If it isn't, in what situation would it be?
 
    