-
Jan 14th, 2011, 02:03 PM
#1
@Transaction proxy mode
Experts,
I am using spring annotation base transactioning along with hibernate.
As per spring documentation -->
"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!"
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 -->
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("com.sample1. hibernate.UserService");
businessService.method1("shri");
}
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("com.sample1.hibernate.BusinessService")
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("shri", "test");
}
@Transactional (propagation=Propagation.REQUIRED)
public void method3 () throws BackingStoreException {
method1("shri");
}
}
@Service("com.sample1.hibernate.BusinessService1")
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
-
Jan 14th, 2011, 04:17 PM
#2
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
-
Jan 14th, 2011, 04:56 PM
#3
Please ignore the "readonly=true", it typo on my part.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules