Quantcast
Viewing all articles
Browse latest Browse all 1486

Transaction manager created twice when using java config for spring

I'm trying to create a DistributedJiniTransactionManager bean via spring using java config, not xml config. But I can see that the Mahalo transaction manager is being created twice.
Feb 16, 2016 7:45:20 PM com.sun.jini.mahalo.TxnManagerImpl doInit
INFO: Started Mahalo (duration=0.122): com.sun.jini.mahalo.TransientMahaloImpl@6a581b90
Feb 16, 2016 7:45:22 PM com.sun.jini.mahalo.TxnManagerImpl doInit
INFO: Started Mahalo (duration=0.015): com.sun.jini.mahalo.TransientMahaloImpl@21929486
I'm creating the manager using a DistributedJiniTxManagerConfigurer -
@Bean
public PlatformTransactionManager txManager() throws Exception {
    return new DistributedJiniTxManagerConfigurer().defaultTimeout(15).transactionManager();
}
After a lot of digging I have found that it is because the AbstractJiniTransactionManager class which DistributedJiniTransactionManager extends, implements InitializingBean. When spring creates a bean that implements InitializingBean, it calls afterPropertiesSet() on the bean. In this case afterPropertiesSet() starts a Mahalo instance. But when you also call transactionManager() on the configurer it too calls afterPropertiesSet() as shown below.
public PlatformTransactionManager transactionManager() throws Exception {
    if (!initialized) {
        distributedJiniTransactionManager.afterPropertiesSet();
        initialized = true;
    }
    return distributedJiniTransactionManager;
}
There is a check transactionManager() to see if the transactionManager has already been initialized, but because AbstractJiniTransactionManager implements InitializingBean, it calls afterPropertiesSet() directly, bypassing this check. This does not happen when I create the transaction manager via xml config, most likely because it is not using the configurer and calling the constructor directly. I have a workaround where I create the transaction manager directly and set the relevant properties, but I would like to be able to use the configurer.
@Bean
public PlatformTransactionManager txManager() throws Exception {
    DistributedJiniTransactionManager tx = new DistributedJiniTransactionManager();
    tx.setDefaultTimeout(15);
    return tx;
}

Viewing all articles
Browse latest Browse all 1486

Trending Articles