Search in sources :

Example 16 with MappedDatastoreException

use of org.datanucleus.store.rdbms.exceptions.MappedDatastoreException in project datanucleus-rdbms by datanucleus.

the class AbstractArrayStore method set.

/**
 * Method to set the array for the specified owner to the passed value.
 * @param op ObjectProvider for the owner
 * @param array the array
 * @return Whether the array was updated successfully
 */
public boolean set(ObjectProvider op, Object array) {
    if (array == null || Array.getLength(array) == 0) {
        return true;
    }
    // Validate all elements for writing
    ExecutionContext ec = op.getExecutionContext();
    int length = Array.getLength(array);
    for (int i = 0; i < length; i++) {
        Object obj = Array.get(array, i);
        validateElementForWriting(ec, obj, null);
    }
    boolean modified = false;
    List exceptions = new ArrayList();
    boolean batched = allowsBatching() && length > 1;
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        try {
            processBatchedWrites(mconn);
            // Loop through all elements to be added
            E element = null;
            for (int i = 0; i < length; i++) {
                element = (E) Array.get(array, i);
                try {
                    // Add the row to the join table
                    int[] rc = internalAdd(op, element, mconn, batched, i, (i == length - 1));
                    if (rc != null) {
                        for (int j = 0; j < rc.length; j++) {
                            if (rc[j] > 0) {
                                // At least one record was inserted
                                modified = true;
                            }
                        }
                    }
                } catch (MappedDatastoreException mde) {
                    exceptions.add(mde);
                    NucleusLogger.DATASTORE.error("Exception thrown in set of element", mde);
                }
            }
        } finally {
            mconn.release();
        }
    } catch (MappedDatastoreException e) {
        exceptions.add(e);
        NucleusLogger.DATASTORE.error("Exception thrown in set of element", e);
    }
    if (!exceptions.isEmpty()) {
        // Throw all exceptions received as the cause of a NucleusDataStoreException so the user can see which
        // record(s) didn't persist
        String msg = Localiser.msg("056009", ((Exception) exceptions.get(0)).getMessage());
        NucleusLogger.DATASTORE.error(msg);
        throw new NucleusDataStoreException(msg, (Throwable[]) exceptions.toArray(new Throwable[exceptions.size()]), op.getObject());
    }
    return modified;
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) ArrayList(java.util.ArrayList) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) ArrayList(java.util.ArrayList) List(java.util.List) ManagedConnection(org.datanucleus.store.connection.ManagedConnection)

Example 17 with MappedDatastoreException

use of org.datanucleus.store.rdbms.exceptions.MappedDatastoreException in project datanucleus-rdbms by datanucleus.

the class AbstractArrayStore method add.

/**
 * Adds one element to the association owner vs elements
 * @param op ObjectProvider for the container
 * @param element The element to add
 * @param position The position to add this element at
 * @return Whether it was successful
 */
public boolean add(ObjectProvider op, E element, int position) {
    ExecutionContext ec = op.getExecutionContext();
    validateElementForWriting(ec, element, null);
    boolean modified = false;
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        try {
            // Add a row to the join table
            int[] returnCode = internalAdd(op, element, mconn, false, position, true);
            if (returnCode[0] > 0) {
                modified = true;
            }
        } finally {
            mconn.release();
        }
    } catch (MappedDatastoreException e) {
        throw new NucleusDataStoreException(Localiser.msg("056009", e.getMessage()), e.getCause());
    }
    return modified;
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) ManagedConnection(org.datanucleus.store.connection.ManagedConnection)

Example 18 with MappedDatastoreException

use of org.datanucleus.store.rdbms.exceptions.MappedDatastoreException in project datanucleus-rdbms by datanucleus.

the class FKSetStore method iterator.

/**
 * Accessor for an iterator for the set.
 * @param ownerOP ObjectProvider for the owner.
 * @return Iterator for the set.
 */
public Iterator<E> iterator(ObjectProvider ownerOP) {
    ExecutionContext ec = ownerOP.getExecutionContext();
    if (elementInfo == null || elementInfo.length == 0) {
        return null;
    }
    // Generate the statement, and statement mapping/parameter information
    IteratorStatement iterStmt = getIteratorStatement(ec, ec.getFetchPlan(), true);
    SelectStatement sqlStmt = iterStmt.getSelectStatement();
    StatementClassMapping iteratorMappingClass = iterStmt.getStatementClassMapping();
    // Input parameter(s) - the owner
    int inputParamNum = 1;
    StatementMappingIndex ownerStmtMapIdx = new StatementMappingIndex(ownerMapping);
    if (sqlStmt.getNumberOfUnions() > 0) {
        // Add parameter occurrence for each union of statement
        for (int j = 0; j < sqlStmt.getNumberOfUnions() + 1; j++) {
            int[] paramPositions = new int[ownerMapping.getNumberOfDatastoreMappings()];
            for (int k = 0; k < ownerMapping.getNumberOfDatastoreMappings(); k++) {
                paramPositions[k] = inputParamNum++;
            }
            ownerStmtMapIdx.addParameterOccurrence(paramPositions);
        }
    } else {
        int[] paramPositions = new int[ownerMapping.getNumberOfDatastoreMappings()];
        for (int k = 0; k < ownerMapping.getNumberOfDatastoreMappings(); k++) {
            paramPositions[k] = inputParamNum++;
        }
        ownerStmtMapIdx.addParameterOccurrence(paramPositions);
    }
    if (ec.getTransaction().getSerializeRead() != null && ec.getTransaction().getSerializeRead()) {
        sqlStmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE, true);
    }
    String stmt = sqlStmt.getSQLText().toSQL();
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            // Create the statement
            PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
            // Set the owner
            ObjectProvider stmtOwnerOP = BackingStoreHelper.getOwnerObjectProviderForBackingStore(ownerOP);
            int numParams = ownerStmtMapIdx.getNumberOfParameterOccurrences();
            for (int paramInstance = 0; paramInstance < numParams; paramInstance++) {
                ownerStmtMapIdx.getMapping().setObject(ec, ps, ownerStmtMapIdx.getParameterPositionsForOccurrence(paramInstance), stmtOwnerOP.getObject());
            }
            try {
                ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
                try {
                    ResultObjectFactory rof = null;
                    if (elementsAreEmbedded || elementsAreSerialised) {
                        throw new NucleusException("Cannot have FK set with non-persistent objects");
                    }
                    rof = new PersistentClassROF(ec, rs, false, iteratorMappingClass, elementCmd, clr.classForName(elementType));
                    return new CollectionStoreIterator(ownerOP, rs, rof, this);
                } finally {
                    rs.close();
                }
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException | MappedDatastoreException e) {
        throw new NucleusDataStoreException(Localiser.msg("056006", stmt), e);
    }
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) SQLException(java.sql.SQLException) ResultObjectFactory(org.datanucleus.store.rdbms.query.ResultObjectFactory) PreparedStatement(java.sql.PreparedStatement) StatementMappingIndex(org.datanucleus.store.rdbms.query.StatementMappingIndex) StatementClassMapping(org.datanucleus.store.rdbms.query.StatementClassMapping) SQLController(org.datanucleus.store.rdbms.SQLController) SelectStatement(org.datanucleus.store.rdbms.sql.SelectStatement) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) PersistentClassROF(org.datanucleus.store.rdbms.query.PersistentClassROF) ResultSet(java.sql.ResultSet) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ObjectProvider(org.datanucleus.state.ObjectProvider) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 19 with MappedDatastoreException

use of org.datanucleus.store.rdbms.exceptions.MappedDatastoreException in project datanucleus-rdbms by datanucleus.

the class JoinArrayStore method iterator.

/**
 * Method to return an iterator to the array.
 * @param ownerOP ObjectProvider for the owner of the array
 */
public Iterator<E> iterator(ObjectProvider ownerOP) {
    ExecutionContext ec = ownerOP.getExecutionContext();
    // Generate the statement, and statement mapping/parameter information
    IteratorStatement iterStmt = getIteratorStatement(ec, ec.getFetchPlan(), true);
    SelectStatement sqlStmt = iterStmt.sqlStmt;
    StatementClassMapping iteratorMappingClass = iterStmt.stmtClassMapping;
    // Input parameter(s) - the owner
    int inputParamNum = 1;
    StatementMappingIndex ownerIdx = new StatementMappingIndex(ownerMapping);
    if (sqlStmt.getNumberOfUnions() > 0) {
        // Add parameter occurrence for each union of statement
        for (int j = 0; j < sqlStmt.getNumberOfUnions() + 1; j++) {
            int[] paramPositions = new int[ownerMapping.getNumberOfDatastoreMappings()];
            for (int k = 0; k < paramPositions.length; k++) {
                paramPositions[k] = inputParamNum++;
            }
            ownerIdx.addParameterOccurrence(paramPositions);
        }
    } else {
        int[] paramPositions = new int[ownerMapping.getNumberOfDatastoreMappings()];
        for (int k = 0; k < paramPositions.length; k++) {
            paramPositions[k] = inputParamNum++;
        }
        ownerIdx.addParameterOccurrence(paramPositions);
    }
    StatementParameterMapping iteratorMappingParams = new StatementParameterMapping();
    iteratorMappingParams.addMappingForParameter("owner", ownerIdx);
    if (ec.getTransaction().getSerializeRead() != null && ec.getTransaction().getSerializeRead()) {
        sqlStmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE, true);
    }
    String stmt = sqlStmt.getSQLText().toSQL();
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            // Create the statement
            PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
            // Set the owner
            ObjectProvider stmtOwnerOP = BackingStoreHelper.getOwnerObjectProviderForBackingStore(ownerOP);
            int numParams = ownerIdx.getNumberOfParameterOccurrences();
            for (int paramInstance = 0; paramInstance < numParams; paramInstance++) {
                ownerIdx.getMapping().setObject(ec, ps, ownerIdx.getParameterPositionsForOccurrence(paramInstance), stmtOwnerOP.getObject());
            }
            try {
                ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
                try {
                    if (elementsAreEmbedded || elementsAreSerialised) {
                        // No ResultObjectFactory needed - handled by SetStoreIterator
                        return new ArrayStoreIterator(ownerOP, rs, null, this);
                    } else if (elementMapping instanceof ReferenceMapping) {
                        // No ResultObjectFactory needed - handled by SetStoreIterator
                        return new ArrayStoreIterator(ownerOP, rs, null, this);
                    } else {
                        ResultObjectFactory rof = new PersistentClassROF(ec, rs, false, iteratorMappingClass, elementCmd, clr.classForName(elementType));
                        return new ArrayStoreIterator(ownerOP, rs, rof, this);
                    }
                } finally {
                    rs.close();
                }
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException | MappedDatastoreException e) {
        throw new NucleusDataStoreException(Localiser.msg("056006", stmt), e);
    }
}
Also used : StatementParameterMapping(org.datanucleus.store.rdbms.query.StatementParameterMapping) MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) SQLException(java.sql.SQLException) ResultObjectFactory(org.datanucleus.store.rdbms.query.ResultObjectFactory) PreparedStatement(java.sql.PreparedStatement) StatementMappingIndex(org.datanucleus.store.rdbms.query.StatementMappingIndex) StatementClassMapping(org.datanucleus.store.rdbms.query.StatementClassMapping) SQLController(org.datanucleus.store.rdbms.SQLController) SelectStatement(org.datanucleus.store.rdbms.sql.SelectStatement) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) ReferenceMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceMapping) PersistentClassROF(org.datanucleus.store.rdbms.query.PersistentClassROF) ResultSet(java.sql.ResultSet) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) ObjectProvider(org.datanucleus.state.ObjectProvider)

Example 20 with MappedDatastoreException

use of org.datanucleus.store.rdbms.exceptions.MappedDatastoreException in project datanucleus-rdbms by datanucleus.

the class JoinMapStore method put.

/**
 * Method to put an item in the Map.
 * @param op ObjectProvider for the map.
 * @param key The key to store the value against
 * @param value The value to store.
 * @return The value stored.
 */
public V put(ObjectProvider op, K key, V value) {
    validateKeyForWriting(op, key);
    validateValueForWriting(op, value);
    boolean exists = false;
    V oldValue;
    try {
        oldValue = getValue(op, key);
        exists = true;
    } catch (NoSuchElementException e) {
        oldValue = null;
        exists = false;
    }
    if (oldValue != value) {
        // Value changed so update the map
        try {
            ExecutionContext ec = op.getExecutionContext();
            ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
            try {
                if (exists) {
                    internalUpdate(op, mconn, false, key, value, true);
                } else {
                    internalPut(op, mconn, false, key, value, true);
                }
            } finally {
                mconn.release();
            }
        } catch (MappedDatastoreException e) {
            throw new NucleusDataStoreException(Localiser.msg("056016", e.getMessage()), e);
        }
    }
    MapMetaData mapmd = ownerMemberMetaData.getMap();
    if (mapmd.isDependentValue() && !mapmd.isEmbeddedValue() && oldValue != null) {
        // Delete the old value if it is no longer contained and is dependent
        if (!containsValue(op, oldValue)) {
            op.getExecutionContext().deleteObjectInternal(oldValue);
        }
    }
    return oldValue;
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) MapMetaData(org.datanucleus.metadata.MapMetaData) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

ExecutionContext (org.datanucleus.ExecutionContext)27 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)27 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)21 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)21 SQLException (java.sql.SQLException)20 SQLController (org.datanucleus.store.rdbms.SQLController)20 PreparedStatement (java.sql.PreparedStatement)19 ResultSet (java.sql.ResultSet)9 ObjectProvider (org.datanucleus.state.ObjectProvider)9 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)9 PersistentClassROF (org.datanucleus.store.rdbms.query.PersistentClassROF)8 ResultObjectFactory (org.datanucleus.store.rdbms.query.ResultObjectFactory)8 StatementClassMapping (org.datanucleus.store.rdbms.query.StatementClassMapping)6 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)6 Iterator (java.util.Iterator)5 Transaction (org.datanucleus.Transaction)5 ArrayList (java.util.ArrayList)4 List (java.util.List)3 ListIterator (java.util.ListIterator)3 NucleusException (org.datanucleus.exceptions.NucleusException)3