Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2008 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.springframework.batch.item.database;
 
 import java.util.List;
 import java.util.Map;
 
 

org.springframework.batch.item.ItemReader for reading database records using JDBC in a paging fashion.

It executes the SQL built by the PagingQueryProvider to retrieve requested data. The query is executed using paged requests of a size specified in AbstractPagingItemReader.setPageSize(int). Additional pages are requested when needed as org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read() method is called, returning an object corresponding to current position. On restart it uses the last sort key value to locate the first page to read (so it doesn't matter if the successfully processed itmes have been removed or modified).

The performance of the paging depends on the database specific features available to limit the number of returned rows. Setting a fairly large page size and using a commit interval that matches the page size should provide better performance.

The implementation is thread-safe in between calls to open(org.springframework.batch.item.ExecutionContext), but remember to use saveState=false if used in a multi-threaded client (no restart available).

Author(s):
Thomas Risberg
Dave Syer
Since:
2.0
 
 public class JdbcPagingItemReader<T> extends AbstractPagingItemReader<T> implements InitializingBean {

 
 	private static final String START_AFTER_VALUE = "start.after";
 
 	public static final int VALUE_NOT_SET = -1;
 
 	private DataSource dataSource;
 
 
 
 
 	private RowMapper rowMapper;
 
 	private String firstPageSql;
 
 
 
	private int fetchSize = ;
	}
	public void setDataSource(DataSource dataSource) {
		this. = dataSource;
	}

Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for this ResultSet object. If the fetch size specified is zero, the JDBC driver ignores the value.

Parameters:
fetchSize the number of rows to fetch
See also:
java.sql.ResultSet.setFetchSize(int)
	public void setFetchSize(int fetchSize) {
		this. = fetchSize;
	}

A PagingQueryProvider. Supplies all the platform dependent query generation capabilities needed by the reader.

Parameters:
queryProvider the PagingQueryProvider to use
	public void setQueryProvider(PagingQueryProvider queryProvider) {
		this. = queryProvider;
	}

The row mapper implementation to be used by this reader. The row mapper is used to convert result set rows into objects, which are then returned by the reader.

	public void setRowMapper(RowMapper rowMapper) {
		this. = rowMapper;
	}

The parameter values to be used for the query execution. If you use named parameters then the key should be the name used in the query clause. If you use "?" placeholders then the key should be the relative index that the parameter appears in the query string built using the select, from and where clauses specified.

Parameters:
parameterValues the values keyed by the parameter named/index used in the query string.
	public void setParameterValues(Map<StringObjectparameterValues) {
		this. = parameterValues;
	}

	public void afterPropertiesSet() throws Exception {
		JdbcTemplate jdbcTemplate = new JdbcTemplate();
			jdbcTemplate.setFetchSize();
		}
		jdbcTemplate.setMaxRows(getPageSize());
		this. = new SimpleJdbcTemplate(jdbcTemplate);
	}
	protected void doReadPage() {
		if ( == null) {
		}
		else {
		}
		PagingRowMapper rowCallback = new PagingRowMapper();
		@SuppressWarnings("unchecked")
		List query;
		if (getPage() == 0) {
				.debug("SQL used for reading first page: [" +  + "]");
			}
			if ( != null && .size() > 0) {
							getParameterMap(null), rowCallback);
				}
				else {
							getParameterList(null).toArray(), rowCallback);
				}
			}
			else {
			}
		}
		else {
				.debug("SQL used for reading remaining pages: [" +  + "]");
			}
			}
			else {
			}
		}
		@SuppressWarnings("unchecked")
		Collection<T> result = query;
		.addAll(result);
	}
	public void update(ExecutionContext executionContextthrows ItemStreamException {
		super.update(executionContext);
		if (isSaveState() &&  != null) {
		}
	}
	public void open(ExecutionContext executionContext) {
		if (isSaveState()) {
		}
		super.open(executionContext);
	}
	protected void doJumpToPage(int itemIndex) {
		/*
		 * Normally this would be false (the startAfterValue is enough
		 * information to restart from.
		 */
		if ( == null && getPage() > 0) {
			String jumpToItemSql;
			jumpToItemSql = .generateJumpToItemQuery(itemIndexgetPageSize());
				.debug("SQL used for jumping: [" + jumpToItemSql + "]");
			}
			RowMapper startMapper = new RowMapper() {
				public Object mapRow(ResultSet rsint ithrows SQLException {
					return rs.getObject(1);
				}
			};
			}
			else {
			}
		}
	}
	private Map<StringObjectgetParameterMap(Map<StringObjectvaluesObject sortKeyValue) {
		Map<StringObjectparameterMap = new LinkedHashMap<StringObject>();
		if (values != null) {
			parameterMap.putAll(values);
		}
		if (sortKeyValue != null) {
			parameterMap.put("_sortKey"sortKeyValue);
		}
			.debug("Using parameterMap:" + parameterMap);
		}
		return parameterMap;
	}
	private List<ObjectgetParameterList(Map<StringObjectvaluesObject sortKeyValue) {
		if (values != null) {
			sm.putAll(values);
		}
		List<ObjectparameterList = new ArrayList<Object>();
		parameterList.addAll(sm.values());
		if (sortKeyValue != null) {
			parameterList.add(sortKeyValue);
		}
			.debug("Using parameterList:" + parameterList);
		}
		return parameterList;
	}
	private class PagingRowMapper implements RowMapper {
		public Object mapRow(ResultSet rsint rowNumthrows SQLException {
			return .mapRow(rsrowNum);
		}
	}
New to GrepCode? Check out our FAQ X