Multiple transaction managers with @Transactional annotation

Viewed 24323
  1. We have base generic manager which is inherited by all managers. Base manager is annotated with @Transactional annotations.

  2. There are 2 groups of transactional services:

    • x.y.service1.* - have to be managed by transactionManager1
    • x.y.service2.* - have to be managed by transactionManager2

How can transactions be configured without nessesity to override ALL transactional methods and specify transaction manager?

@Transactional(readOnly = true)
public abstract class GenericManagerImpl<D extends IGenericDao, T extends BaseObject, PK extends Serializable>
        implements IGenericManager<T, PK> {

    protected D dao;

    @Autowired
    public void setDao(D dao) {
        this.dao = dao;
    }


    @Transactional(readOnly = false)
    public void save(T object) {
        dao.save(object);
    }

    @Transactional(readOnly = false)
    public void remove(T object) {
        dao.remove(object);
    }


}

@Service
class UserManagerImpl
        extends GenericManagerImpl<IUserDao, User, Long>
        implements IUserManager {

// Everything is OK. User is managed by txManager1

}

@Service
class BillingManagerImpl
        extends GenericManagerImpl<IBillingDao, Billing, Long>
        implements IBillingManager {

    @Override
    @Transactional(readOnly = false, value="txManager2") // <--have to override method only to specify txManager
    public void save(final Billing billing ) {
        super.save(billing);
    }

    @Override
    @Transactional(readOnly = false, value="txManager2") // <--have to override method only to specify txManager
    public void remove(final Billing billing ) {
        super.remove(billing);
    }
}

Most probably i need to combine aop with @Transactional annotation.
Actually, what i want is:
1) To be able to configure transactions (read only flag, propogation, isolation etc) with @Transactional annotation.
2) define strategy to choose transaction manager outside of my classes (using AOP, for example)
x.y.service1.* -> use txManager1
x.y.service2.* -> use txManager2

Is it possible?

2 Answers
Related