spring icon indicating copy to clipboard operation
spring copied to clipboard

recreate SqlSessionFactory will cause rollback fail

Open cloorc opened this issue 8 years ago • 3 comments

Hi, guys!

In my circumstance:

org.springframework.boot:spring-boot-dependencies:1.3.0.RELEASE
spring 4.2.3
mybatis-3.2.3
mybatis-spring 1.2.3

We need multiple different DataSources and SqlSessionFactorys and so on. So I tried to register them manually in ApplicationListener. However, when I registered the BeanDefinition with type of SqlSessionFactory, I found the transaction rollback do not work.

Actually, I built the BeanDefinition from the Configuration of DefaultSqlSessionFactory built from SqlSessionFactoryBean. I doubt this would be the reason.

The registration code:

BeanDefinition sessionFactory = getSqlSessionFactoryDefinition(single);
registry.registerBeanDefinition(single.getSqlSessionFactoryName(), sessionFactory);

The getSqlSessionFactoryDefinition method:

public BeanDefinition getSqlSessionFactoryDefinition (MybatisProperties mybatis) {
    DataSource source = getDataSource(mybatis);
    DefaultSqlSessionFactory factory = (DefaultSqlSessionFactory) getSqlSessionFactory(mybatis, source);
    Configuration configuration = factory.getConfiguration();

    Argument[] arguments = new Argument[] {
        new Argument(CV, configuration)
    };
    return getBeanDefinition(DefaultSqlSessionFactory.class, arguments);
}

The method getBeanDefinition just try to fill the BeanDefinitionBuilder with configuration as the only parameter of addConstructorArgValue and return a proper BeanDefinition.

Actually, it was something like getting a BeanDefinition by getting a Bean. But I'd no idea how to build a right BeanDefinition without the help of SqlSessionFactoryBean.

Please give me some tips, thanks a lot!

cloorc avatar Mar 06 '16 02:03 cloorc

Instead, If I registered directly through registerSingleton as following:

SqlSessionFactory sessionFactory = getSqlSessionFactory(single, (DataSource) registry.getBean(single.getDataSourceName()));
registry.registerSingleton(single.getSqlSessionFactoryName(), sessionFactory);

Transaction rollback normally.

cloorc avatar Mar 06 '16 02:03 cloorc

Hi @cloorc, Sorry for the late reply. Could be resolved this issue? If you cannot reolved, I hope to provide a reproduce project on GitHub.

kazuki43zoo avatar Oct 27 '17 16:10 kazuki43zoo

Hi, I think this is something about best practice. Users should not recreate it multiple times I think. My solution is just not trying to recreate it again.

cloorc avatar Oct 28 '17 01:10 cloorc