【Java开源代码栏目提醒】:网学会员鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“BatcherImpl.java”一文,供大家参考学习
//$Id: BatcherImpl.java,v 1.9.2.17 2004/01/20 14:32:12 oneovthafew Exp $
package net.sf.hibernate.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.hibernate.AssertionFailure;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.JDBCException;
import net.sf.hibernate.engine.Batcher;
import net.sf.hibernate.engine.SessionFactoryImplementor;
import net.sf.hibernate.engine.SessionImplementor;
import net.sf.hibernate.util.GetGeneratedKeysHelper;
import net.sf.hibernate.util.JDBCExceptionReporter;
/**
* Manages prepared statements and batching.
*
* @author Gavin King
*/
public abstract class BatcherImpl implements Batcher {
private static int openPreparedStatements;
private static int openResultSetCount;
protected static final Log log = LogFactory.getLog(BatcherImpl.class);
protected static final Log sqlLog = LogFactory.getLog("net.sf.hibernate.SQL");
private final SessionImplementor session;
private final SessionFactoryImplementor factory;
private PreparedStatement batchUpdate;
private String batchUpdateSQL;
private HashSet statementsToClose = new HashSet();
private HashSet resultSetsToClose = new HashSet();
private PreparedStatement lastQuery;
public BatcherImpl(SessionImplementor session) {
this.session = session;
this.factory = session.getFactory();
}
protected PreparedStatement getStatement() {
return batchUpdate;
}
public PreparedStatement prepareStatement(String sql) throws SQLException, HibernateException {
return prepareStatement(sql, false);
}
public PreparedStatement prepareStatement(String sql, boolean getGeneratedKeys) throws SQLException, HibernateException {
executeBatch();
logOpenPreparedStatement();
return getPreparedStatement( session.connection(), sql, false, getGeneratedKeys );
}
public PreparedStatement prepareQueryStatement(String sql, boolean scrollable) throws SQLException, HibernateException {
logOpenPreparedStatement();
PreparedStatement ps = getPreparedStatement( session.connection(), sql, scrollable );
setStatementFetchSize(ps);
statementsToClose.add(ps);
lastQuery=ps;
return ps;
}
public void abortBatch(SQLException sqle) {
//JDBCExceptionReporter.logExceptions(sqle);
final PreparedStatement ps = batchUpdate;
batchUpdate=null;
batchUpdateSQL=null;
try {
closeStatement(ps);
}
catch (SQLException e) {
//noncritical, swallow and let the other propagate!
JDBCExceptionReporter.logExceptions(e);
}
}
public ResultSet getResultSet(PreparedStatement ps) throws SQLException {
ResultSet rs = ps.executeQuery();
resultSetsToClose.add(rs);
logOpenResults();
return rs;
}
public void closeQueryStatement(PreparedStatement ps, ResultSet rs) throws SQLException {
statementsToClose.remove(ps);
if (rs!=null) resultSetsToClose.remove(rs);
try {
if (rs!=null) {
logCloseResults();
rs.close();
}
}
finally {
closeQueryStatement(ps);
}
}
public PreparedStatement prepareBatchStatement(String sql) throws SQLException, HibernateException {
if ( !sql.equals(batchUpdateSQL) ) {
batchUpdate=prepareStatement(sql); // calls executeBatch()
batchUpdateSQL=sql;
}
return batchUpdate;
}
public void executeBatch() throws HibernateException {
if (batchUpdate!=null) {
final PreparedStatement ps = batchUpdate;
batchUpdate=null;
batchUpdateSQL=null;
try {
try {
doExecuteBatch(ps);
}
finally {
closeStatement(ps);
}
}
catch (SQLException sqle) {
throw new JDBCException("Could not execute JDBC batch update",
sqle);
}
}
}
public void closeStatement(PreparedStatement ps) throws SQLException {
logClosePreparedStatement();
closePreparedStatement(ps);
}
private void closeQueryStatement(PreparedStatement ps) throws SQLException {
try {
//work around a bug in all known connection pools....
if ( ps.getMaxRows()!=0 ) ps