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"}
JDBC chunk oriented processing with jberet.local-tx
TweetPosted 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
Entire project can be obtained from my GitHub repository. the batch has 2 steps as follows:
-
prepare
: createsSRC
andDEST
table, and insert 100 rows intoSRC
table. refer source of Batchlet for details. -
test
: loads data fromSRC
table, then simply writes data intoDEST
table as is using chunk oriented processing. this step has a propertyjberet.local-tx
withtrue
value.MyItemReader
creates and disposes JDBC resources inopen
andclose
method. refer source of MyItemReader and MyItemWriter for details.
How to run the batch
-
Define a H2 DataSource
-
Deploy the project
-
Access http://localhost:8080/localtxtest-1.0-SNAPSHOT/ to launch the batch through the Servlet which mapped at
/
-
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.