Back Forum Reply New

@Transaction proxy mode

Experts,

I am using spring annotation base transactioning along with hibernate.

As per spring documentation --gt;
quot;Note: In proxy mode (which is the default), only 'external' method calls coming in through the proxy will be intercepted. This means that 'self-invocation', i.e. a method within the target object calling some other method of the target object, won't lead to an actual transaction at runtime even if the invoked method is marked with @Transactional!quot;

I have 2 business service as below.

As per notes - no new transaction get create when BusinessService.method3() calls BusinessService.method1(). Even though BusinessServicemethod1() has REQUIRES_NEW

While the BusinessService1.method1() when invoked from BusinessService.method2(), will create a new transaction.

Please correct me if i am wrong.

I see 2 possible solutions at this point --gt;

1. Either move method2() to another Business Service.
2. Change method 3 to do spring lookup to get proxy object to itself.
@Transactional (propagation=Propagation.REQUIRED)
public void method3 () throws BackingStoreException {
BusinessService businessService = (BusinessService) ServiceFactory.getInstance().getBean(quot;com.sample1.  hibernate.UserServicequot;);
businessService.method1(quot;shriquot;);   }
3. Chaneg the configuration to use AspectJ mode instead of default proxy mode.

Do i have any more options then above 3.
I m leaning towards solution 2. Are there are negative effect of this.

@Service(quot;com.sample1.hibernate.BusinessServicequot;)
public class BusinessService {

@Autowired
private BusinessService1 businessService1;
   @Transactional (propagation=Propagation.REQUIRES_NEW, readOnly=true)   public void method1(String login) {          }      @Transactional (propagation=Propagation.REQUIRED)
public void method2 () throws BackingStoreException {
businessService1.method1(quot;shriquot;, quot;testquot;);       }

@Transactional (propagation=Propagation.REQUIRED)
public void method3 () throws BackingStoreException {
method1(quot;shriquot;);   }

}

@Service(quot;com.sample1.hibernate.BusinessService1quot;)
public class BusinessService1 {

@Autowired   private UserDAO userDAO;
   public void setUserDAO(UserDAO userDAO) {       this.userDAO = userDAO;   }
   @Transactional (propagation=Propagation.REQUIRES_NEW, readOnly=true)
public void method1 (String login, String password) {       }   
}

Thanks, Appreciate your thoughts.
Shri

I can't say I'd recommend calling Spring directly.  Switching to a dependency lookup paradigm like you'd be doing there would make your code pretty difficult to test and maintain.  If you had to, I'd say using AspectJ for Transactions would be your best bet.  

That said, your example has all the REQUIRES_NEW transactions defined as read only.  If this is really the case, you get absolutely NO benefit from starting a new transaction - the value-add in REQUIRES_NEW is that changes made in the new transaction survive rollbacks in the original transaction.  If you're not making any changes, there's not going to be any difference between running in the original or running in a new transaction.

Hope this helps
- Don

Please ignore the quot;readonly=truequot;, it typo on my part.
¥
Back Forum Reply New