Can we simplify programmatic transaction management with AOP?

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?

Answers


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.


Need Your Help

How to keep app running in background to monitor movements

ios background long-running-processes background-application

I am implementing an application to monitor user's movement/activity (when playing game/sport or sleeping in bed) using accelerometer via CMMotionManager, so I need to keep app running in backgroun...

Facebook SSO error passing reference of App Delegate

objective-c facebook facebook-graph-api

I followed the example SSO on Facebook's developer website but am getting a strange warning message when I call the authorize message:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.