Search code examples
javaspringtransactionsspring-transactions

Does suspended @transactional method block resource?


AFAIK outer method and inner method are run in 2 different transactions on db level (because of REQUIRES_NEW). outer method makes update and waits for the inner method to commit. Does it mean that outer hold lock on user resource till inner commits?

@Autowired
private TestDAO testDAO;

@Autowired
private SomeBean someBean;

@Override
@Transactional(propagation=Propagation.REQUIRED)
public void outerMethod(User user) {
  testDAO.updateUser(user);
  
  someBean.innerMethod();
  
}

SomeBean:

@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
public void innerMethod() {
  //some work
}

Update:

Can this be presented like open 2 db consoles, then type and run one by one:

1. begin # 1st console
 
2. update user set...# 1st console
 
3. 4. 5. begin ...#do some work; commit; # 2nd console

6. commit; # 1st console

Solution

  • The transaction itself isn’t in charge of locking a row or table. If the sql executed by the dao is locking the row being updated, then that lock will be held until the transaction completes or times out. The update could be using optimistic locking in which case there isn’t a lock being held. So just looking at this code isn’t enough to tell if something is getting locked.

    But yes, any database level locking the code does while it’s in a transaction lasts until the transaction finishes.