-->
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: Hibernate session closing/best approach to run query
PostPosted: Thu Jan 04, 2007 3:54 pm 
Newbie

Joined: Thu Jan 04, 2007 3:48 pm
Posts: 5
Here is a general desc of my app:

I am running through the database of an LMS with hibernate and deleting records such as test and activity history, saved tests, users, and etc. At the present time I am just deleting the records the show a users progress on an activity.

I have zipped up the app portion of my our Java web app and included it in my classpath. I am creating POJO's by making calls to the app. This part is working fine.

My problem is that if i run my class on a database that has 500,000+ records in it my session will get closed and then my app crashes. I have added some lines to "re-open" if the session is closed but the issue still occurs at the line "contRecArr = contRecs.toArray();" of the code

By looking at my code is their something obvious that I am missing? Can you think of a better way that this class could be constructed?

Another thing i have noticed is that I can watch the logging of the program and see that records should be deleted but it does not seem like they are actually being removed from the DB until the whole class is finished. What is the best way to get around this so each transaction is processed as it happens?

Here is a link to my source code.
http://sourcepost.sytes.net/sourcepost/sourceview.aspx?source_id=29061

Thank you much for your help[/list]

_________________
2007-01-04 12:00:24,327 WARN com.yoofoo.GetSmartUser - Could not find any intelligent end-users


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 06, 2007 12:37 am 
Newbie

Joined: Thu Jan 04, 2007 3:48 pm
Posts: 5
I figured i may get mroe replies by posting the code. Out of 243 views i hoped someone would at least say something.

Code:
public class StudentAssignmentRemover {
   
   
    //GLOBALS
    private static Logger logger = Logger.getLogger(StudentAssignmentRemover.class);
    protected Session session;
    protected Transaction tx;
    protected User user;
    protected Relation relation;
    protected ContentRecord contRec;
    protected AssignmentRecord assignRec;
    protected Group group;
    protected Assignment assignment;
    protected Location location;
    protected Permission permission;
   
    /** Creates a new instance of StudentAssignmentRemover */
    public StudentAssignmentRemover() {
    }
   
    /** Deletes assignment history records from LMS_CONTENT_RECORD. This method calls
     * removeRelations(Set "User Object".getRelations()) and deleteContentRecords(Set "User Object".getAssignmentsAssignedTo()) */
    public void deleteAssignments()   {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        tx = session.beginTransaction();
        Query query;
        List users;
       
        query = session.getNamedQuery("findAllNonSystemGroups");        // get all non-system groups
        Object[] groups = query.list().toArray();                       //throw them into an array of Object[]
        int numGroupsFound = groups.length;                             //num of groups found
       
       for(int g = 0; g < numGroupsFound;g++)   {                       //loop through each group
            group = (Group) groups[g];                                  //cast to a Group obj from the group array
            logger.debug("Getting users of group: " + group.getName());
            //If the session was closed by a commit(), reopen it
            if(! session.isOpen())
            {
                logger.warn("SessionFactory was closed, no worries, Re-opening sessionFactory for StudentAssignmentRemover");
                session = HibernateUtil.getSessionFactory().getCurrentSession();
                tx = session.beginTransaction();
            }
// bypassing UserDAO and calling the query directly. Doing this because Group.getUsers() returned DAOFactory exception (null)
            query = session.getNamedQuery("findUsersInGroup");         
            query.setParameter(0,group);
            users = query.list();                                       //query for the users of this group
            logger.debug("Working on group: " + group.getName());
           
            if(! users.isEmpty())   {                        //if no users in group do nothing.
                int index = 0;
                Object[] relObj;
                logger.info("Query Returned Results.. ");
                int resultSize = users.size();
                int userCounter = 0;
                logger.debug("There is/are " + resultSize + " users for the above listed class");

                while(userCounter < resultSize)   {                //for each user, remove each content_record for each assignment
                    user = (User) users.get(userCounter);
                    logger.info("Deleting LMS assignment data of user: " + user.getUsername() + " for the current group");
                    //Delete relations
                    logger.debug("Removing any entries for user in LMS_RELATION");
                    deleteRelations(user.getRelations());
                    //delete content_records
                    logger.debug("Removing assignment history for the user in the current group...");
                    deleteContentRecords(user.getAssignmentsAssignedTo());
                    userCounter++;               
                }

            tx.commit();
            }

            else    {
            logger.info("No records that match query");     
            } 
           
       }
       
        if(session.isOpen())    {
            session.close();
        }
   }    // end
   
   
   protected void deleteRelations(Set relationSet)    {
       Set relations = relationSet;
       Object[] relObj;
       
       for(int i = 0; i < relations.size(); i++)   {
            relObj = relations.toArray();
            relation = (Relation) relObj[i];
            logger.info("Deleting the relation: " + relation.getRelationshipType().toString());
            session.delete(relObj[i]);
            tx.commit();
        }
   }    // end listRelations(Set relationSet)
   
   
   protected void deleteContentRecords(Set assignedTo)  {

        Object[] assgnToArr = assignedTo.toArray();
        Set contRecs;
       
        for(int i = 0; i < assignedTo.size(); i++)   {
            if(! session.isOpen())
            {
                logger.warn("SessionFactory was closed, no worries, Re-opening sessionFactory for StudentAssignmentRemover");
                session = HibernateUtil.getSessionFactory().getCurrentSession();
                tx = session.beginTransaction();
            }
            assignment = (Assignment) assgnToArr[i];
            logger.info("Deleting assignment history for: " + assignment.getName() + ", PK_ASSIGNMENT_ID: " + assignment.getId());
            assignRec = assignment.getAssignmentRecord();   //LMS_CONTENT_RECORD
           
            contRecs = assignRec.getContentRecords();       //LMS_CONTENT_RECORD
            Object[] contRecArr = contRecs.toArray();
            int arrSize = contRecs.size();
            logger.debug("For the current user there is/are " + contRecs.size() + " content record(s) for the assignment: " + assignRec.getAssignment().getName());
           
                for(int r = 0; r < arrSize; r++)    {
                    contRec = (ContentRecord) contRecArr[r];
                    logger.info("Deleting content Record " + contRec.getId());
                    assignRec.removeActivityRecord(contRec);
                    tx.commit();
                        if(! session.isOpen())
                        {
                            logger.warn("Re-opening sessionFactory for StudentAssignmentRemover");
                            session = HibernateUtil.getSessionFactory().getCurrentSession();
                            tx = session.beginTransaction();
                        }
                }
        }
   }        // end deleteContentRecords()
   
   
}       // End Class

_________________
2007-01-04 12:00:24,327 WARN com.yoofoo.GetSmartUser - Could not find any intelligent end-users


Last edited by devmaster on Mon Mar 31, 2008 3:41 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 06, 2007 10:04 am 
Beginner
Beginner

Joined: Sat Dec 16, 2006 9:53 pm
Posts: 31
Location: Brisbane, Australia
WARN com.guru.GetSmartProgrammer - Could not find any intelligent Programmers


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 11:44 pm 
Newbie

Joined: Thu Jan 04, 2007 3:48 pm
Posts: 5
That's it? wow. looks like you have alot to say.

_________________
2007-01-04 12:00:24,327 WARN com.yoofoo.GetSmartUser - Could not find any intelligent end-users


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.