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

📄 batchingbatcher.java

📁 用Java实现的23个常用设计模式源代码
💻 JAVA
字号:
//$Id: BatchingBatcher.java,v 1.4.2.6 2003/11/27 16:09:59 oneovthafew Exp $package net.sf.hibernate.impl;import java.sql.PreparedStatement;import java.sql.SQLException;import net.sf.hibernate.HibernateException;import net.sf.hibernate.engine.SessionImplementor;import net.sf.hibernate.util.JDBCExceptionReporter;/** * An implementation of the <tt>Batcher</tt> interface that  * actually uses batching * @author Gavin King */public class BatchingBatcher extends BatcherImpl {		private int batchSize;	private int[] expectedRowCounts;		public BatchingBatcher(SessionImplementor session) {		super(session);		expectedRowCounts = new int[ getFactory().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().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 ( log.isDebugEnabled() )		log.debug("Executing batch size: " + batchSize );				try {			if (batchSize!=0) {				final int[] results = ps.executeBatch();				// check return codes				for ( int i=0; i<batchSize; i++ ) {					if ( results[i]==-2 ) {						if ( log.isDebugEnabled() ) log.debug("success of batch update unknown: " + i);					}					else if ( results[i]==-3 ) {						throw new HibernateException("Batch update failed: " + i);					}					else {						if ( expectedRowCounts[i]>=0 && results[i]!=expectedRowCounts[i] ) {							throw new HibernateException("Batch update row count wrong: " + i);						}					}				}			}		}		catch(SQLException sqle) {			JDBCExceptionReporter.logExceptions(sqle);			throw sqle;		}		catch(RuntimeException re) {			log.error("Exception executing batch: ", re);			throw re;		}		finally {			batchSize=0;			//ps.clearBatch();		}	}			}

⌨️ 快捷键说明

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