BATCH-965: renamed batch writer to JdbcBatchItemWriter and added named parameter support, added JPA and Hibernate writers

This commit is contained in:
trisberg
2008-12-11 17:46:59 +00:00
parent d025bb6dc1
commit 91a0df5ba7
10 changed files with 1023 additions and 156 deletions

View File

@@ -1,140 +0,0 @@
/*
* Copyright 2006-2007 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.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.util.Assert;
/**
* {@link ItemWriter} that uses the batching features from
* {@link PreparedStatement} if available and can take some rudimentary steps to
* locate a failure during a flush, and identify the items that failed. When one
* of those items is encountered again the batch is flushed aggressively so that
* the bad item is eventually identified and can be dealt with in
* isolation.<br/>
*
* The user must provide an SQL query and a special callback
* {@link ItemPreparedStatementSetter}, which is responsible for mapping the
* item to a PreparedStatement.<br/>
*
* It is expected that {@link #write(List)} is called inside a transaction.<br/>
*
* The writer is thread safe after its properties are set (normal singleton
* behaviour), so it can be used to write in multiple concurrent transactions.
* Note, however, that the set of failed items is stored in a collection
* internally, and this collection is never cleared, so it is not a great idea
* to go on using the writer indefinitely. Normally it would be used for the
* duration of a batch job and then discarded.
*
* @author Dave Syer
*
*/
public class BatchSqlUpdateItemWriter<T> implements ItemWriter<T>, InitializingBean {
private JdbcOperations jdbcTemplate;
private ItemPreparedStatementSetter<T> preparedStatementSetter;
private String sql;
private boolean assertUpdates = true;
/**
* Public setter for the flag that determines whether an assertion is made
* that all items cause at least one row to be updated.
* @param assertUpdates the flag to set. Defaults to true;
*/
public void setAssertUpdates(boolean assertUpdates) {
this.assertUpdates = assertUpdates;
}
/**
* Public setter for the query string to execute on write. The parameters
* should correspond to those known to the
* {@link ItemPreparedStatementSetter}.
* @param sql the query to set
*/
public void setSql(String sql) {
this.sql = sql;
}
/**
* Public setter for the {@link ItemPreparedStatementSetter}.
* @param preparedStatementSetter the {@link ItemPreparedStatementSetter} to
* set
*/
public void setItemPreparedStatementSetter(ItemPreparedStatementSetter<T> preparedStatementSetter) {
this.preparedStatementSetter = preparedStatementSetter;
}
/**
* Public setter for the {@link JdbcOperations}.
* @param jdbcTemplate the {@link JdbcOperations} to set
*/
public void setJdbcTemplate(JdbcOperations jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
* Check mandatory properties - there must be a delegate.
*/
public void afterPropertiesSet() throws Exception {
Assert.notNull(jdbcTemplate, "BatchSqlUpdateItemWriter requires an data source.");
Assert.notNull(preparedStatementSetter, "BatchSqlUpdateItemWriter requires a ItemPreparedStatementSetter");
}
/* (non-Javadoc)
* @see org.springframework.batch.item.ItemWriter#write(java.util.List)
*/
public void write(final List<? extends T> items) throws Exception {
if (!items.isEmpty()) {
int[] values = (int[]) jdbcTemplate.execute(sql, new PreparedStatementCallback() {
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
for (T item : items) {
preparedStatementSetter.setValues(item, ps);
ps.addBatch();
}
return ps.executeBatch();
}
});
if (assertUpdates) {
for (int i = 0; i < values.length; i++) {
int value = values[i];
if (value == 0) {
throw new EmptyResultDataAccessException("Item " + i + " of " + values.length
+ " did not update any rows: [" + items.get(i) + "]", 1);
}
}
}
}
}
}

View File

