⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 batchingbatcher.java

📁 hibernate-3.1.3-all-src.zip 面向对象的访问数据库工具
💻 JAVA
字号:
//$Id: BatchingBatcher.java 7683 2005-07-29 19:10:20Z maxcsaucdk $
package org.hibernate.jdbc;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.StaleStateException;

/**
 * An implementation of the <tt>Batcher</tt> interface that
 * actually uses batching
 * @author Gavin King
 */
public class BatchingBatcher extends AbstractBatcher {

	private int batchSize;
	private int[] expectedRowCounts;
	
	public BatchingBatcher(ConnectionManager connectionManager, Interceptor interceptor) {
		super( connectionManager, interceptor );		
		expectedRowCounts = new int[ getFactory().getSettings().getJdbcBatchSize() ];
	}

	public void addToBatch(int expectedRowCount) throws SQLException, HibernateException {

		log.trace("Adding to batch");
		PreparedStatement batchUpdate = getStatement();
		batchUpdate.addBatch();
		expectedRowCounts[ batchSize++ ] = expectedRowCount;
		if ( batchSize==getFactory().getSettings().getJdbcBatchSize() ) {
			//try {
				doExecuteBatch(batchUpdate);
			/*}
			catch (SQLException sqle) {
				closeStatement(batchUpdate);
				throw sqle;
			}
			catch (HibernateException he) {
				closeStatement(batchUpdate);
				throw he;
			}*/
		}

	}

	protected void doExecuteBatch(PreparedStatement ps) throws SQLException, HibernateException {
		
		if (batchSize==0) {
			log.debug("no batched statements to execute");
		}
		else {
		
			if ( log.isDebugEnabled() ) log.debug("Executing batch size: " + batchSize );
	
			try {
				checkRowCounts( ps.executeBatch() );
			}
			catch (RuntimeException re) {
				log.error("Exception executing batch: ", re);
				throw re;
			}
			finally {
				batchSize=0;
				//ps.clearBatch();
			}
			
		}

	}

	private void checkRowCounts(int[] rowCounts) {
		int rowCountLength = rowCounts.length;
		if ( rowCountLength!=batchSize ) {
			log.warn("JDBC driver did not return the expected number of row counts");
		}
		for ( int i=0; i<rowCountLength; i++ ) {
			checkRowCount( rowCounts[i], expectedRowCounts[i], i );
		}
	}
	
	private void checkRowCount(int rowCount, int expectedRowCount, int i) {
		if ( rowCount==-2 ) {
			if ( log.isDebugEnabled() ) log.debug("success of batch update unknown: " + i);
		}
		else if ( rowCount==-3 ) {
			throw new HibernateException("Batch update failed: " + i);
		}
		else {
			if ( expectedRowCount>=0 ) {
				if ( rowCount<expectedRowCount ) {
					throw new StaleStateException(
							"Batch update returned unexpected row count from update: " + i +
							" actual row count: " + rowCount +
							" expected: " + expectedRowCount
					);
				}
				if ( rowCount>expectedRowCount ) {
					throw new HibernateException(
							"Batch update returned unexpected row count from update: " + i +
							" actual row count: " + rowCount +
							" expected: " + expectedRowCount
					);
				}
			}
		}
	}

}






⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -