Search in sources :

Example 6 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class UnitOfWorkImpl method commitToDatabase.

/**
 * INTERNAL:
 * CommitChanges To The Database from a calculated changeSet
 * @param commitTransaction false if called by writeChanges as intent is
 * not to finalize the transaction.
 */
protected void commitToDatabase(boolean commitTransaction) {
    try {
        // CR4202 - ported from 3.6.4
        if (wasTransactionBegunPrematurely()) {
            // beginTransaction() has been already called
            setWasTransactionBegunPrematurely(false);
        } else {
            beginTransaction();
        }
        if (commitTransaction) {
            setWasNonObjectLevelModifyQueryExecuted(false);
        }
        this.preDeleteComplete = false;
        // PERF: Avoid deletion if nothing to delete.
        List deletedObjects = null;
        if (hasDeletedObjects()) {
            deletedObjects = new ArrayList(this.deletedObjects.size());
            for (Object objectToDelete : this.deletedObjects.keySet()) {
                ClassDescriptor descriptor = getDescriptor(objectToDelete);
                if (descriptor.hasPreDeleteMappings()) {
                    for (DatabaseMapping mapping : descriptor.getPreDeleteMappings()) {
                        DeleteObjectQuery deleteQuery = descriptor.getQueryManager().getDeleteQuery();
                        if (deleteQuery == null) {
                            deleteQuery = new DeleteObjectQuery();
                            deleteQuery.setDescriptor(descriptor);
                        } else {
                            // Ensure original query has been prepared.
                            deleteQuery.checkPrepare(this, deleteQuery.getTranslationRow());
                            deleteQuery = (DeleteObjectQuery) deleteQuery.clone();
                        }
                        deleteQuery.setIsExecutionClone(true);
                        deleteQuery.setTranslationRow(new DatabaseRecord());
                        deleteQuery.setObject(objectToDelete);
                        deleteQuery.setSession(this);
                        mapping.earlyPreDelete(deleteQuery, objectToDelete);
                    }
                }
                deletedObjects.add(objectToDelete);
            }
            this.preDeleteComplete = true;
        }
        if (this.shouldPerformDeletesFirst) {
            if (deletedObjects != null) {
                // This must go to the commit manager because uow overrides to do normal deletion.
                getCommitManager().deleteAllObjects(deletedObjects);
                // Clear change sets of the deleted object to avoid redundant updates.
                for (Iterator objects = getObjectsDeletedDuringCommit().keySet().iterator(); objects.hasNext(); ) {
                    org.eclipse.persistence.internal.sessions.ObjectChangeSet objectChangeSet = (org.eclipse.persistence.internal.sessions.ObjectChangeSet) this.unitOfWorkChangeSet.getObjectChangeSetForClone(objects.next());
                    if (objectChangeSet != null) {
                        objectChangeSet.clear(true);
                    }
                }
            }
            // Let the commit manager figure out how to write the objects
            super.writeAllObjectsWithChangeSet(this.unitOfWorkChangeSet);
            // Issue all the SQL for the ModifyAllQuery's, don't touch the cache though
            issueModifyAllQueryList();
        } else {
            // Let the commit manager figure out how to write the objects
            super.writeAllObjectsWithChangeSet(this.unitOfWorkChangeSet);
            if (deletedObjects != null) {
                // This must go to the commit manager because uow overrides to do normal deletion.
                getCommitManager().deleteAllObjects(deletedObjects);
            }
            // Issue all the SQL for the ModifyAllQuery's, don't touch the cache though
            issueModifyAllQueryList();
        }
        // Issue prepare event.
        if (this.eventManager != null) {
            this.eventManager.prepareUnitOfWork();
        }
        // do not lock objects unless we are at the commit stage
        if (commitTransaction) {
            try {
                acquireWriteLocks();
                commitTransaction();
            } catch (RuntimeException throwable) {
                releaseWriteLocks();
                throw throwable;
            } catch (Error throwable) {
                releaseWriteLocks();
                throw throwable;
            }
        } else {
            setWasTransactionBegunPrematurely(true);
        // must let the UnitOfWork know that the transaction was begun
        // before the commit process.
        }
    } catch (RuntimeException exception) {
        // The number of SQL statements been prepared need be stored into UOW
        // before any exception being thrown.
        copyStatementsCountIntoProperties();
        try {
            rollbackTransaction(commitTransaction);
        } catch (RuntimeException ignore) {
        // Ignore
        }
        if (hasExceptionHandler()) {
            getExceptionHandler().handleException(exception);
        } else {
            throw exception;
        }
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) ArrayList(java.util.ArrayList) DeleteObjectQuery(org.eclipse.persistence.queries.DeleteObjectQuery) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) Iterator(java.util.Iterator) DescriptorIterator(org.eclipse.persistence.internal.descriptors.DescriptorIterator) List(java.util.List) ArrayList(java.util.ArrayList)

