I am using the HibernateUtil class outlined in the Hibernate tutorial but it does not appear to be the appropriate tool for what I am doing.
Within a java function that queries the DB, there may be any number of subsequent function calls. The reason for this is that the database consists of a potentially infinite level of parent-child relationships between entities, and information from the children "bubbles up" to the parents.
While the function works, it leaves many DB connections open (which eventually result in a "too many connections" error). How should I be handling connections in this situation?
The offending function:
Code:
public double getActualFromChildren(Task task){
    double toReturn = 0;
    try {   
        //Set up Hibernate Session   
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        //Get all children
        List<Task> tasks = null;       
        tasks = session.createCriteria(Task.class).add(Restrictions.eq("parentID",task.getID())).list();
           
        if(tasks.isEmpty()){
            //If this task has no children (is an end node), return its value
            toReturn = task.getActualHours();
        } else {
            //If this task has children, return the summation of each child's value
            Iterator<Task> taskIterator = tasks.iterator();
            while(taskIterator.hasNext()){
                Task tempTask = taskIterator.next();
                toReturn += getActualFromChildren(tempTask);
            }
        }
    } catch (HibernateException e) {
        e.printStackTrace();
    }
    return toReturn;
}