使用分布式 Mysql 数据库时,我使用 BeanDefinitionBuilder 创建了多个数据源、会话工厂但是当我使用下面的 getBean('bean name') 方法执行插入 SQL 时,事务注释似乎不起作用
((SqlSessionTemplate)context.getBean('bean name')).insert("xxxx",params)
你能解释一下我错过了什么吗?
Private GenericApplicationContext context = new GenericApplicationContext();
BeanDefinitionBuilder sessionFactoryBuilder = BeanDefinitionBuilder.rootBeanDefinition(org.mybatis.spring.SqlSessionFactoryBean.class);
sessionFactoryBuilder.addPropertyReference("dataSource", "dataSource" + beanName);
sessionFactoryBuilder.addPropertyValue("configLocation", "/sqlmap.xml");
context.registerBeanDefinition("sqlSessionFactory" + beanName, sessionFactoryBuilder.getBeanDefinition());
BeanDefinitionBuilder transactionManagerBuilder = BeanDefinitionBuilder.rootBeanDefinition(org.springframework.jdbc.datasource.DataSourceTransactionManager.class);
transactionManagerBuilder.addPropertyReference("dataSource", "dataSource" + beanName);
context.registerBeanDefinition("transactionManager",transactionManagerBuilder.getBeanDefinition());
ctx.refresh();