Example 7 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class IllegalArgumentWhileInvokingAttributeMethodTest method setup.

@Override
protected void setup() {
    descriptor = new RelationalDescriptor();
    descriptor.setJavaClass(IllegalArgumentWhileInvokingAttributeMethodTest.class);
    descriptor.addTableName("EMPLOYEE");
    mapping = new TransformationMapping();
    mapping.setAttributeName("normalHours");
    mapping.setAttributeTransformation("invalidMethod");
    descriptor.addMapping(mapping);
    mapping.initialize((AbstractSession) getSession());
    row = new DatabaseRecord();
    expectedException = DescriptorException.illegalArgumentWhileInvokingAttributeMethod(mapping, new Exception());
}
Also used : RelationalDescriptor(org.eclipse.persistence.descriptors.RelationalDescriptor) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) TransformationMapping(org.eclipse.persistence.mappings.TransformationMapping) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException)

Example 8 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class IndirectSetTest method setUp.

/**
 * set up the test fixture:
 */
@Before
public void setUp() {
    list = setUpList();
    Object temp = new HashSet<>(list);
    ValueHolderInterface vh = new QueryBasedValueHolder(new ReadAllQuery(), new DatabaseRecord(), new TestSession(temp));
    if (cls == null) {
        testList = IndirectCollectionsFactory.createIndirectSet();
    } else {
        try {
            testList = cls.getConstructor().newInstance();
        } catch (ReflectiveOperationException e) {
            throw new RuntimeException(e);
        }
    }
    testList.setValueHolder(vh);
    if (useListener) {
        testListLsn = new Listener();
        testList._persistence_setPropertyChangeListener(testListLsn);
    }
}
Also used : PropertyChangeListener(java.beans.PropertyChangeListener) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) ValueHolderInterface(org.eclipse.persistence.indirection.ValueHolderInterface) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) HashSet(java.util.HashSet) QueryBasedValueHolder(org.eclipse.persistence.internal.indirection.QueryBasedValueHolder) Before(org.junit.Before)

Example 9 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class IndexedInteraction method buildRow.

/**
 * Build a database row from the record returned from the interaction.
 * Also handles MappedRecords for case of input being indexed but mapped ouput.
 */
@Override
public AbstractRecord buildRow(jakarta.resource.cci.Record record, EISAccessor accessor) {
    AbstractRecord row = null;
    if (record instanceof IndexedRecord) {
        IndexedRecord indexedRecord = (IndexedRecord) record;
        row = new DatabaseRecord(indexedRecord.size());
        for (int index = 0; index < indexedRecord.size(); index++) {
            DatabaseField field = getOutputArguments().get(index);
            row.put(field, indexedRecord.get(index));
        }
    } else if (record instanceof MappedRecord) {
        MappedRecord mappedRecord = (MappedRecord) record;
        // Handle the case of a single output argument of the entire row contained within the return record.
        if (getOutputArgumentNames().size() == 1) {
            mappedRecord = (MappedRecord) mappedRecord.get(getOutputArgumentNames().get(0));
        // Handle the case were the output row is mapped into a database row of values.
        } else if (getOutputArgumentNames().size() > 1) {
            row = new DatabaseRecord(getOutputArgumentNames().size());
            for (int index = 0; index < getOutputArgumentNames().size(); index++) {
                DatabaseField field = getOutputArguments().get(index);
                row.put(field, mappedRecord.get(getOutputArgumentNames().get(index)));
            }
            return row;
        }
        // Wrapped the record in a database to avoid loosing any information in conversion to database row,
        // also gets around problem of some adatpers not supporting keySet or entrySet.
        row = new EISMappedRecord(mappedRecord, accessor);
    } else {
        row = new DatabaseRecord(1);
        row.put(getOutputResultPath(), record);
    }
    return row;
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord)

