Search in sources :

Example 36 with ExecutionContext

use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.

the class JoinMapStore method putAll.

/**
 * Method to put all elements from a Map into our Map.
 * @param op ObjectProvider for the Map
 * @param m The Map to add
 */
public void putAll(ObjectProvider op, Map<? extends K, ? extends V> m) {
    if (m == null || m.size() == 0) {
        return;
    }
    Set<Map.Entry> puts = new HashSet<>();
    Set<Map.Entry> updates = new HashSet<>();
    Iterator i = m.entrySet().iterator();
    while (i.hasNext()) {
        Map.Entry e = (Map.Entry) i.next();
        Object key = e.getKey();
        Object value = e.getValue();
        // Make sure the related objects are persisted (persistence-by-reachability)
        validateKeyForWriting(op, key);
        validateValueForWriting(op, value);
        // Check if this is a new entry, or an update
        try {
            Object oldValue = getValue(op, key);
            if (oldValue != value) {
                updates.add(e);
            }
        } catch (NoSuchElementException nsee) {
            puts.add(e);
        }
    }
    boolean batched = allowsBatching();
    // Put any new entries
    if (puts.size() > 0) {
        try {
            ExecutionContext ec = op.getExecutionContext();
            ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
            try {
                // Loop through all entries
                Iterator<Map.Entry> iter = puts.iterator();
                while (iter.hasNext()) {
                    // Add the row to the join table
                    Map.Entry entry = iter.next();
                    internalPut(op, mconn, batched, entry.getKey(), entry.getValue(), (!iter.hasNext()));
                }
            } finally {
                mconn.release();
            }
        } catch (MappedDatastoreException e) {
            throw new NucleusDataStoreException(Localiser.msg("056016", e.getMessage()), e);
        }
    }
    // Update any changed entries
    if (updates.size() > 0) {
        try {
            ExecutionContext ec = op.getExecutionContext();
            ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
            try {
                // Loop through all entries
                Iterator<Map.Entry> iter = updates.iterator();
                while (iter.hasNext()) {
                    // Update the row in the join table
                    Map.Entry entry = iter.next();
                    internalUpdate(op, mconn, batched, entry.getKey(), entry.getValue(), !iter.hasNext());
                }
            } finally {
                mconn.release();
            }
        } catch (MappedDatastoreException mde) {
            throw new NucleusDataStoreException(Localiser.msg("056016", mde.getMessage()), mde);
        }
    }
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) Iterator(java.util.Iterator) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException) HashSet(java.util.HashSet)

Example 37 with ExecutionContext

use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.

the class JoinMapStore method internalUpdate.

/**
 * Method to process an "update" statement (where the key already has a value in the join table).
 * @param ownerOP ObjectProvider for the owner
 * @param conn The Connection
 * @param batched Whether we are batching it
 * @param key The key
 * @param value The new value
 * @param executeNow Whether to execute the statement now or wait til any batch
 * @throws MappedDatastoreException Thrown if an error occurs
 */
protected void internalUpdate(ObjectProvider ownerOP, ManagedConnection conn, boolean batched, Object key, Object value, boolean executeNow) throws MappedDatastoreException {
    ExecutionContext ec = ownerOP.getExecutionContext();
    SQLController sqlControl = storeMgr.getSQLController();
    try {
        PreparedStatement ps = sqlControl.getStatementForUpdate(conn, updateStmt, false);
        try {
            int jdbcPosition = 1;
            if (valueMapping != null) {
                jdbcPosition = BackingStoreHelper.populateValueInStatement(ec, ps, value, jdbcPosition, valueMapping);
            } else {
                jdbcPosition = BackingStoreHelper.populateEmbeddedValueFieldsInStatement(ownerOP, value, ps, jdbcPosition, (JoinTable) mapTable, this);
            }
            jdbcPosition = BackingStoreHelper.populateOwnerInStatement(ownerOP, ec, ps, jdbcPosition, this);
            jdbcPosition = BackingStoreHelper.populateKeyInStatement(ec, ps, key, jdbcPosition, keyMapping);
            if (batched) {
                ps.addBatch();
            } else {
                sqlControl.executeStatementUpdate(ec, conn, updateStmt, ps, true);
            }
        } finally {
            sqlControl.closeStatement(conn, ps);
        }
    } catch (SQLException e) {
        throw new MappedDatastoreException(getUpdateStmt(), e);
    }
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) ExecutionContext(org.datanucleus.ExecutionContext) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) SQLController(org.datanucleus.store.rdbms.SQLController) JoinTable(org.datanucleus.store.rdbms.table.JoinTable)

Example 38 with ExecutionContext

use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.

the class JoinPersistableRelationStore method add.

/* (non-Javadoc)
     * @see org.datanucleus.store.scostore.PersistableRelationStore#add(org.datanucleus.store.ObjectProvider, org.datanucleus.store.ObjectProvider)
     */
public boolean add(ObjectProvider op1, ObjectProvider op2) {
    String addStmt = getAddStmt();
    ExecutionContext ec = op1.getExecutionContext();
    SQLController sqlControl = storeMgr.getSQLController();
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, addStmt, false);
        try {
            // Insert the join table row
            int jdbcPosition = 1;
            jdbcPosition = populateOwnerInStatement(op1, ec, ps, jdbcPosition, joinTable);
            BackingStoreHelper.populateElementInStatement(ec, ps, op2.getObject(), jdbcPosition, joinTable.getRelatedMapping());
            // Execute the statement
            int[] nums = sqlControl.executeStatementUpdate(ec, mconn, addStmt, ps, true);
            return (nums != null && nums.length == 1 && nums[0] == 1);
        } finally {
            sqlControl.closeStatement(mconn, ps);
            mconn.release();
        }
    } catch (SQLException sqle) {
        throw new NucleusDataStoreException("Exception thrown inserting row into persistable relation join table", sqle);
    }
}
Also used : 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 39 with ExecutionContext

