سؤال

I wanted to set the transactionManager based on some input criteria and hence I moved from declarative to programmatic transaction management .

something like

public User saveUser(NewUser newUser){

     return transactionTemplate.execute(new TransactionCallback() {
              // the code in this method executes in a transactional context
              public Object doInTransaction(TransactionStatus status) {
                  try {
                        User savedObj = someService.saveUser(newUser);
        return savedObj ;
                } catch (DataManagerAPIException e) {
                    throw new RuntimeException(e);
                }
              }
            });
 }

Everything works fine but wrapping every service call (our transaction boundary is at the GWT client service level.Something like UI-->Client Service-->Service-->Dao) with a Transaction callback is making the code bit of a mess compared to @Transactional . Can there be an easier way to do this ? Maybe an AOP based approach ?

I tried the following

//Wrap every Client service method with a transaction.

@Around("execution(* com.myProject.server.service.*.*(..))")
public void transactionManagerProviderResult(final ProceedingJoinPoint pjp) {

    transactionTemplate.execute(new TransactionCallback() {

        @Override
        public Object doInTransaction(TransactionStatus status) {
            try {
                            Object result = pjp.proceed();
            return result ;
            } catch (Throwable e) {
                e.printStackTrace();
                return null;
            }
        }
    });
}

The code runs fine inside the transactional context and the 'result ' contains the right value(The newly saved user) but the caller of the saveUser method(GWT Widget layer) always gets a null object . This is not an issue with GWT since I tried it independent of GWT also . Everything seems to be fine till 'result' . After this the Object is lost. What could be the possible reason and solution for the issue?

هل كانت مفيدة؟

المحلول

The caller gets a null object because you don't return anything from your method:

public Object transactionManagerProviderResult(final ProceedingJoinPoint pjp) {
    return transactionTemplate.execute(new TransactionCallback() {
        // ...

It's not very clear why you need to do this yourself instead of letting the @Transactional Spring support do it for you.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top