Search in sources :

Example 26 with MappedDatastoreException

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

the class AbstractListStore method internalRemoveAt.

/**
 * Internal method to remove an object at a location in the List.
 * @param op ObjectProvider
 * @param index The location
 * @param stmt The statement to remove the element from the List
 * @param size Current list size (if known). -1 if not known
 */
protected void internalRemoveAt(ObjectProvider op, int index, String stmt, int size) {
    int currentListSize = 0;
    if (size < 0) {
        // Get the current size from the datastore
        currentListSize = size(op);
    } else {
        currentListSize = size;
    }
    ExecutionContext ec = op.getExecutionContext();
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, stmt, false);
            try {
                int jdbcPosition = 1;
                jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
                jdbcPosition = BackingStoreHelper.populateOrderInStatement(ec, ps, index, jdbcPosition, orderMapping);
                if (relationDiscriminatorMapping != null) {
                    jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
                }
                int[] rowsDeleted = sqlControl.executeStatementUpdate(ec, mconn, stmt, ps, true);
                if (rowsDeleted[0] == 0) {
                // ?? throw exception??
                }
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
            // shift down
            if (index != currentListSize - 1) {
                for (int i = index + 1; i < currentListSize; i++) {
                    // Shift this index down 1
                    internalShift(op, mconn, false, i, -1, true);
                }
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("056012", stmt), e);
    } catch (MappedDatastoreException e) {
        throw new NucleusDataStoreException(Localiser.msg("056012", stmt), e);
    }
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) SQLException(java.sql.SQLException) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) PreparedStatement(java.sql.PreparedStatement) SQLController(org.datanucleus.store.rdbms.SQLController)

Example 27 with MappedDatastoreException

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

the class AbstractSetStore method removeAll.

/**
 * Remove all elements from a collection from the association owner vs elements.
 * This implementation iterates around the remove() method doing each element 1 at a time.
 * Please refer to the JoinSetStore and FKSetStore for the variations used there.
 * This is used for Map key and value stores.
 * @param op ObjectProvider for the container
 * @param elements Collection of elements to remove
 * @return Whether the database was updated
 */
public boolean removeAll(ObjectProvider op, Collection elements, int size) {
    if (elements == null || elements.size() == 0) {
        return false;
    }
    boolean modified = false;
    List exceptions = new ArrayList();
    boolean batched = (elements.size() > 1);
    // Validate all elements exist
    Iterator iter = elements.iterator();
    while (iter.hasNext()) {
        Object element = iter.next();
        if (!validateElementForReading(op, element)) {
            NucleusLogger.DATASTORE.debug("AbstractSetStore::removeAll element=" + element + " doesn't exist in this Set.");
            return false;
        }
    }
    try {
        ExecutionContext ec = op.getExecutionContext();
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        try {
            // Process all waiting batched statements before we start our work
            SQLController sqlControl = storeMgr.getSQLController();
            try {
                sqlControl.processStatementsForConnection(mconn);
            } catch (SQLException e) {
                throw new MappedDatastoreException("SQLException", e);
            }
            iter = elements.iterator();
            while (iter.hasNext()) {
                Object element = iter.next();
                try {
                    // Process the remove
                    int[] rc = null;
                    String removeStmt = getRemoveStmt(element);
                    try {
                        PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, removeStmt, batched);
                        try {
                            int jdbcPosition = 1;
                            jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
                            jdbcPosition = BackingStoreHelper.populateElementForWhereClauseInStatement(ec, ps, element, jdbcPosition, elementMapping);
                            if (relationDiscriminatorMapping != null) {
                                jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
                            }
                            // Execute the statement
                            rc = sqlControl.executeStatementUpdate(ec, mconn, removeStmt, ps, !batched || (batched && !iter.hasNext()));
                            if (rc != null) {
                                for (int i = 0; i < rc.length; i++) {
                                    if (rc[i] > 0) {
                                        // At least one record was inserted
                                        modified = true;
                                    }
                                }
                            }
                        } finally {
                            sqlControl.closeStatement(mconn, ps);
                        }
                    } catch (SQLException e) {
                        throw new MappedDatastoreException("SQLException", e);
                    }
                } catch (MappedDatastoreException mde) {
                    exceptions.add(mde);
                    NucleusLogger.DATASTORE.error("Exception in remove", mde);
                }
            }
        } finally {
            mconn.release();
        }
    } catch (MappedDatastoreException e) {
        exceptions.add(e);
        NucleusLogger.DATASTORE.error("Exception performing removeAll on set backing store", e);
    }
    if (!exceptions.isEmpty()) {
        // Throw all exceptions received as the cause of a NucleusDataStoreException so the user can see which record(s) didn't remove
        String msg = Localiser.msg("056012", ((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) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) SQLController(org.datanucleus.store.rdbms.SQLController) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ManagedConnection(org.datanucleus.store.connection.ManagedConnection)

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