These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Session Management with Recursive Java Functions
PostPosted: Wed Oct 05, 2011 9:29 am 
Newbie

Joined: Fri Aug 26, 2011 5:40 pm
Posts: 6
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;
}


Top
 Profile  
 
 Post subject: Re: Session Management with Recursive Java Functions
PostPosted: Thu Oct 13, 2011 6:08 pm 
Newbie

Joined: Fri Aug 26, 2011 5:40 pm
Posts: 6
I guess hibernate can't do recursion.


Top
 Profile  
 
 Post subject: Re: Session Management with Recursive Java Functions
PostPosted: Fri Oct 14, 2011 1:53 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
You open a new connection at each level and start a transaction but you never do commit/rollback and you don't close the session. If your first level has only one entity but 100 child-entities you end up with 101 connections before the outer-most transaction can be commited or rollback.

You could open the session and start the transaction outside the method and pass it in as a parameter. So the whole thing uses one connection wirh one transaction.


Top
 Profile  
 
 Post subject: Re: Session Management with Recursive Java Functions
PostPosted: Fri Oct 14, 2011 9:05 am 
Newbie

Joined: Fri Aug 26, 2011 5:40 pm
Posts: 6
Thanks for the advice. Will follow and post results.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.