我有2个不同的数据库。我配置每个,然后将相关的TransactionManager与相应的@Transactional服务相关联。问题是,每当我尝试保存它时,尝试只使用其中一个事务管理器。它完全忽略了其他事务管理器。以下是配置:另请注意,这两个保存操作几乎同时在两个独立的Spring集成异步进程中运行,因为请求被发送到save(Product)并使用recipient-list保存(User)。这可能是问题的原因吗?
@Configuration
@EnableTransactionManagement
@ComponentScan("com.edx.db2")
@PropertySource(value=....)
public class DB2Config {
@Autowired
private Environment env;
@Bean
public UserDao UserDao(){
UserDao UserDao=new UserService();
return UserDao;
}
@Bean
@Qualifier(value="db2SessionFactory")
public SessionFactory sessionFactory(){
return db2DBSessionfactory().getObject();
}
@Bean
public LocalSessionFactoryBean db2DBSessionfactory() {
LocalSessionFactoryBean sessionFactoryBean =
new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(db2DataSource());
sessionFactoryBean.setPackagesToScan("com.edx");
sessionFactoryBean.
setHibernateProperties(hibernateProperties());
return sessionFactoryBean;
}
@Bean
public Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialeCT...
properties.put("hibernate.show_sql", true);
properties.put("hibernate.connection.d
properties.put("hibernate.connection.url
properties.put("hibernate.connection.username", e
properties.put("hibernate.connection.passwor
return properties;
}
@Bean(destroyMethod="close")
public DataSource db2DataSource() {
try {
DriverManage..
return dataSource;
} catch (Exception e) {
LOG.error("Unable to create connection pool",e);
return null;
}
}
@Bean
@Qualifier(value="db2TransactionManager")
public PlatformTransactionManager db2TransactionManager() {
HibernateTransactionManager htm=new
HibernateTransactionManager(db2DBSessionfactory().getObject());
htm.setDataSource(db2DataSource());
return htm;
}
}
@Configuration
@EnableTransactionManagement
@ComponentScan("com.edx.oracle")
@PropertySource(value={"classpath:...)
public class ORAConfig {
@Autowired
private Environment env;
@Bean
public ProductDao productDao(){
return new ProductService();
}
@Bean
public SessionFactory oracleSessionFactory(){
return oracleDBSessionfactory().getObject();
}
@Bean
public LocalSessionFactoryBean oracleDBSessionfactory() {
LocalSessionFactoryBean sessionFactoryBean =
new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(oracleDataSource());
sessionFactoryBean.setPackagesToScan("com.edx.oracle");
sessionFactoryBean.setHibernateProperties(hibernateProperties());
return sessionFactoryBean;
}
@Bean
@Qualifier(value="oracleTransactionManager")
public PlatformTransactionManager oracleTransactionManager() {
HibernateTransactionManager htm=new
HibernateTransactionManager
(oracleDBSessionfactory().getObject());
htm.setDataSource(oracleDataSource());
return htm;
}
@Bean
public Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect"
,env.getProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", true);
properties.put("hibernate.connection.driver_class",
env.getProperty("jdbc.driverClassName"));
properties.put("hibernate.connection.url",
env.getProperty("jdbc.url"));
properties.put("hibernate.connection.username",
env.getProperty("jdbc.username"));
properties.put("hibernate.connection.password",
env.getProperty("jdbc.password"));
return properties;
}
@Bean
public DataSource oracleDataSource() {
try {
DriverManagerDataSource dm=new DriverManagerDataSource();
dm.setDriverClassName(env.getProperty("jdbc.driverClassName"));
dm.setUsername(env.getProperty("jdbc.username"));
dm.setPassword(env.getProperty("jdbc.password"));
dm.setUrl(env.getProperty("jdbc.url"));
return dm;
} catch (Exception e) {
e.printStackTrace();
LOG.error("Unable to create connection pool",e);
return null;
}
}
}
@Repository
@Import(ORAConfig.class)
@Transactional("oracleTransactionManager")
public class UserService implements UserDao{
@Autowired
private SessionFactory oracleSessionFactory;
private Logger LOG=LoggerFactory.getLogger(UserService.class);
private PayloadIndexExtractor payloadIndexExtractor;
public UserService() {
}
public UserService() {
}
public void save(final UserInfo user) {
try{
//Changed from getCurrentSession()
Session session=oracleSessionFactory.openSession();
session.save(user) ;
}catch(Exception e){
e.printStackTrace();
LOG.error("Failed to save user",e);
}
}
}
@Repository
@Import(DB2Config.class)
@Transactional("db2TransactionManager")
public class ProductService implements ProductDao {
public ProducrService() {
}
@Autowired
private SessionFactory db2SessionFactory;
public void saveProduct(Product) {
sessionFactory.getCurrentSession().save(product);
}
任何想法我的配置有什么问题?请帮忙
答案 0 :(得分:0)
你需要的是一个ChainedTransactionManager作为名为transactionManager的bean。它必须包含其他HibernateTransactionManagers,并且要注意两者都在启动并使用@Transactional提交事务
代码应该看起来像
A = 1
B = 2
C = 4
D = 8
E = 16
F = 32
G = 64
H = 128
I = 256
J = 512
K = 1024
L = 2048
M = 4096
N = 8192