Search in sources :

Example 31 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class JoinMapStore method removeInternal.

protected void removeInternal(ObjectProvider op, Object key) {
    ExecutionContext ec = op.getExecutionContext();
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, removeStmt, false);
            try {
                int jdbcPosition = 1;
                jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
                BackingStoreHelper.populateKeyInStatement(ec, ps, key, jdbcPosition, keyMapping);
                sqlControl.executeStatementUpdate(ec, mconn, removeStmt, ps, true);
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("056012", removeStmt), e);
    }
}
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 32 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.

the class JoinMapStore method getValue.

/**
 * Method to retrieve a value from the Map given the key.
 * @param ownerOP ObjectProvider for the owner of the map.
 * @param key The key to retrieve the value for.
 * @return The value for this key
 * @throws NoSuchElementException if the value for the key was not found
 */
protected V getValue(ObjectProvider ownerOP, Object key) throws NoSuchElementException {
    if (!validateKeyForReading(ownerOP, key)) {
        return null;
    }
    ExecutionContext ec = ownerOP.getExecutionContext();
    if (getStmtLocked == null) {
        synchronized (// Make sure this completes in case another thread needs the same info
        this) {
            // Generate the statement, and statement mapping/parameter information
            SQLStatement sqlStmt = getSQLStatementForGet(ownerOP);
            getStmtUnlocked = sqlStmt.getSQLText().toSQL();
            sqlStmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE, true);
            getStmtLocked = sqlStmt.getSQLText().toSQL();
        }
    }
    Transaction tx = ec.getTransaction();
    String stmt = (tx.getSerializeRead() != null && tx.getSerializeRead() ? getStmtLocked : getStmtUnlocked);
    Object value = null;
    try {
        ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
        SQLController sqlControl = storeMgr.getSQLController();
        try {
            // Create the statement and supply owner/key params
            PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
            StatementMappingIndex ownerIdx = getMappingParams.getMappingForParameter("owner");
            int numParams = ownerIdx.getNumberOfParameterOccurrences();
            for (int paramInstance = 0; paramInstance < numParams; paramInstance++) {
                ownerIdx.getMapping().setObject(ec, ps, ownerIdx.getParameterPositionsForOccurrence(paramInstance), ownerOP.getObject());
            }
            StatementMappingIndex keyIdx = getMappingParams.getMappingForParameter("key");
            numParams = keyIdx.getNumberOfParameterOccurrences();
            for (int paramInstance = 0; paramInstance < numParams; paramInstance++) {
                keyIdx.getMapping().setObject(ec, ps, keyIdx.getParameterPositionsForOccurrence(paramInstance), key);
            }
            try {
                ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
                try {
                    boolean found = rs.next();
                    if (!found) {
                        throw new NoSuchElementException();
                    }
                    if (valuesAreEmbedded || valuesAreSerialised) {
                        int[] param = new int[valueMapping.getNumberOfDatastoreMappings()];
                        for (int i = 0; i < param.length; ++i) {
                            param[i] = i + 1;
                        }
                        if (valueMapping instanceof SerialisedPCMapping || valueMapping instanceof SerialisedReferenceMapping || valueMapping instanceof EmbeddedKeyPCMapping) {
                            // Value = Serialised
                            int ownerFieldNumber = ((JoinTable) mapTable).getOwnerMemberMetaData().getAbsoluteFieldNumber();
                            value = valueMapping.getObject(ec, rs, param, ownerOP, ownerFieldNumber);
                        } else {
                            // Value = Non-PC
                            value = valueMapping.getObject(ec, rs, param);
                        }
                    } else if (valueMapping instanceof ReferenceMapping) {
                        // Value = Reference (Interface/Object)
                        int[] param = new int[valueMapping.getNumberOfDatastoreMappings()];
                        for (int i = 0; i < param.length; ++i) {
                            param[i] = i + 1;
                        }
                        value = valueMapping.getObject(ec, rs, param);
                    } else {
                        // Value = PC
                        ResultObjectFactory rof = new PersistentClassROF(ec, rs, false, getMappingDef, valueCmd, clr.classForName(valueType));
                        value = rof.getObject();
                    }
                    JDBCUtils.logWarnings(rs);
                } finally {
                    rs.close();
                }
            } finally {
                sqlControl.closeStatement(mconn, ps);
            }
        } finally {
            mconn.release();
        }
    } catch (SQLException e) {
        throw new NucleusDataStoreException(Localiser.msg("056014", stmt), e);
    }
    return (V) value;
}
Also used : SerialisedReferenceMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedReferenceMapping) SQLException(java.sql.SQLException) ResultObjectFactory(org.datanucleus.store.rdbms.query.ResultObjectFactory) PreparedStatement(java.sql.PreparedStatement) StatementMappingIndex(org.datanucleus.store.rdbms.query.StatementMappingIndex) SQLStatement(org.datanucleus.store.rdbms.sql.SQLStatement) SQLController(org.datanucleus.store.rdbms.SQLController) NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) ExecutionContext(org.datanucleus.ExecutionContext) SerialisedReferenceMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedReferenceMapping) ReferenceMapping(org.datanucleus.store.rdbms.mapping.java.ReferenceMapping) Transaction(org.datanucleus.Transaction) PersistentClassROF(org.datanucleus.store.rdbms.query.PersistentClassROF) ResultSet(java.sql.ResultSet) ManagedConnection(org.datanucleus.store.connection.ManagedConnection) SerialisedPCMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedPCMapping) NoSuchElementException(java.util.NoSuchElementException) EmbeddedKeyPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedKeyPCMapping)

Example 33 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException 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 34 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException 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 35 with NucleusDataStoreException

use of org.datanucleus.exceptions.NucleusDataStoreException 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)

Aggregations

NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)288 SQLException (java.sql.SQLException)185 ExecutionContext (org.datanucleus.ExecutionContext)139 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)131 PreparedStatement (java.sql.PreparedStatement)128 SQLController (org.datanucleus.store.rdbms.SQLController)126 ResultSet (java.sql.ResultSet)73 Iterator (java.util.Iterator)43 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)38 CollectionAddOperation (org.datanucleus.flush.CollectionAddOperation)34 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)33 ArrayList (java.util.ArrayList)32 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)30 Collection (java.util.Collection)27 CollectionRemoveOperation (org.datanucleus.flush.CollectionRemoveOperation)26 StatementClassMapping (org.datanucleus.store.rdbms.query.StatementClassMapping)24 List (java.util.List)23 ObjectProvider (org.datanucleus.state.ObjectProvider)22 SelectStatement (org.datanucleus.store.rdbms.sql.SelectStatement)22 MappedDatastoreException (org.datanucleus.store.rdbms.exceptions.MappedDatastoreException)21