Kohei Nozaki's blog 

JDBC chunk oriented processing with jberet.local-tx


Posted on Saturday Feb 21, 2015 at 11:40PM in Technology


As following URL said, in my understanding, each open, read, write and close of every JDBC resources (Connection, Statement and ResultSet) needs to be in it’s own transaction.

There are open and close methods in ItemReader and ItemWriter interface. these methods looks like good to create and dispose JDBC resources such as Connection, Statement and ResultSet but we can’t go that way due to the JSR352 spec. before invocation of these methods, the framework starts a transaction, then commits a transaction after invocation finished. according to preceding URL said, these resources become unusable at readItem and writeItems method because these resources were created in another transaction.

It’s terrible to do open a cursor again and again at start of every chunk processing. to overcome this problem, JBeret supplied an implementation specific parameter named jberet.local-tx. so I created a sample batch project to test that parameter. my test was done with WildFly 8.2.0.Final.

How the batch works

  1. prepare: creates SRC and DEST table, and insert 100 rows into SRC table. refer source of Batchlet for details.

  2. test: loads data from SRC table, then simply writes data into DEST table as is using chunk oriented processing. this step has a property jberet.local-tx with true value. MyItemReader creates and disposes JDBC resources in open and close method. refer source of MyItemReader and MyItemWriter for details.

How to run the batch

  1. Define a H2 DataSource

    xa-data-source add \
     --name=MyDS \
     --driver-name=h2 \
     --jndi-name=java:jboss/datasources/MyDS \
     --user-name=sa \
     --password=sa \
     --xa-datasource-properties={ \
      "URL" => "jdbc:h2:/tmp/localtxtest;AUTO_SERVER=TRUE"}
  2. Deploy the project

  3. Access http://localhost:8080/localtxtest-1.0-SNAPSHOT/ to launch the batch through the Servlet which mapped at /

  4. Look your database to the check batch works expectedly

Notes

Actually, the problem ARJUNA016087 warning is disappeared in latest WildFly 8.2.0.Final without using jberet.local-tx. but I don’t know whether it is intended to fix or simply by chance still. I’ll keep looking further of this discussion.



No one has commented yet.

Leave a Comment

HTML Syntax: NOT allowed