AM01
AM01

Reputation: 314

Service calls to multiple dao methods

I have a Service class such as following:

@Transactional
@Component(value = "userServiceImpl")
public class UserServiceImpl implements UserService
{
  @Autowired
  private UserDao             userDaoiBatis;

  public boolean X()
  {
    // Call UserDao.A and UserDao.B in transaction.
  }
}

And a Dao class such as following:

@Transactional
@Component(value = "userDaoiBatis")
public class UserDaoiBatis extends SqlMapClientDaoSupport implements UserDao
{
  @Autowired
  private void injectSqlMapClient(SqlMapClient sqlMapClient)
  {
    setSqlMapClient(sqlMapClient);
  }

  @Override
  public boolean A()
  {
    throw new RuntimeException("Not implemented");
  }

  @Override
  public boolean B()
  {
    throw new RuntimeException("Not implemented");
  }
}

How can I call UserDaoiBatis.A() and UserDaoiBatis.B() as part of a transaction in userServiceImpl.Z() ???

Upvotes: 1

Views: 1791

Answers (1)

AngerClown
AngerClown

Reputation: 6229

What you have setup looks correct. The transaction will be started by UserServiceImpl then both calls to UserDaoiBatis will inherit the existing transaction (@Transactional defaults to REQUIRES propagation). You can confirm this by enabling debug level logging on the AbstractPlatformTransactionManager class.

Note that with A() throwing an exception, the tx will rollback.

Upvotes: 2

Related Questions