Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recreate SqlSessionFactory will cause rollback fail #114

Open
cloorc opened this issue Mar 6, 2016 · 3 comments
Open

recreate SqlSessionFactory will cause rollback fail #114

cloorc opened this issue Mar 6, 2016 · 3 comments

Comments

@cloorc
Copy link

cloorc commented Mar 6, 2016

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
Copy link
Author

cloorc commented Mar 6, 2016

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.

@kazuki43zoo
Copy link
Member

kazuki43zoo commented Oct 27, 2017

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.

@cloorc
Copy link
Author

cloorc commented Oct 28, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants