我有一个场景,我必须先在三个表中更新一行,然后在每个表中插入一个新行。如果失败,所有这些应该在一批语句和回滚中。
Scenario below
e.g
statement1 = update table1;
statement2= update table2;
statement3 =update table3;
statement4 insert into table1;
statement5 insert into table2;
statement6 insert into tables3
Camel社区对上述问题的回答是使用了一个事务客户端,但现在问题是当一个MyBatis语句失败时事务没有被回滚。
E.g. Exception case:The first two updates were not rolled back on failure of the third one below:
.to("mybatis:userMapper.updatePerson?statementType=Update") --- Passed
.to("mybatis:userMapper.updateCertificate8?statementType=Update") ---- Passed
.to("mybatis:userMapper.updateApplicationGroup?statementType=Update") ---- Failed
**`Am I missing anything?`**
Camel Registry
SimpleRegistry registry = new SimpleRegistry();
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(
sqlSessionFactory.getConfiguration().getEnvironment()
.getDataSource());
registry.put("transactionManager",dataSourceTransactionManager);
SpringTransactionPolicy springTransactionPolicy = new SpringTransactionPolicy();
springTransactionPolicy.setTransactionManager(dataSourceTransactionManager);
springTransactionPolicy.setPropagationBehaviorName("PROPAGATION_REQUIRED");
registry.put("PROPAGATION_REQUIRED",springTransactionPolicy);
camelContext = new DefaultCamelContext(registry);
camelContext.setTracing(true);
camelContext.start();
Camel Route:
onException(JMSException.class)
.handled(true).maximumRedeliveries(0).end();
onException(IllegalArgumentException.class)
.handled(true).maximumRedeliveries(0).rollback("Rolling back the IllegalArgumentException")
.end();
onException(PersistenceException.class)
.handled(true).maximumRedeliveries(0).rollback("Rolling back the transaction")
.end();
onException(RollbackExchangeException.class)
.handled(false).maximumRedeliveries(0).process(new CamelTibcoMessageProcessor())
.end();
from("timer:foo?period=10000")
.policy("PROPAGATION_REQUIRED") .to("mybatis:userMapper.updatePerson?statementType=Update")
.to("mybatis:userMapper.updateCertificate8?statementType=Update")
.to("mybatis:userMapper.updateApplicationGroup?statementType=Update")
.to("mybatis:userMapper.insertPersonFromCAMSCTSBridge?statementType=InsertList&executorType=batch")
.end();
答案 0 :(得分:0)
想出来,解决方案很简单,请参阅Camel MyBatis和MyBatis-Spring的链接。
将Spring配置转换为Java,然后就可以了。以下是我的所作所为,效果很好。
将 mybatis-spring maven依赖项添加到您的pom。
**------------------Sample setup--------------------**
SimpleRegistry registry = new SimpleRegistry();
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass("oracle.jdbc.driver.OracleDriver");
cpds.setJdbcUrl("jdbc_url");
cpds.setUser("username");
cpds.setPassword("password");
TransactionAwareDataSourceProxy transactionAwareDataSourceProxy = new TransactionAwareDataSourceProxy(cpds);
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(transactionAwareDataSourceProxy);
registry.put("transactionManager",transactionManager);
ApplicationContext appContext = new ClassPathXmlApplicationContext();
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setConfigLocation(appContext.getResource("mapper/your_mybatis_config.xml"));
factoryBean.setDataSource(cpds);
SpringTransactionPolicy propagationRequired = new SpringTransactionPolicy();
propagationRequired.setTransactionManager(transactionManager);
propagationRequired.setPropagationBehaviorName("PROPAGATION_REQUIRED");
registry.put("PROPAGATION_REQUIRED",propagationRequired);
SpringTransactionPolicy propagationRequiredNew= new SpringTransactionPolicy();
propagationRequiredNew.setTransactionManager(transactionManager);
propagationRequiredNew.setPropagationBehaviorName("PROPAGATION_REQUIRES_NEW");
registry.put("PROPAGATION_REQUIRES_NEW",propagationRequiredNew);
camelContext = new DefaultCamelContext(registry);
camelContext.setTracing(true);
camelContext.start();
MyBatisComponent component = new MyBatisComponent();
component.setSqlSessionFactory(factoryBean.getObject());
camelContext.addComponent("mybatis", component);
camelContext.addRoutes(new SomeRoute());