@@ -0,0 +1,39 @@
/*
* 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 org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
/**
* A convenient implementation for providing BeanPropertySqlParameterSource when the item has JavaBean properties
* that correspond to names used for parameters in the SQL statement.
*
* @author Thomas Risberg
* @since 2.0
*/
public class BeanPropertyItemSqlParameterSourceProvider<T> implements ItemSqlParameterSourceProvider<T> {
/**
* Provide parameter values in an {@link BeanPropertySqlParameterSource} based on values from
* the provided item.
* @param item the item to use for parameter values
*/
public SqlParameterSource createSqlParameterSource(T item) {
return new BeanPropertySqlParameterSource(item);
}
}

View File

@@ -0,0 +1,115 @@
/*
* Copyright 2006-2007 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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.orm.hibernate3.HibernateOperations;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.util.Assert;
/**
* {@link ItemWriter} that uses a Hibernate session to save or update entities
* that are not part of the current Hibernate session. It will also flush
* and clear the session at chunk boundaries.<br/><br/>
*
* The writer is thread safe after its properties are set (normal singleton
* behaviour), so it can be used to write in multiple concurrent transactions.
*
* @author Dave Syer
* @author Thomas Risberg
*
*/
public class HibernateItemWriter<T> implements ItemWriter<T>, InitializingBean {
protected static final Log logger = LogFactory.getLog(HibernateItemWriter.class);
private HibernateOperations hibernateTemplate;
/**
* Public setter for the {@link HibernateOperations} property.
*
* @param hibernateTemplate the hibernateTemplate to set
*/
public void setHibernateTemplate(HibernateOperations hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
/**
* Set the Hibernate SessionFactory to be used internally. Will
* automatically create a HibernateTemplate for the given SessionFactory.
*
* @see #setHibernateTemplate
*/
public final void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
/**
* Check mandatory properties - there must be a hibernateTemplate.
*/
public void afterPropertiesSet() throws Exception {
Assert.notNull(hibernateTemplate, "HibernateItemWriter requires a HibernateOperations");
}
/**
* Save or update any entities not in the current hibernate session and then flush and
* clear the hibernate session.
*
* @see org.springframework.batch.item.ItemWriter#write(java.util.List)
*/
public final void write(List<? extends T> items) throws Exception {
doWrite(hibernateTemplate, items);
try {
hibernateTemplate.flush();
}
finally {
// This should happen when the transaction commits anyway, but to be
// sure...
hibernateTemplate.clear();
}
}
/**
* Do perform the actual write operation. This can be overridden in a subclass if necessary.
*
* @param hibernateTemplate the HibernateTemplate to use for the operation
* @param items the list of items to use for the write
*/
protected void doWrite(HibernateOperations hibernateTemplate, List<? extends T> items) {
if (!items.isEmpty()) {
long saveOrUpdateCount = 0;
for (T item : items) {
if (!hibernateTemplate.contains(item)) {
hibernateTemplate.saveOrUpdate(item);
saveOrUpdateCount++;
}
}
if (logger.isDebugEnabled()) {
logger.debug(saveOrUpdateCount + " entities saved/updated.");
logger.debug((items.size() - saveOrUpdateCount) + " entities found in session.");
}
}
}
}

View File

@@ -0,0 +1,35 @@
/*
* 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 org.springframework.jdbc.core.namedparam.SqlParameterSource;
/**
* A convenient strategy for providing SqlParameterSource for named parameter SQL updates.
*
* @author Thomas Risberg
* @since 2.0
*/
public interface ItemSqlParameterSourceProvider<T> {
/**
* Provide parameter values in an {@link SqlParameterSource} based on values from
* the provided item.
* @param item the item to use for parameter values
*/
SqlParameterSource createSqlParameterSource(T item);
}

View File

@@ -0,0 +1,205 @@
/*
* 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.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.support.JdbcParameterUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcOperations;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.util.Assert;
/**
* {@link ItemWriter} that uses the batching features from
* {@link SimpleJdbcTemplate} to execute a batch of statements for all items
* provided.<br/>
*
* The user must provide an SQL query and a special callback in the for of either
* {@link ItemPreparedStatementSetter}, or a {@link ItemSqlParameterSourceProvider}.
* You can use either named parameters or the traditional '?' placeholders. If you use the
* named parameter support then you should provide a {@link ItemSqlParameterSourceProvider},
* otherwise you should provide a {@link ItemPreparedStatementSetter}.
* This callback would be responsible for mapping the item to the parameters needed to
* execute the SQL statement.<br/>
*
* It is expected that {@link #write(List)} is called inside a transaction.<br/>
*
* The writer is thread safe after its properties are set (normal singleton
* behaviour), so it can be used to write in multiple concurrent transactions.
*
* @author Dave Syer
* @author Thomas Risberg
* @since 2.0
*/
public class JdbcBatchItemWriter<T> implements ItemWriter<T>, InitializingBean {
protected static final Log logger = LogFactory.getLog(JdbcBatchItemWriter.class);
private SimpleJdbcOperations simpleJdbcTemplate;
private ItemPreparedStatementSetter<T> itemPreparedStatementSetter;
private ItemSqlParameterSourceProvider<T> itemSqlParameterSourceProvider;
private String sql;
private boolean assertUpdates = true;
private int parameterCount;
private boolean usingNamedParameters;
/**
* Public setter for the flag that determines whether an assertion is made
* that all items cause at least one row to be updated.
* @param assertUpdates the flag to set. Defaults to true;
*/
public void setAssertUpdates(boolean assertUpdates) {
this.assertUpdates = assertUpdates;
}
/**
* Public setter for the query string to execute on write. The parameters
* should correspond to those known to the
* {@link ItemPreparedStatementSetter}.
* @param sql the query to set
*/
public void setSql(String sql) {
this.sql = sql;
}
/**
* Public setter for the {@link ItemPreparedStatementSetter}.
* @param preparedStatementSetter the {@link ItemPreparedStatementSetter} to
* set. This is required when using traditional '?' placeholders for the SQL statement.
*/
public void setItemPreparedStatementSetter(ItemPreparedStatementSetter<T> preparedStatementSetter) {
this.itemPreparedStatementSetter = preparedStatementSetter;
}
/**
* Public setter for the {@link ItemSqlParameterSourceProvider}.
* @param itemSqlParameterSourceProvider the {@link ItemSqlParameterSourceProvider} to
* set. This is required when using named parameters for the SQL statement.
*/
public void setItemSqlParameterSourceProvider(ItemSqlParameterSourceProvider<T> itemSqlParameterSourceProvider) {
this.itemSqlParameterSourceProvider = itemSqlParameterSourceProvider;
}
/**
* Public setter for the data source for injection purposes.
*
* @param dataSource
*/
public void setDataSource(DataSource dataSource) {
if (simpleJdbcTemplate == null) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
}
/**
* Public setter for the {@link JdbcOperations}.
* @param simpleJdbcTemplate the {@link JdbcOperations} to set
*/
public void setSimpleJdbcTemplate(SimpleJdbcOperations simpleJdbcTemplate) {
this.simpleJdbcTemplate = simpleJdbcTemplate;
}
/**
* Check mandatory properties - there must be a delegate.
*/
public void afterPropertiesSet() throws Exception {
Assert.notNull(simpleJdbcTemplate, "A DataSource or a SimpleJdbcTemplate is required.");
Assert.notNull(sql, "An SQL statement is required.");
List<String> namedParameters = new ArrayList<String>();
parameterCount = JdbcParameterUtils.countParameterPlaceholders(sql, namedParameters);
if (namedParameters.size() > 0) {
if (parameterCount != namedParameters.size()) {
throw new InvalidDataAccessApiUsageException("You can't use both named parameters and classic \"?\" placeholders: " + sql);
}
usingNamedParameters = true;
}
if (usingNamedParameters) {
Assert.notNull(itemSqlParameterSourceProvider, "Using SQL statement with named parameters requires an ItemSqlParameterSourceProvider");
}
else {
Assert.notNull(itemPreparedStatementSetter, "Using SQL statement with '?' placeholders requires an ItemPreparedStatementSetter");
}
}
/* (non-Javadoc)
* @see org.springframework.batch.item.ItemWriter#write(java.util.List)
*/
public void write(final List<? extends T> items) throws Exception {
if (!items.isEmpty()) {
if (logger.isDebugEnabled()) {
logger.debug("Executing batch with " + items.size() + " items.");
}
int[] values = null;
if (usingNamedParameters) {
SqlParameterSource[] batchArgs = new SqlParameterSource[items.size()];
int i = 0;
for (T item : items) {
batchArgs[i++] = itemSqlParameterSourceProvider.createSqlParameterSource(item);
}
values = simpleJdbcTemplate.batchUpdate(sql, batchArgs);
}
else {
values = (int[]) simpleJdbcTemplate.getJdbcOperations().execute(sql, new PreparedStatementCallback() {
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
for (T item : items) {
itemPreparedStatementSetter.setValues(item, ps);
ps.addBatch();
}
return ps.executeBatch();
}
});
}
if (assertUpdates) {
for (int i = 0; i < values.length; i++) {
int value = values[i];
if (value == 0) {
throw new EmptyResultDataAccessException("Item " + i + " of " + values.length
+ " did not update any rows: [" + items.get(i) + "]", 1);
}
}
}
}
}
}

