From 0934d7f4a5ef1db5b27a52fc491ac451787bc5a3 Mon Sep 17 00:00:00 2001 From: costin Date: Fri, 2 Jul 2010 16:17:28 +0300 Subject: [PATCH] + added exception translation + fixed config + added docs on TM management and exception translation --- docs/reference/data.xml | 41 +++++++++++++++---- .../data/gemfire/CacheFactoryBean.java | 20 ++++++++- .../data/gemfire/basic-cache.xml | 6 +-- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/docs/reference/data.xml b/docs/reference/data.xml index dd72cd97..4dba7230 100644 --- a/docs/reference/data.xml +++ b/docs/reference/data.xml @@ -1,17 +1,44 @@ - - Bootstrapping GemFire through the Spring container + + Working with the GemFire APIs + Once the GemFire cache and regions have been configured, these can injected and used inside application objects. This chapter describes the integration with the + Spring transaction management, DaoException hierarchy and wiring of GemFire managed objects. -
- Requirements - - - +
+ Exception translation + + Using a new API requires not just accommodating to the new semantics but also handling its particular exception set. To accommodate this case, Spring Framework provides a + generic, consistent exception hierarchy that + abstracts one from proprietary (and usually checked) exceptions to a set of focused runtime exceptions. As mentioned in the Spring Framework documentation, by using annotations + (@Repository) or AOP, exception translations happens automatically without any code changes. The same holds true for GemFire as long as at least a + CacheFactoryBean is declared. The Cache factory acts as an exception translator which is automatically detected by the + Spring infrastructure and used accordingly. + +
+ +
+ Transaction Management + + One of the most popular features of Spring Framework is transaction + management. If you are not familiar with it, we strongly recommend + looking into it as it offers a consistent programming + model that works transparently across multiple API that can be configured either programmatically or declaratively (the most popular choice). + + For Gemfire, SGI provides a dedicated, per-cache, transaction manager that once declared, allows actions on the Regions to be grouped and executed atomically through + Spring: + + ]]> + + Note that currently GemFire supports optimistic transactions with read committed isolation. Further more to guarantee this isolation, developers should + avoid making in-place changes, that is manually modifying the values present in the cache. To prevent this from happening, the transaction manager configured the cache + to use copy on read semantics, meaning a clone of the actual value is created, each time a read is performed. This behaviour can be disabled if needed through the + copyOnRead property. For more information on the semantics of the underlying GemFire transaction manager, see the GemFire + documentation.
\ No newline at end of file diff --git a/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java b/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java index 751b4300..b24a8a13 100644 --- a/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java @@ -29,8 +29,11 @@ import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.Resource; +import org.springframework.dao.DataAccessException; +import org.springframework.dao.support.PersistenceExceptionTranslator; import org.springframework.util.StringUtils; +import com.gemstone.gemfire.GemFireException; import com.gemstone.gemfire.cache.Cache; import com.gemstone.gemfire.cache.CacheClosedException; import com.gemstone.gemfire.cache.CacheFactory; @@ -40,11 +43,18 @@ import com.gemstone.gemfire.distributed.DistributedSystem; /** * Factory used for configuring a Gemfire Cache manager. Allows either retrieval of an existing, opened cache * or the creation of a new one. + + *

This class implements the {@link org.springframework.dao.support.PersistenceExceptionTranslator} + * interface, as autodetected by Spring's + * {@link org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor}, + * for AOP-based translation of native exceptions to Spring DataAccessExceptions. + * Hence, the presence of this class automatically enables + * a PersistenceExceptionTranslationPostProcessor to translate GemFire exceptions. * * @author Costin Leau */ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanClassLoaderAware, DisposableBean, - InitializingBean, FactoryBean { + InitializingBean, FactoryBean, PersistenceExceptionTranslator { private static final Log log = LogFactory.getLog(CacheFactoryBean.class); @@ -126,6 +136,14 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl factoryLocator.destroy(); } + public DataAccessException translateExceptionIfPossible(RuntimeException ex) { + if (ex instanceof GemFireException) { + return GemfireCacheUtils.convertGemfireAccessException((GemFireException) ex); + } + + return null; + } + public Cache getObject() throws Exception { return cache; } diff --git a/src/test/resources/org/springframework/data/gemfire/basic-cache.xml b/src/test/resources/org/springframework/data/gemfire/basic-cache.xml index 877c6c68..640a2d29 100644 --- a/src/test/resources/org/springframework/data/gemfire/basic-cache.xml +++ b/src/test/resources/org/springframework/data/gemfire/basic-cache.xml @@ -2,10 +2,7 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-lazy-init="true"> @@ -21,7 +18,6 @@ - cache-with-props