Example 10 with DatabaseRecord

use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.

the class StoredProcedureVARRAYParametersTest method verify.

@Override
public void verify() {
    if ((results2.size() != 1) && (results3.size() != 1)) {
        throw new TestErrorException("Collections returned did not contain expected number of results");
    }
    // check that a Struct was returned for address
    Object addressOut = ((DatabaseRecord) results2.get(0)).get("address");
    if ((addressOut == null) || (!(addressOut instanceof Struct))) {
        throw new TestErrorException("Address returned by SProc_Read_PHolders was null or not a Struct :" + addressOut);
    }
    // This is an Address instance instead of struct since it picks up the address class from the value passed in.
    Address addressInOut = ((Address) ((DatabaseRecord) results3.get(0)).get("address"));
    if ((addressInOut == null) || !originalAddress.getStreet().equals(addressInOut.getStreet())) {
        throw new TestErrorException("Address in did not equal the address returned out by SProc_Delete_PHolders " + addressInOut);
    }
    Vector childrenRead = ((Vector) ((DatabaseRecord) results2.get(0)).get("childrenNames"));
    if ((childrenNames.size() != childrenRead.size()) && (!childrenNames.get(0).equals(childrenRead.get(0)))) {
        throw new TestErrorException("First Child's Name did not match what was returned out by SProc_Read_PHolders");
    }
    childrenRead = ((Vector) ((DatabaseRecord) results3.get(0)).get("childrenNames"));
    if ((childrenNames.size() != childrenRead.size()) && (!childrenNames.get(0).equals(childrenRead.get(0)))) {
        throw new TestErrorException("First Child's Name did not match what was returned out by SProc_Delete_PHolders");
    }
    Vector phonesRead = ((Vector) ((DatabaseRecord) results2.get(0)).get("phones"));
    if ((phoneNumbers.size() != phonesRead.size()) && (!phoneNumbers.get(0).equals(childrenRead.get(0)))) {
        throw new TestErrorException("First phone did not match what was returned out by SProc_Read_PHolders");
    }
    phonesRead = ((Vector) ((DatabaseRecord) results3.get(0)).get("phones"));
    if ((phoneNumbers.size() != phonesRead.size()) && (!phoneNumbers.get(0).equals(phonesRead.get(0)))) {
        throw new TestErrorException("First phone did not match what was returned out by SProc_Delete_PHolders");
    }
}
Also used : DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) Address(org.eclipse.persistence.testing.models.insurance.Address) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) Vector(java.util.Vector) Struct(java.sql.Struct)

Aggregations

DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)110 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)44 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)43 Vector (java.util.Vector)31 ArrayList (java.util.ArrayList)17 NonSynchronizedVector (org.eclipse.persistence.internal.helper.NonSynchronizedVector)17 BigDecimal (java.math.BigDecimal)14 List (java.util.List)14 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)14 DatabaseSession (org.eclipse.persistence.sessions.DatabaseSession)14 Test (org.junit.Test)14 Expression (org.eclipse.persistence.expressions.Expression)12 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)12 HashMap (java.util.HashMap)11 Map (java.util.Map)10 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)7 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)7 RelationalDescriptor (org.eclipse.persistence.descriptors.RelationalDescriptor)6 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)6 SQLUpdateStatement (org.eclipse.persistence.internal.expressions.SQLUpdateStatement)6