javatransactionsspring-batchstack-overflowcircular-dependency

Circular dependency on TransactionManager Bean in Spring Batch


I'm working on a Spring Batch project and I have a StackOverflowError with this Bean.

@Bean
@Primary
public JtaTransactionManager transactionManager(UserTransaction userTransaction,
        TransactionManager transactionManager) {
    JtaTransactionManager jtaTransactionManager = new JtaTransactionManager(userTransaction, transactionManager);
    jtaTransactionManager.setAllowCustomIsolationLevels(true);
    return jtaTransactionManager;
}

If I changed the function name with transactionManager2 for example, it solve my error but I don't know why. Someone can tell me why and suggest a better option ? Thanks.


Solution

  • The JtaTransactionManager does not implement the Java Transaction API, it just delegates the management of distributed transactions to a third-party provider, such as the WildFly or Jakarta application server. This means that you are already supposed to have a bean in your application context that implements the TransactionManager interface. However, the circular dependency means that the transactionManager bean that you want to receive as a parameter for the transactionManager() method is the same bean that the transactionManager() method should return. This means that there are no other transaction managers in your application. This can happen if you run your application outside of the application server, or if you remove the dependency on the library that implements JTA.