Spring @Transactional propagation is not working

时光怂恿深爱的人放手 提交于 2019-12-12 02:24:38

问题


I have a very simple code comprising of Service -> RequestProcessor -> DAO having 2-3 classes (interface, abstract, concrete) in each layer.

Service layer:-

public interface Service {
   public void saveOrUpdate(Object entity, String operationName);
 }
}

public abstract class AbstractService implements Service{

   public abstract ReqProcessor getRP();
   @Override
   public void saveOrUpdate(Object entity, String operationName) {
     ReqProcessor hiberTestRP = getRP();
     hiberTestRP.saveOrUpdate(entity, operationName);
 }
}

@Component
public class ServiceImpl extends AbstractService {

  @Autowired
  public ReqProcessor hibertestRPImpl;

  @Override
  public HiberTestRP getRP() {
    return hibertestRPImpl;
 }
}

ReqProcessor layer:-

public interface ReqProcessor {
   public void saveOrUpdate(Object entity, String operationName);
   public void saveObject();
 }
}

public abstract class AbstractReqProcessor  implements ReqProcessor {
@Override
public void saveOrUpdate(Object entity, String operationName) {
    saveObject();
 }
}

@Component
public class ReqProcessorImpl extends AbstractReqProcessor {
 @Autowired
 public CustomHibernateDao customWSDaoImpl;

 @Override
 @Transactional(value="transactionManagerWS", propagation=Propagation.REQUIRED)
 public void saveObject() {
  // object created //
  customWSDaoImpl.saveOrUpdate(object);  // exception is thrown at this line
 }
}

DAO layer:-

public interface CustomHibernateDao {
  public void saveOrUpdate(Object entity, String operationName);
}

@Repository
@Transactional(value="transactionManagerWS", propagation=Propagation.MANDATORY)
public class CustomWSDaoImpl implements CustomHibernateDao {

 @Autowired
 public SessionFactory sessionFactoryWS;

 protected Session getCurrentSession() {
    return sessionFactoryWS.getCurrentSession();
 }

 @Override
 public void saveOrUpdate(Object entity, String operationName) {
    Session session = getCurrentSession();
    session.saveOrUpdate(entity);
 }
}

I get the following exception at the commented line :

Exception in thread "main" org.springframework.transaction.IllegalTransactionStateException: No existing transaction found for transaction marked with propagation 'mandatory'
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:359)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:447)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:277)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy37.saveOrUpdate(Unknown Source)

The code works absolutely fine when the abstract classes are removed, with only interfaces and their implementing classes remaining. But with the above setup, the transaction is not being propagated from ReqProcessor layer to the DAO layer. Please help. (Dont mind the 'public' accessors everywhere, it's just for testing) I have also searched on SO and other forums but couldnt find a solution.


回答1:


As @m-deinum has mentioned, Spring uses proxies to add "transactional" functionality, and this feature does not work when you call method annotated with @Transactional from another method of the class.

You have two ways to fix the problem:

  1. In AbstractReqProcessor autowire ApplicationContext and then use it to get a bean of CustomHibernateDao type. On this retrieved object you can call saveObject - then the transactional magic happens.
  2. The more preferred way is to annotate method saveOrUpdate of class AbstractService with @Transactional annotation too - then it will work again.

But I think you know the cause of the problem now and you can find another - more suitable for you - way.



来源:https://stackoverflow.com/questions/43343593/spring-transactional-propagation-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!