View File

@@ -0,0 +1,115 @@
/*
* 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 javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.jpa.EntityManagerFactoryUtils;
import org.springframework.util.Assert;
/**
* {@link org.springframework.batch.item.ItemWriter} that is using a JPA
* EntityManagerFactory to merge any Entities that aren't part of the
* persistence context.
*
* It is required that {@link #write(List)} is called inside a transaction.<br/>
*
* The reader must be configured with an
* {@link javax.persistence.EntityManagerFactory} that is capable of
* participating in Spring managed transactions.
*
* The writer is thread safe after its properties are set (normal singleton
* behaviour), so it can be used to write in multiple concurrent transactions.
*
* @author Thomas Risberg
*
*/
public class JpaItemWriter<T> implements ItemWriter<T>, InitializingBean {
protected static final Log logger = LogFactory.getLog(JpaItemWriter.class);
private EntityManagerFactory entityManagerFactory;
/**
* Set the EntityManager to be used internally.
*
* @param entityManagerFactory the entityManagerFactory to set
*/
public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}
/**
* Check mandatory properties - there must be an entityManagerFactory.
*/
public void afterPropertiesSet() throws Exception {
Assert.notNull(entityManagerFactory, "An EntityManagerFactory is required");
}
/**
* Merge all provided items that aren't already in the persistence context
* and then flush and clear the entity manager.
*
* @see org.springframework.batch.item.ItemWriter#write(java.util.List)
*/
public final void write(List<? extends T> items) throws Exception {
EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory);
if (entityManager == null) {
throw new DataAccessResourceFailureException("Unable to obtain a transactional EntityManager");
}
doWrite(entityManager, items);
try {
entityManager.flush();
}
finally {
entityManager.clear();
}
}
/**
* Do perform the actual write operation. This can be overridden in a subclass if necessary.
*
* @param entityManager the EntityManager to use for the operation
* @param items the list of items to use for the write
*/
protected void doWrite(EntityManager entityManager, List<? extends T> items) {
if (!items.isEmpty()) {
long mergeCount = 0;
for (T item : items) {
if (!entityManager.contains(item)) {
entityManager.merge(item);
mergeCount++;
}
}
if (logger.isDebugEnabled()) {
logger.debug(mergeCount + " entities merged.");
logger.debug((items.size() - mergeCount) + " entities found in persistence context.");
}
}
}
}