Skip to content

@Transactional with txManager name is cumbersome with base dao classes [SPR-9661] #14295

Closed
@spring-projects-issues

Description

@spring-projects-issues

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: dataIssues in data modules (jdbc, orm, oxm, tx)status: bulk-closedAn outdated, unresolved issue that's closed in bulk as part of a cleaning process

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions