Are the open and close methods in itemreader and itemwriter the right places to open and close database connection in jsr-352 java batch job? I couldn’t find in spec that when those two methods will be invoked, especially in exceptional senario
Database is one of many data sources for batch jobs, so the batch spec wouldn't prescribe requirements specific to database connection. The answer to your question depends much on how you implement your jdbc item reader and jdbc item writer.
In general, database connections are scarce and expensive resource, and therefore are shared resources. You don't want any part of your application to hold on to connections for long period of time. So a typical pattern is to acquire database connection on-demand, and release (close) it immediately after.
Now look at the lifecycle of jdbc item reader and jdbc item writer. They belong to a step execution, and so their life will span the whole step execution. It is not a good idea to hold on to connection for such a long period of time, especially for connection used in jdbc item writer for updating database records. For example, when implementing JdbcItemWriter, we chose to obtain connection on-demand when ready to write the chunk of data and release connection immediately after usage (i.e., not in open
or close
methods). In this case, there is no need to keep the connection open between chunks.
For JdbcItemReader, we chose to open connection in item reader open
method and close it in close
method. This is because our implementation is based on a live jdbc ResultSet
from which to continuously fetch data. Of course, other implementations can choose to cache or detach data and thus not rely on a live ResultSet
, and instead use the on-demand pattern for better resource utilization.