当前位置: 首页 > 知识库问答 >
问题:

Apache Camel 2.13.1 MyBatis事务未回滚

李飞翼
2023-03-14

我有一个场景,我必须首先更新三个表中的一行,然后在每个表中插入一个新行。所有这些都应该放在一批语句中,如果失败则回滚。

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(); 

共有1个答案

桓嘉谊
2023-03-14

解决方案很简单,请参见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()); 
 类似资料:
  • 我有一个场景如下 我的问题是,对于一个给定的流,我可能有两个请求同时调用这个方法。我希望methodC抛出乐观锁定失败和回滚事务的异常。 发生的情况如下:R1和R2调用methodA->methodB(启动一个新事务)->methodC(启动一个新事务):两个都读取相同的实体版本,都进行相同的更改并调用merge->methodC完成流回methodB->methodB完成强制事务提交->事务提交

  • 问题内容: 我在EJB3无状态会话Bean中使用CMT。另外,我还创建了自己的具有注释“ @ApplicationException(rollback = true)”的异常。 要回滚事务时是否必须使用“ context.setRollbackOnly()”? 我可以通过在bean的public方法内抛出异常来回滚事务吗? 如果是这样(对Q#2的回答是“是”),我是否必须通过在方法中声明异常来将异

  • 我使用spring 3.2并有一些事务。当我遇到异常时,回滚似乎没有完成。 我的代码 } 基本上,我有一个类X,createX方法启动一个事务。它调用createY谁调用call YY。在这个方法中发生了异常。我在想,那么自createX以来的所有持久操作都将被回滚,但这不是谁发生的 我在日志中没有看到交易信息 任何想法

  • 我在使用Async/Await时遇到了一个问题,无法使事务范围回滚。在没有事务作用域的情况下,一切都正常工作,但只要我有意造成异常(第二次迭代时插入的主键重复),就不会发生回滚(更新时)或任何与事务相关的错误。 我还应该注意,除非连接字符串中包含“OLE DB Services=4”,否则我将收到错误:“ITransactionLocal接口不受'Microsoft.ACE.OLEDB12.0'提

  • 我正在使用spring-test运行JUnit测试,我的代码如下所示 我的问题是我希望我的测试不影响其他测试。所以我想为每个测试创建一些类似回滚的东西。我为此找了很多,但到目前为止一无所获。我使用Hibernate和MySql来实现这个

  • -ZJ 以下是我在Application.Properties中的数据源设置: