001
014
015 package com.liferay.portal.transaction;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.spring.aop.Skip;
020 import com.liferay.portal.kernel.util.GetterUtil;
021 import com.liferay.portal.kernel.util.PropsKeys;
022 import com.liferay.portal.kernel.util.ReflectionUtil;
023 import com.liferay.portal.spring.aop.ServiceBeanAopCacheManager;
024 import com.liferay.portal.spring.aop.ServiceBeanAopCacheManagerUtil;
025 import com.liferay.portal.util.PropsUtil;
026 import com.liferay.portal.util.PropsValues;
027
028 import java.lang.annotation.Annotation;
029 import java.lang.reflect.Field;
030
031 import java.util.HashMap;
032 import java.util.concurrent.ConcurrentHashMap;
033
034 import org.aopalliance.intercept.MethodInvocation;
035
036
039 public class TransactionsUtil {
040
041 public static void disableTransactions() {
042 if (_log.isDebugEnabled()) {
043 _log.debug("Disable transactions");
044 }
045
046 PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED = false;
047
048 try {
049 Field field = ReflectionUtil.getDeclaredField(
050 ServiceBeanAopCacheManager.class, "_annotations");
051
052 field.set(
053 null,
054 new HashMap<MethodInvocation, Annotation[]>() {
055
056 @Override
057 public Annotation[] get(Object key) {
058 return _annotations;
059 }
060
061 private Annotation[] _annotations = new Annotation[] {
062 new Skip() {
063
064 @Override
065 public Class<? extends Annotation>
066 annotationType() {
067
068 return Skip.class;
069 }
070
071 }
072 };
073
074 });
075 }
076 catch (Exception e) {
077 throw new RuntimeException(
078 "Unexpected error disabling transactions", e);
079 }
080 }
081
082 public static void enableTransactions() {
083 if (_log.isDebugEnabled()) {
084 _log.debug("Enable transactions");
085 }
086
087 PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED = GetterUtil.getBoolean(
088 PropsUtil.get(PropsKeys.SPRING_HIBERNATE_SESSION_DELEGATED));
089
090 try {
091 Field field = ReflectionUtil.getDeclaredField(
092 ServiceBeanAopCacheManager.class, "_annotations");
093
094 field.set(
095 null, new ConcurrentHashMap<MethodInvocation, Annotation[]>());
096
097 ServiceBeanAopCacheManagerUtil.reset();
098 }
099 catch (Exception e) {
100 throw new RuntimeException(
101 "Unexpected error disabling transactions", e);
102 }
103 }
104
105 private static final Log _log = LogFactoryUtil.getLog(
106 TransactionsUtil.class);
107
108 }