The Kat's Work - Blog
Main | Blog | Registration | Login
Thursday
2024-03-28
2:05 PM
Welcome Guest | RSS
Main » 2009 » November » 11 » Speed Optimising Part 1 – Lazy Loading.
5:20 PM
Speed Optimising Part 1 – Lazy Loading.

I am currently undertaking a process of speed optimising of Spree4.com. This is a process I have been through a few times. I thought I’d detail the steps I go through in the hope it might help someone else. Be warned some of these steps and most of the examples are specific to Hibernate and Java Spring.

 

  1. Lazy loading.

 

If your using a persitance module (eg hibernate) and aren’t using lazy loading, this may explain any slow responses from your code. If you aren’t careful the code you trigger to retrieve one object may retrieve the entire database to memory. The ease of implementing lazy loading is very dependant on the language, module, framework etc you are using. In java struts using hibernate and xml config files, its easy. In Java spring , using hibernate and annotations not so easy.

 

Java Spring, Hibernate and Annotations

 

1.      Place the following in your application-context.xml

 

      <bean id="openSessionInViewInterceptor"

class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"

>

            <property name="sessionFactory">

                  <ref local="sessionFactory"/>

            </property>

            <property name="flushModeName">

                  <value>FLUSH_AUTO</value>

            </property>

      </bean>

 

2.      Add The Interceptor to your interceptor list

 

     <property name="interceptors">

                  <list>

                        <ref bean="openSessionInViewInterceptor" />

                  </list>

            </property>

 

3.      Add FetchType.LAZY to the annotation mappings you need to lazy load.

 

@ManyToMany(cascade = { CascadeType.PERSIST,CascadeType.MERGE },fetch=FetchType.LAZY)

 

4.      In instances you need to load eagerly things you have set to lazy. Use the following

example.

 

      Criteria crit = this.hibernateTemplate.getSessionFactory()

                  .getCurrentSession()

                  .createCriteria(Test.class)

                  .add(Restrictions.eq("id",id))

                  .setFetchMode("subTest", FetchMode.JOIN)

                  .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

     

            return (Indicator)crit.uniqueResult();

 

 

 

 


Views: 977 | Added by: The_Kat | Tags: Lazy Loading, Java Spring, spring, Hibernate | Rating: 0.0/0
Total comments: 0
Name *:
Email *:
Code *:
Login form
Adverts
Search
Calendar
«  November 2009  »
SuMoTuWeThFrSa
1234567
891011121314
15161718192021
22232425262728
2930
Entries archive
Site friends
  • Create your own site
  • Spree4.com
  • My Blog
  • Statistics

    Total online: 1
    Guests: 1
    Users: 0
    spree4
    Copyright MyCorp © 2024
    Website builderuCoz