与Camel DSL交易时使用多个SQL插入语句

时间:2019-05-14 12:16:51

标签: apache-camel prepared-statement camel-sql

我有以下SQL Server查询:

BEGIN TRANSACTION T1; 

INSERT INTO table1 (t1_column1, t2_column2)
VALUES(1, GETDATE()); 

INSERT INTO table2 (column1_fk, column1, column2)
VALUES(SCOPE_IDENTITY(), 'someValue', CONVERT(VARBINARY, 'abcdefg'); 

COMMIT TRANSACTION T1;

使用SQL组件从Camel DSL执行此操作的最佳方法是什么?我可以想到四个选择:

  1. 将每个插入语句放在单独的“ to”中

    from("sourcedata")
        .setHeader("value2", simple("${date:now}"))
        .transacted()
        .to("sql:INSERT INTO table1 (t1_column1, t2_column2) VALUES (:#value1, :#value2)")
        .to("sql:INSERT INTO table1 (column1_fk, t1_column1, t2_column2) VALUES (:#GENERATED_KEYS, :#value1, :#value2)")
        .log("transaction is done");
    
  2. 允许JDBC执行多个查询

    首先设置JDBC连接:

    String dbUrl = "jdbc:mysql:///test?allowMultiQueries=true"; 
    

    使用以下骆驼DSL代码:

    from("sourcedata")
         .setHeader("value2", simple("${date:now}"))
        .to("sql:BEGIN;INSERT INTO table1 (t1_column1, t2_column2) VALUES (:#value1, :#value2; INSERT INTO table1 (column1_fk, t1_column1, t2_column2) VALUES (:#GENERATED_KEYS, :#value1, :#value2); COMMIT")
        .log("transaction is done");
    
  3. 写一个存储过程

    from("sourcedata")
        .setHeader("value2", simple("${date:now}"))
        .to("sql-stored:template")
        .log("transaction is done");
    
  4. 编写Java bean

    from("sourcedata")
        .setHeader("value2", simple("${date:now}"))
        .to("bean:StoreTransaction")
        .log("transaction is done");
    

使用Java代码中的JDBC事务准备语句,如MyKyong JDBC Transaction Example


什么是最佳选择? (我也在考虑性能/调整)。还有其他更简单的选择吗?

0 个答案:

没有答案