JavaAssist and CGLIB Hibernate and Spring

I recently came across some legacy code that brought to mind some interesting aspects of Spring and Hibernate. This involves the code generation libaray CGLIB.

Javassist is a class library for editing bytecodes in Java; it enables Java programs to define a new class at runtime and to modify a class file when the JVM loads it.

Cglib It is used to extend JAVA classes and implements interfaces at runtime.

Hibernate v 3.2 and below utilize CGLIB to generate proxies for the database connections. Hibernate v3.3 and above use Javassist by default.

The way CGLIB (Older version 2.1.1 this should be fixed in new versions) generates proxies causes the memory to contain a lot of used but referenced objects hence increasing the size of the heap. So much so that the heap will grow until we get a Out Of Memory Error. This happens just before a proxy is instantiated when using the CGLIBLazyInitializer.getProxy().  

Please refer to http://opensource.atlassian.com/projects/hibernate/browse/HHH-2481 for more information. First and formost Spring managed Hibernate sessions and transactions can be very powerful and have advantages such as cleaning up of sessions.  However combining Spring with Hibernate v3.2 and below leads to memory mismanagement due to the CGLIB which has some internal issues.

If you are however stuck to modifying or maintaining applications that are utilizing CGLIB you could add the following code snipplet to improve performance and fix the above issue:

cfg.setListeners("load", new LoadEventListener [] {new DefaultLoadEventListener(), new LoadEventListener() {

            public void onLoad(LoadEvent event, LoadType loadType) throws HibernateException {

                Object obj = event.getResult();

                if (obj instanceof HibernateProxy) {

                    Enhancer.registerCallbacks(obj.getClass(),null);

                }

            }

        }});

 

The cfg variable is the configuration variable of hibernate.

In addition when upgrading to Hibernate v3.5 be aware that it implements strick checks on whether SQL queries are well formed.

Having implemented Hibernate v3.5 some of my memory issues were gone and the application was left in a stable state.

 

Advertisements

About gjcbell

I'm a software designer and integration specialist in Cape Town, South Africa. I work for SPF at SGH. I develop applications in Java, Cplusplus and Python and JavaScript. I design websites and web applications.
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s