From b71cd3e2f999d200ea02c17acfd24b1cded78ba2 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 14 Apr 2014 11:19:12 +0200 Subject: [PATCH] DATAJPA-507 - LocakModeRepositoryPostProcessor now caches lock metadata. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now cache the LockModeType annotation found for a method to avoid repeated lookups of the annotation using AnnotationUtils.findAnnotation(…) as the call to it is quite expensive (as it needs to travers the entire method hierarchy and inspect meta-annotations etc.). This is a partial back-port of the fix for DATAJPA-173 with only the performance improvement, leaving the functional improvements in the master branch. --- .../LockModeRepositoryPostProcessor.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/support/LockModeRepositoryPostProcessor.java b/src/main/java/org/springframework/data/jpa/repository/support/LockModeRepositoryPostProcessor.java index 8b2cdff40..5557dab23 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/LockModeRepositoryPostProcessor.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/LockModeRepositoryPostProcessor.java @@ -16,6 +16,8 @@ package org.springframework.data.jpa.repository.support; import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; import javax.persistence.LockModeType; @@ -71,6 +73,8 @@ public enum LockModeRepositoryPostProcessor implements RepositoryProxyPostProces INSTANCE; + private final Map lockModeTypeCache = new HashMap(); + /* * (non-Javadoc) * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation) @@ -84,9 +88,18 @@ public enum LockModeRepositoryPostProcessor implements RepositoryProxyPostProces return invocation.proceed(); } - Lock annotation = AnnotationUtils.findAnnotation(method, Lock.class); - LockModeType lockMode = (LockModeType) AnnotationUtils.getValue(annotation); - TransactionSynchronizationManager.bindResource(method, lockMode == null ? NULL : lockMode); + Object lockModeType = lockModeTypeCache.get(method); + + if (lockModeType == null) { + + Lock annotation = AnnotationUtils.findAnnotation(method, Lock.class); + lockModeType = AnnotationUtils.getValue(annotation); + lockModeType = lockModeType == null ? NULL : lockModeType; + + lockModeTypeCache.put(method, lockModeType); + } + + TransactionSynchronizationManager.bindResource(method, lockModeType); try { return invocation.proceed();