spring
spring copied to clipboard
recreate SqlSessionFactory will cause rollback fail
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 DataSource
s and SqlSessionFactory
s 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!
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.
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.
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.