Matej Tymes
Matej Tymes

Reputation: 1724

Spring - get used TransactionManager

I have two transaction managers and was curious if there is some possibility to get the one that has been used.

To be more concrete, how could underlyingMethod(..) find out which transactionManager was used (without sending it an additional parameter "transactionManagerName/Ref"):

@Transactional("transactionManager1")
public void transactionFromFirstTM() {
    someClass.underlyingMethod()
}


@Transactional("transactionManager2")
public void transactionFromSecondTM() {
    someClass.underlyingMethod()
}

?


ok I have used this to get the hibernate Session from actual transaction manager:

protected Session getSession() {
    Map<Object, Object> resourceMap = TransactionSynchronizationManager.getResourceMap();

    Session session = null;
    for (Object value : resourceMap.values()) {
        if (value instanceof SessionHolder) {
            session = ((SessionHolder) value).getSession();
            break;
        }
    }

    return session;
}

Upvotes: 4

Views: 4279

Answers (1)

Bozho
Bozho

Reputation: 597116

I don't think you can, but you shouldn't do anything with the transaction manager. Some actions on the current transaction are available in TransactionSynchronizationManager

Another useful class is the TransactionAspectUtils. But not that both are meant to be used internally, and you should not rely on them in many places in your code.

Upvotes: 2

Related Questions