Archive for the 'Hibernate' Category

Use Hibernate Paging when processing lots of objects

At work right now I have been trying to solve an OutOfMemoryException in a Java web application. The app exposes a bunch of web services. All of which can end up processing a large collection of objects returned via Hibernate. All of which can also keep a Hibernate session opened for a very long time.

So far, the solution we have found is to use paging along with releasing the session before doing another Hibernate query. We do the query, get back a collection of objects and iterate over that collection, processing each one in turn. Then, when we are done, we close that session, open a new one and get another collection of objects.

Hibernate makes this very easy to do:

public Collection find(int page, String hsql) {
    getSession.close();
    getSession();
    int page_size = 25;
    query=session.createQuery(hsql);
    return query.setFirstResult(page * page_size)
            .setMaxResults(page_size).list();
}

I have not supplied implementations for getSession since this depends on how you are working with Hibernate (i.e., Spring or HibernateUtil class).

Lazy Loading and stupid io mistakes

Well, the filter/interceptor solution that I found for solving the “lazy loading problem in TeamDocs”:http://www.fuzzylizard.com/archives/2006/07/29/753/ didn’t work, so I had to remove all lazy loading from the app. This took a little bit of work to get all the lazy="false" statements put in the right spots. However, it now works. This is probably not the best solution, but it is good enough for now. Once I pick a web framework I will have to revisit this.

On the other hand, The entire object graph is connected so I am not sure what the best solution is. It may simply be to load all the objects on startup and cache them in memory somewhere. Then when objects are needed, they are simply retrieved from the cache and only persisted via Hibernate when they change or objects are added. I am not sure.

As for my stupid mistake, I was trying to create directories on the file system and nothing was working. I kept getting a false result when calling dir.mkdir(). Moral of this story, always make sure you call the mkdirs() to ensure that all parent directories are created as well. I gotta read the Javadocs more often instead of just picking the first method and going with it.

As far as TeamDocs is concerned, I can once again log in, see the explorer view (the view that shows a level of directories and documents) and create a new directory. Cool, now on to uploading documents.

BTW, for anyone for whom the previous paragraphs did not make sense, TeamDocs is a document management application that I am creating. More information can be found at the “project website”:http://teamdocs.fuzzylizard.com, which is woefully un-updated and slightly out of date.

Lazy loading, Hibernate, and web apps

One problem that I have been having developing TeamDocs is in the area of dealing with Lazy Loading and Hibernate. Specifically, when I load some objects using a DAO and then send those objects to the View to be loaded I kept getting an exception: LazyInitializationException: Session has been closed. This has been driving me nuts. I finally “found the solution”:http://www.hibernate.org/43.html in a post on the “Hibernate webiste”:http://www.hibernate.org.

bq. A first solution would be to open another unit of work for rendering the view. This can easily be done but is usually not the right approach. Rendering the view for a completed action is supposed to be inside the first unit of work, not a separate one. The solution, in two-tiered systems, with the action execution, data access through the Session, and the rendering of the view all in the same virtual machine, is to keep the Session open until the view has been rendered.

Unfortunately, I don’t fully understand the solution yet, but I do know it works.

The above article on the Hibernate website goes along with this one on “Sessions and Transactions”:http://www.hibernate.org/42.html which describes concepts like “Units of Work”, “Transactions”, “Transaction Demarcation with JDBC”, and other topics. You should probably read this article before the one listed above.

Real-World Experiences With Hibernate – Shine Technologies

Real-World Experiences With Hibernate, from Shine Technologies, is an interesting article detailing one company’s experience using Hibernate and some of the problems and benefits they encountered.