← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 3263: Made it possible to mark HibernateGenericStores as cacheable. Applied to SystemSettingStore. Been...

 

------------------------------------------------------------
revno: 3263
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2011-04-02 11:48:38 +0200
message:
  Made it possible to mark HibernateGenericStores as cacheable. Applied to SystemSettingStore. Been monitoring db logs and there are now no queries sent to the database during normal navigation.
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateDataElementStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java
  dhis-2/dhis-services/dhis-service-options/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateDataElementStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateDataElementStore.java	2011-02-14 04:01:17 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/hibernate/HibernateDataElementStore.java	2011-04-02 09:48:38 +0000
@@ -127,7 +127,6 @@
     @SuppressWarnings( "unchecked" )
     public Collection<DataElement> searchDataElementByName( String key )
     {
-
         Session session = sessionFactory.getCurrentSession();
 
         Criteria criteria = session.createCriteria( DataElement.class );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java	2010-12-30 09:13:41 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/user/hibernate/HibernateUserStore.java	2011-04-02 09:48:38 +0000
@@ -351,8 +351,8 @@
     }
     
     @SuppressWarnings( "unchecked" )
-    public Collection<UserCredentials> searchUsersByName( String key ){
-        
+    public Collection<UserCredentials> searchUsersByName( String key )
+    {        
         Session session = sessionFactory.getCurrentSession();
 
         Criteria criteria = session.createCriteria( UserCredentials.class );
@@ -432,7 +432,6 @@
         return userRoleStore.getBetweenByName( name, first, max );
     }
 
-    // ===================
     public Collection<UserCredentials> getUsersByOrganisationUnitBetween( OrganisationUnit orgUnit, int first, int max )
     {
         return getBlockUser( toUserCredentials( getUsersByOrganisationUnit( orgUnit ) ), first, max );
@@ -454,10 +453,6 @@
         return findByName( toUserCredentials( getUsersByOrganisationUnit( orgUnit ) ), name ).size();
     }
 
-    // -------------------------------------------------------------------------
-    // 
-    // -------------------------------------------------------------------------
-    
     public Collection<UserCredentials> getUsersWithoutOrganisationUnitBetween( int first, int max )
     {
         return getBlockUser( toUserCredentials( getUsersWithoutOrganisationUnit()), first, max );
@@ -465,7 +460,6 @@
 
     public Collection<UserCredentials> getUsersWithoutOrganisationUnitBetweenByName( String name, int first, int max )
     {
-
         return getBlockUser( findByName( toUserCredentials( getUsersWithoutOrganisationUnit() ), name ), first, max );
     }
 

=== modified file 'dhis-2/dhis-services/dhis-service-options/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-options/src/main/resources/META-INF/dhis/beans.xml	2011-03-31 12:37:13 +0000
+++ dhis-2/dhis-services/dhis-service-options/src/main/resources/META-INF/dhis/beans.xml	2011-04-02 09:48:38 +0000
@@ -26,6 +26,7 @@
     class="org.hisp.dhis.hibernate.HibernateGenericStore">
     <property name="clazz" value="org.hisp.dhis.options.SystemSetting"/>
     <property name="sessionFactory" ref="sessionFactory"/>
+    <property name="cacheable" value="true"/>
   </bean>
 
   <bean id="org.hisp.dhis.options.SystemSettingManager"

=== modified file 'dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java'
--- dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2011-01-06 13:33:29 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2011-04-02 09:48:38 +0000
@@ -47,8 +47,6 @@
 public class HibernateGenericStore<T>
     implements GenericIdentifiableObjectStore<T>
 {
-    //TODO cacheable
-    
     protected SessionFactory sessionFactory;
 
     public void setSessionFactory( SessionFactory sessionFactory )
@@ -74,6 +72,24 @@
         this.clazz = clazz;
     }
     
+    private boolean cacheable = false;
+
+    /**
+     * Could be overridden programmatically.
+     */
+    protected boolean isCacheable()
+    {
+        return cacheable;
+    }
+
+    /**
+     * Could be injected through container.
+     */
+    public void setCacheable( boolean cacheable )
+    {
+        this.cacheable = cacheable;
+    }
+    
     // -------------------------------------------------------------------------
     // Convenience methods
     // -------------------------------------------------------------------------
@@ -86,7 +102,9 @@
      */
     protected final Query getQuery( String hql )
     {
-        return sessionFactory.getCurrentSession().createQuery( hql );
+        Query query =  sessionFactory.getCurrentSession().createQuery( hql );
+        query.setCacheable( cacheable );
+        return query;
     }
     
     /**
@@ -97,7 +115,9 @@
      */
     protected final SQLQuery getSqlQuery( String sql )
     {
-        return sessionFactory.getCurrentSession().createSQLQuery( sql );
+        SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery( sql );
+        query.setCacheable( cacheable );
+        return query;
     }
     
     /**
@@ -107,7 +127,9 @@
      */
     protected final Criteria getCriteria()
     {
-        return sessionFactory.getCurrentSession().createCriteria( getClazz() );
+        Criteria criteria = sessionFactory.getCurrentSession().createCriteria( getClazz() );
+        criteria.setCacheable( cacheable );
+        return criteria;
     }
     
     /**
@@ -119,7 +141,7 @@
      */
     protected final Criteria getCriteria( Criterion... expressions )
     {
-        Criteria criteria = sessionFactory.getCurrentSession().createCriteria( getClazz() );
+        Criteria criteria = getCriteria();
         
         for ( Criterion expression : expressions )
         {