|
Spring Framework example source code file (BatchSqlUpdate.java)
The Spring Framework BatchSqlUpdate.java source code
/*
* Copyright 2002-2006 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.jdbc.object;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
/**
* SqlUpdate subclass that performs batch update operations. Encapsulates
* queuing up records to be updated, and adds them as a single batch once
* <code>flush is called or the given batch size has been met.
*
* <p>Note that this class is a non-thread-safe object, in contrast
* to all other JDBC operations objects in this package. You need to create
* a new instance of it for each use, or call <code>reset before
* reuse within the same thread.
*
* @author Keith Donald
* @author Juergen Hoeller
* @since 1.1
* @see #flush
* @see #reset
*/
public class BatchSqlUpdate extends SqlUpdate {
/**
* Default number of inserts to accumulate before commiting a batch (5000).
*/
public static int DEFAULT_BATCH_SIZE = 5000;
private int batchSize = DEFAULT_BATCH_SIZE;
private boolean trackRowsAffected = true;
private final LinkedList parameterQueue = new LinkedList();
private final List rowsAffected = new ArrayList();
/**
* Constructor to allow use as a JavaBean. DataSource and SQL
* must be supplied before compilation and use.
* @see #setDataSource
* @see #setSql
*/
public BatchSqlUpdate() {
super();
}
/**
* Construct an update object with a given DataSource and SQL.
* @param ds DataSource to use to obtain connections
* @param sql SQL statement to execute
*/
public BatchSqlUpdate(DataSource ds, String sql) {
super(ds, sql);
}
/**
* Construct an update object with a given DataSource, SQL
* and anonymous parameters.
* @param ds DataSource to use to obtain connections
* @param sql SQL statement to execute
* @param types SQL types of the parameters, as defined in the
* <code>java.sql.Types class
* @see java.sql.Types
*/
public BatchSqlUpdate(DataSource ds, String sql, int[] types) {
super(ds, sql, types);
}
/**
* Construct an update object with a given DataSource, SQL,
* anonymous parameters and specifying the maximum number of rows
* that may be affected.
* @param ds DataSource to use to obtain connections
* @param sql SQL statement to execute
* @param types SQL types of the parameters, as defined in the
* <code>java.sql.Types class
* @param batchSize the number of statements that will trigger
* an automatic intermediate flush
* @see java.sql.Types
*/
public BatchSqlUpdate(DataSource ds, String sql, int[] types, int batchSize) {
super(ds, sql, types);
setBatchSize(batchSize);
}
/**
* Set the number of statements that will trigger an automatic intermediate
* flush. <code>update calls or the given statement parameters will
* be queued until the batch size is met, at which point it will empty the
* queue and execute the batch.
* <p>You can also flush already queued statements with an explicit
* <code>flush call. Note that you need to this after queueing
* all parameters to guarantee that all statements have been flushed.
*/
public void setBatchSize(int batchSize) {
this.batchSize = batchSize;
}
/**
* Set whether to track the rows affected by batch updates performed
* by this operation object.
* <p>Default is "true". Turn this off to save the memory needed for
* the list of row counts.
* @see #getRowsAffected()
*/
public void setTrackRowsAffected(boolean trackRowsAffected) {
this.trackRowsAffected = trackRowsAffected;
}
/**
* BatchSqlUpdate does not support BLOB or CLOB parameters.
*/
protected boolean supportsLobParameters() {
return false;
}
/**
* Overridden version of <code>update that adds the given statement
* parameters to the queue rather than executing them immediately.
* All other <code>update methods of the SqlUpdate base class go
* through this method and will thus behave similarly.
* <p>You need to call
Other Spring Framework examples (source code examples)Here is a short list of links related to this Spring Framework BatchSqlUpdate.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.