← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 22301: SystemSettingManager. Declare transactions on individual methods. The get-methods do not have tra...

 

------------------------------------------------------------
revno: 22301
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2016-03-14 16:55:25 +0100
message:
  SystemSettingManager. Declare transactions on individual methods. The get-methods do not have transactions declared, instead a programmatic transaction is initiated on cache miss in order to reduce the number of transactions to improve performance.
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/setting/DefaultSystemSettingManager.java
  dhis-2/dhis-support/dhis-support-hibernate/src/main/resources/META-INF/dhis/beans.xml


--
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/setting/DefaultSystemSettingManager.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/setting/DefaultSystemSettingManager.java	2016-03-07 21:51:08 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/setting/DefaultSystemSettingManager.java	2016-03-14 15:55:25 +0000
@@ -35,7 +35,11 @@
 import org.hisp.dhis.system.util.SystemUtils;
 import org.hisp.dhis.system.util.ValidationUtils;
 import org.jasypt.encryption.pbe.PBEStringEncryptor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.TransactionStatus;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.support.TransactionCallback;
+import org.springframework.transaction.support.TransactionTemplate;
 
 import javax.annotation.Resource;
 import java.io.Serializable;
@@ -45,10 +49,13 @@
 import java.util.stream.Collectors;
 
 /**
+ * Declare transactions on individual methods. The get-methods do not have
+ * transactions declared, instead a programmatic transaction is initiated on
+ * cache miss in order to reduce the number of transactions to improve performance.
+ * 
  * @author Stian Strandli
  * @author Lars Helge Overland
  */
-@Transactional
 public class DefaultSystemSettingManager
     implements SystemSettingManager
 {
@@ -81,6 +88,9 @@
     {
         this.flags = flags;
     }
+    
+    @Autowired
+    private TransactionTemplate transactionTemplate;
 
     @Resource( name = "stringEncryptor" )
     private PBEStringEncryptor pbeStringEncryptor;
@@ -90,6 +100,7 @@
     // -------------------------------------------------------------------------
 
     @Override
+    @Transactional
     public void saveSystemSetting( String name, Serializable value )
     {
         SETTING_CACHE.invalidate( name );
@@ -119,12 +130,14 @@
     }
 
     @Override
+    @Transactional
     public void saveSystemSetting( SettingKey setting, Serializable value )
     {
         saveSystemSetting( setting.getName(), value );
     }
 
     @Override
+    @Transactional
     public void deleteSystemSetting( String name )
     {
         SystemSetting setting = systemSettingStore.getByName( name );
@@ -138,12 +151,14 @@
     }
 
     @Override
+    @Transactional
     public void deleteSystemSetting( SettingKey setting )
     {
         deleteSystemSetting( setting.getName() );
     }
 
     @Override
+    @Transactional
     public Serializable getSystemSetting( String name )
     {
         SystemSetting setting = systemSettingStore.getByName( name );
@@ -156,6 +171,10 @@
         return setting != null && setting.hasValue() ? setting.getValue() : null;
     }
 
+    /**
+     * No transaction for this method, transaction is initiated in 
+     * {@link getSystemSettingOptional} on cache miss.
+     */
     @Override
     public Serializable getSystemSetting( SettingKey setting )
     {
@@ -172,16 +191,34 @@
         }
     }
 
+    /**
+     * No transaction for this method, transaction is initiated in 
+     * {@link getSystemSettingOptional}.
+     */
     @Override
     public Serializable getSystemSetting( SettingKey setting, Serializable defaultValue )
     {
         return getSystemSettingOptional( setting.getName(), defaultValue ).orElse( null );
     }
 
+    /**
+     * Get system setting. The database call is executed in a programmatic 
+     * transaction.
+     * 
+     * @param name the system setting name.
+     * @param defaultValue the default value for the system setting.
+     * @return an optional system setting value.
+     */
     private Optional<Serializable> getSystemSettingOptional( String name, Serializable defaultValue )
-    {        
-        SystemSetting setting = systemSettingStore.getByName( name );
-
+    {
+        SystemSetting setting = transactionTemplate.execute( new TransactionCallback<SystemSetting>()
+        {
+            public SystemSetting doInTransaction( TransactionStatus status) 
+            {
+                return systemSettingStore.getByName( name );
+            }
+        } );
+        
         if ( setting != null && setting.hasValue() )
         {
             return isConfidential( name ) ?
@@ -192,10 +229,10 @@
         {
             return Optional.ofNullable( defaultValue );
         }
-
     }
 
     @Override
+    @Transactional
     public List<SystemSetting> getAllSystemSettings()
     {
         return systemSettingStore.getAll().stream()
@@ -204,6 +241,7 @@
     }
 
     @Override
+    @Transactional
     public Map<String, Serializable> getSystemSettingsAsMap()
     {
         Map<String, Serializable> settingsMap = new HashMap<>();
@@ -231,6 +269,7 @@
     }
 
     @Override
+    @Transactional
     public Map<String, Serializable> getSystemSettingsAsMap( Set<String> names )
     {
         Map<String, Serializable> map = new HashMap<>();
@@ -259,6 +298,7 @@
     }
 
     @Override
+    @Transactional
     public Map<String, Serializable> getSystemSettings( Collection<SettingKey> settings )
     {
         Map<String, Serializable> map = new HashMap<>();

=== modified file 'dhis-2/dhis-support/dhis-support-hibernate/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-support/dhis-support-hibernate/src/main/resources/META-INF/dhis/beans.xml	2016-01-07 09:08:16 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/resources/META-INF/dhis/beans.xml	2016-03-14 15:55:25 +0000
@@ -14,6 +14,10 @@
     <property name="dataSource" ref="dataSource" />
   </bean>
 
+  <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
+    <property name="transactionManager" ref="transactionManager" />
+  </bean>
+
   <!-- Configuration -->
 
   <bean id="cacheManager" class="org.hisp.dhis.cache.DefaultHibernateCacheManager">