use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.

the class JoinPersistableRelationStore method update.

/* (non-Javadoc)
     * @see org.datanucleus.store.scostore.PersistableRelationStore#update(org.datanucleus.store.ObjectProvider, org.datanucleus.store.ObjectProvider)
     */
public boolean update(ObjectProvider op1, ObjectProvider op2) {
    String updateStmt = getUpdateStmt();
    ExecutionContext ec = op1.getExecutionContext();
    SQLController sqlControl = storeMgr.getSQLController();
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, updateStmt, false);
        try {
            // Update the join table row
            int jdbcPosition = 1;
            jdbcPosition = BackingStoreHelper.populateElementInStatement(ec, ps, op2.getObject(), jdbcPosition, joinTable.getRelatedMapping());
            populateOwnerInStatement(op1, ec, ps, jdbcPosition, joinTable);
            // Execute the statement
            int[] nums = sqlControl.executeStatementUpdate(ec, mconn, updateStmt, ps, true);
            return (nums != null && nums.length == 1 && nums[0] == 1);
        } finally {
            sqlControl.closeStatement(mconn, ps);
            mconn.release();
        }
    } catch (SQLException sqle) {
        throw new NucleusDataStoreException("Exception thrown updating row into persistable relation join table", sqle);
    }
}
Also used : 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 40 with ExecutionContext

use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.

the class JoinSetStore method doInternalAdd.

protected int[] doInternalAdd(ObjectProvider op, E element, ManagedConnection conn, boolean batched, int orderId, boolean executeNow) throws MappedDatastoreException {
    // Check for dynamic schema updates prior to addition
    if (storeMgr.getBooleanObjectProperty(RDBMSPropertyNames.PROPERTY_RDBMS_DYNAMIC_SCHEMA_UPDATES).booleanValue()) {
        DynamicSchemaFieldManager dynamicSchemaFM = new DynamicSchemaFieldManager(storeMgr, op);
        Collection coll = new HashSet();
        coll.add(element);
        dynamicSchemaFM.storeObjectField(ownerMemberMetaData.getAbsoluteFieldNumber(), coll);
        if (dynamicSchemaFM.hasPerformedSchemaUpdates()) {
            invalidateAddStmt();
        }
    }
    String addStmt = getAddStmtForJoinTable();
    boolean notYetFlushedError = false;
    ExecutionContext ec = op.getExecutionContext();
    SQLController sqlControl = storeMgr.getSQLController();
    try {
        PreparedStatement ps = sqlControl.getStatementForUpdate(conn, addStmt, batched);
        try {
            // Insert the join table row
            int jdbcPosition = 1;
            jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
            jdbcPosition = BackingStoreHelper.populateElementInStatement(ec, ps, element, jdbcPosition, elementMapping);
            if (orderMapping != null) {
                jdbcPosition = BackingStoreHelper.populateOrderInStatement(ec, ps, orderId, jdbcPosition, orderMapping);
            }
            if (relationDiscriminatorMapping != null) {
                jdbcPosition = BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
            }
            return sqlControl.executeStatementUpdate(ec, conn, addStmt, ps, executeNow);
        } catch (NotYetFlushedException nfe) {
            notYetFlushedError = true;
            throw nfe;
        } finally {
            if (notYetFlushedError) {
                sqlControl.abortStatementForConnection(conn, ps);
            } else {
                sqlControl.closeStatement(conn, ps);
            }
        }
    } catch (SQLException e) {
        throw new MappedDatastoreException(addStmt, e);
    }
}
Also used : MappedDatastoreException(org.datanucleus.store.rdbms.exceptions.MappedDatastoreException) ExecutionContext(org.datanucleus.ExecutionContext) DynamicSchemaFieldManager(org.datanucleus.store.rdbms.fieldmanager.DynamicSchemaFieldManager) SQLException(java.sql.SQLException) Collection(java.util.Collection) PreparedStatement(java.sql.PreparedStatement) NotYetFlushedException(org.datanucleus.exceptions.NotYetFlushedException) HashSet(java.util.HashSet) SQLController(org.datanucleus.store.rdbms.SQLController)

Aggregations

ExecutionContext (org.datanucleus.ExecutionContext)178 ObjectProvider (org.datanucleus.state.ObjectProvider)85 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)73 SQLException (java.sql.SQLException)66 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)64 SQLController (org.datanucleus.store.rdbms.SQLController)63 PreparedStatement (java.sql.PreparedStatement)62 Iterator (java.util.Iterator)56 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)27 ResultSet (java.sql.ResultSet)26 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)26 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)25 Map (java.util.Map)23 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)20 Collection (java.util.Collection)18 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)18 StatementClassMapping (org.datanucleus.store.rdbms.query.StatementClassMapping)17 DatastoreClass (org.datanucleus.store.rdbms.table.DatastoreClass)16 SCOCollectionIterator (org.datanucleus.store.types.SCOCollectionIterator)16 ArrayList (java.util.ArrayList)15