Results 1 to 5 of 5

Thread: multiple cursor with spring batch

  1. #1
    Join Date
    Sep 2009
    Posts
    3

    Question multiple cursor with spring batch

    Hi All,

    I am learning spring batch and have a problem,
    I am trying to do the same as the following with spring batch.
    How can I get it done?
    NOTE:It has two cursors in one step.

    Code:
    con = dataSource.getConnection();
    stmt = con.createStatement();
    
    res1 = stmt.executeQuery(sql1);
    while (res1.next()) {
        ……
    }
    /* write data to file1 */
    
    res2 = stmt.executeQuery(sql2);
    while (res2.next()) {
        ……
    }
    /* write data to file2 */
    Thanks

  2. #2
    Join Date
    Dec 2006
    Posts
    1,061

    Default

    Unless I'm missing something, this scenario would be easily implemented using two steps, one for each cursor, each using the JdbcCursorItemReader. Keep in mind, that the steps run through all of the input before moving on to the next step. Meaning, step1 reads and writes until there's nothing left to read, then the second step starts.

  3. #3
    Join Date
    Sep 2009
    Posts
    3

    Default

    lucasward

    Thanks for your reply.
    I'm trying to solve it using one step.
    Is that possible with spring batch?

  4. #4
    Join Date
    Feb 2008
    Posts
    488

    Default

    If you're reading from two separate queries, it really makes sense to keep the processing separate. Step 1 processes query 1. Step 2 processes query 2. Unless you need to process both queries together, in which case a JOIN would be appropriate, but still that's just one SQL execution.

    If that doesn't seem to work for you, perhaps you could explain why you need two queries in one step.

  5. #5
    Join Date
    Sep 2009
    Posts
    3

    Default

    There is something worng with my code.
    I changed it:

    Code:
    con = dataSource.getConnection();
    stmt = con.createStatement();
    
    res1 = stmt.executeQuery(sql1);
    while (res1.next()) {
        ……
    }
    /* write data to table1 */
    
    res2 = stmt.executeQuery(sql2);
    while (res2.next()) {
        ……
    }
    /* write data to table2 */
    My meaning is when the code get res2 was failure, then the operation of writing table1 should be rollback.
    I don`t know how to achieve it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •