Author: tomdz Date: Sun Jan 14 15:15:01 2007 New Revision: 496186 URL: http://svn.apache.org/viewvc?view=rev&rev=496186 Log: Reworked the deferral and insertion of deferred beans
Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataToDatabaseSink.java Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataToDatabaseSink.java URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataToDatabaseSink.java?view=diff&rev=496186&r1=496185&r2=496186 ============================================================================== --- db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataToDatabaseSink.java (original) +++ db/ddlutils/trunk/src/java/org/apache/ddlutils/io/DataToDatabaseSink.java Sun Jan 14 15:15:01 2007 @@ -334,38 +334,62 @@ insertBeanIntoDatabase(table, bean); + if (_log.isDebugEnabled()) + { + _log.debug("Inserted bean " + origIdentity); + } + if (_ensureFkOrder && _fkTables.contains(table)) { Identity newIdentity = buildIdentityFromPKs(table, bean); ArrayList finishedObjs = new ArrayList(); _identityMap.put(origIdentity, newIdentity); - for (Iterator waitingObjIt = _waitingObjects.iterator(); waitingObjIt.hasNext();) - { - WaitingObject waitingObj = (WaitingObject)waitingObjIt.next(); - Identity fkIdentity = waitingObj.removePendingFK(origIdentity); - if (!waitingObj.hasPendingFKs()) - { - waitingObjIt.remove(); - // the object was only waiting for this one, so store it now - // prior to that we also update the fk fields in case one of the pk - // columns of the target object is auto-incremented by the database - updateFKColumns(waitingObj.getObject(), fkIdentity.getForeignKeyName(), newIdentity); - // we defer handling of the finished objects to avoid concurrent modification exceptions - finishedObjs.add(waitingObj.getObject()); - } - } - for (Iterator finishedObjIt = finishedObjs.iterator(); finishedObjIt.hasNext();) + // we're doing multiple passes so that we can insert as much objects in + // one go as possible + ArrayList identitiesToCheck = new ArrayList(); + + identitiesToCheck.add(origIdentity); + while (!identitiesToCheck.isEmpty() && !_waitingObjects.isEmpty()) { - DynaBean finishedObj = (DynaBean)finishedObjIt.next(); + Identity curIdentity = (Identity)identitiesToCheck.get(0); + Identity curNewIdentity = (Identity)_identityMap.get(curIdentity); - addBean(finishedObj); - if (_log.isDebugEnabled()) + identitiesToCheck.remove(0); + finishedObjs.clear(); + for (Iterator waitingObjIt = _waitingObjects.iterator(); waitingObjIt.hasNext();) { - Table waitingObjTable = ((SqlDynaClass)finishedObj.getDynaClass()).getTable(); + WaitingObject waitingObj = (WaitingObject)waitingObjIt.next(); + Identity fkIdentity = waitingObj.removePendingFK(curIdentity); - _log.debug("Inserted deferred row "+buildIdentityFromPKs(waitingObjTable, finishedObj)); + if (fkIdentity != null) + { + updateFKColumns(waitingObj.getObject(), fkIdentity.getForeignKeyName(), curNewIdentity); + } + if (!waitingObj.hasPendingFKs()) + { + waitingObjIt.remove(); + // we defer handling of the finished objects to avoid concurrent modification exceptions + finishedObjs.add(waitingObj.getObject()); + } + } + for (Iterator finishedObjIt = finishedObjs.iterator(); finishedObjIt.hasNext();) + { + DynaBean finishedObj = (DynaBean)finishedObjIt.next(); + Table tableForObj = _model.getDynaClassFor(finishedObj).getTable(); + Identity objIdentity = buildIdentityFromPKs(tableForObj, finishedObj); + + insertBeanIntoDatabase(tableForObj, finishedObj); + + Identity newObjIdentity = buildIdentityFromPKs(tableForObj, finishedObj); + + _identityMap.put(objIdentity, newObjIdentity); + identitiesToCheck.add(objIdentity); + if (_log.isDebugEnabled()) + { + _log.debug("Inserted deferred row " + objIdentity); + } } } } @@ -492,10 +516,6 @@ if (!_connection.getAutoCommit()) { _connection.commit(); - } - if (_log.isDebugEnabled()) - { - _log.debug("Inserted bean "+buildIdentityFromPKs(table, bean).toString()); } } catch (Exception ex)