Description
matthew inger opened SPR-9661 and commented
By embedding the transaction manager name in the @Transactional
anotations, using base dao classes is cumbersome. Example:
@Transactional
public class GenericDao<K extends Serializable,E> {
protected Class<E> mappedClass;
protected SessionFactory sessionFactory;
public GenericDao(Class<E> mappedClass, SessionFactory sessionFactory) {
this.mappedClass = mappedClass;
this.sessionFactory = sessionFactory;
}
@Transactional(readOnly=false)
public E getById(K key) {
return sessionFactory.getCurrentSession().get(mappedClass, key);
}
public E merge(E entity) {
return sessionFactory.getCurrentSession().merge(entity);
}
}
Now we have two subclasses, each of which will use different sessionfactory, and transaction manager (we don't need jta).
@Transactional
("fooTxManager")
public class FooDao extends GenericDao<Long, Foo> {
...
}
@Transactional
("barTxManager")
public class FooDao extends GenericDao<Long, Foo> {
...
}
The problem is that we'd have to override the getById() method otherwise that method will use the default transaction manager. Not a big deal with 1 method, but it is a big deal when there's a bunch of methods.
@Transactional
("barTxManager")
public class FooDao extends GenericDao<Long, Foo> {
@Override
@Transactional
("barTxManager")
public Foo getById(Long key) {
return super.getById(key);
}
...
}
I think a better approach would be to either:
a) Allow the transaction manager name, when left blank, to fallback to the annotation at the class level.
b) Separate the transaction semantics from the manager name with a new annotation:
@TransactionManager
("barTxManager")
Affects: 3.1.1