use of org.datanucleus.store.rdbms.exceptions.MappedDatastoreException in project datanucleus-rdbms by datanucleus.
the class JoinMapStore method internalPut.
/**
* Method to process a "put" statement (where the key has no 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 value
* @param executeNow Whether to execute the statement now or wait til batching
* @return The return codes from any executed statement
* @throws MappedDatastoreException Thrown if an error occurs
*/
protected int[] internalPut(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, putStmt, 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);
if (adapterMapping != null) {
// Only set the adapter mapping if we have a new object
long nextIDAdapter = getNextIDForAdapterColumn(ownerOP);
adapterMapping.setObject(ec, ps, MappingHelper.getMappingIndices(jdbcPosition, adapterMapping), Long.valueOf(nextIDAdapter));
jdbcPosition += adapterMapping.getNumberOfDatastoreMappings();
}
jdbcPosition = BackingStoreHelper.populateKeyInStatement(ec, ps, key, jdbcPosition, keyMapping);
// Execute the statement
return sqlControl.executeStatementUpdate(ec, conn, putStmt, ps, true);
} finally {
sqlControl.closeStatement(conn, ps);
}
} catch (SQLException e) {
throw new MappedDatastoreException(getPutStmt(), e);
}
}
use of org.datanucleus.store.rdbms.exceptions.MappedDatastoreException in project datanucleus-rdbms by datanucleus.
the class JoinSetStore method addAll.
/**
* Adds all elements from a collection to the association container.
* @param op ObjectProvider for the container.
* @param elements Collection of elements to add
* @return Whether it was successful
*/
public boolean addAll(ObjectProvider op, Collection<E> elements, int size) {
if (elements == null || elements.size() == 0) {
return false;
}
List exceptions = new ArrayList();
boolean batched = (elements.size() > 1);
// Validate all elements for writing
ExecutionContext ec = op.getExecutionContext();
Iterator<E> iter = elements.iterator();
while (iter.hasNext()) {
Object element = iter.next();
validateElementForWriting(ec, element, null);
if (relationType == RelationType.ONE_TO_MANY_BI) {
// TODO This is ManagedRelations - move into RelationshipManager
// Managed Relations : make sure we have consistency of relation
ObjectProvider elementOP = op.getExecutionContext().findObjectProvider(element);
if (elementOP != null) {
AbstractMemberMetaData[] relatedMmds = ownerMemberMetaData.getRelatedMemberMetaData(clr);
Object elementOwner = elementOP.provideField(relatedMmds[0].getAbsoluteFieldNumber());
if (elementOwner == null) {
// No owner, so correct it
NucleusLogger.PERSISTENCE.info(Localiser.msg("056037", op.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(elementOP.getObject())));
elementOP.replaceField(relatedMmds[0].getAbsoluteFieldNumber(), op.getObject());
} else if (elementOwner != op.getObject() && op.getReferencedPC() == null) {
// Inconsistent owner, so throw exception
throw new NucleusUserException(Localiser.msg("056038", op.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(elementOP.getObject()), StringUtils.toJVMIDString(elementOwner)));
}
}
}
}
boolean modified = false;
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
try {
SQLController sqlControl = storeMgr.getSQLController();
try {
// Process all waiting batched statements before we start our work
sqlControl.processStatementsForConnection(mconn);
} catch (SQLException e) {
throw new MappedDatastoreException("SQLException", e);
}
int nextOrderID = orderMapping != null ? getNextIDForOrderColumn(op) : 0;
// Loop through all elements to be added
iter = elements.iterator();
E element = null;
while (iter.hasNext()) {
element = iter.next();
try {
// Add the row to the join table
boolean toBeInserted = true;
if (relationType == RelationType.MANY_TO_MANY_BI) {
toBeInserted = !elementAlreadyContainsOwnerInMtoN(op, element);
}
if (toBeInserted) {
int[] rc = doInternalAdd(op, element, mconn, batched, nextOrderID, !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;
}
}
}
nextOrderID++;
}
} catch (MappedDatastoreException mde) {
exceptions.add(mde);
NucleusLogger.DATASTORE.error("Exception thrown", mde);
}
}
} finally {
mconn.release();
}
} catch (MappedDatastoreException e) {
exceptions.add(e);
NucleusLogger.DATASTORE.error("Exception thrown", 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;
}
use of org.datanucleus.store.rdbms.exceptions.MappedDatastoreException in project datanucleus-rdbms by datanucleus.
the class JoinSetStore method add.
/**
* Adds one element to the association owner vs elements.
* @param op ObjectProvider for the container.
* @param element Element to add
* @return Whether it was successful
*/
public boolean add(ObjectProvider op, E element, int size) {
// Check that the object is valid for writing
ExecutionContext ec = op.getExecutionContext();
validateElementForWriting(ec, element, null);
if (relationType == RelationType.ONE_TO_MANY_BI) {
// TODO This is ManagedRelations - move into RelationshipManager
// Managed Relations : make sure we have consistency of relation
ObjectProvider elementOP = ec.findObjectProvider(element);
if (elementOP != null) {
AbstractMemberMetaData[] relatedMmds = ownerMemberMetaData.getRelatedMemberMetaData(clr);
Object elementOwner = elementOP.provideField(relatedMmds[0].getAbsoluteFieldNumber());
if (elementOwner == null) {
// No owner, so correct it
NucleusLogger.PERSISTENCE.info(Localiser.msg("056037", op.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(elementOP.getObject())));
elementOP.replaceField(relatedMmds[0].getAbsoluteFieldNumber(), op.getObject());
} else if (elementOwner != op.getObject() && op.getReferencedPC() == null) {
// Inconsistent owner, so throw exception
throw new NucleusUserException(Localiser.msg("056038", op.getObjectAsPrintable(), ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(elementOP.getObject()), StringUtils.toJVMIDString(elementOwner)));
}
}
}
boolean modified = false;
boolean toBeInserted = true;
if (relationType == RelationType.MANY_TO_MANY_BI) {
toBeInserted = !elementAlreadyContainsOwnerInMtoN(op, element);
}
if (toBeInserted) {
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
try {
// Add a row to the join table
int orderID = orderMapping != null ? getNextIDForOrderColumn(op) : -1;
int[] returnCode = doInternalAdd(op, element, mconn, false, orderID, true);
if (returnCode[0] > 0) {
modified = true;
}
} finally {
mconn.release();
}
} catch (MappedDatastoreException e) {
String msg = Localiser.msg("056009", e.getMessage());
NucleusLogger.DATASTORE.error(msg, e);
throw new NucleusDataStoreException(msg, e);
}
}
return modified;
}
use of org.datanucleus.store.rdbms.exceptions.MappedDatastoreException in project datanucleus-rdbms by datanucleus.
the class JoinSetStore method iterator.
/**
* Accessor for an iterator for the set.
* @param ownerOP ObjectProvider for the set.
* @return Iterator for the set.
*/
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 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 < paramPositions.length; k++) {
paramPositions[k] = inputParamNum++;
}
ownerStmtMapIdx.addParameterOccurrence(paramPositions);
}
} else {
int[] paramPositions = new int[ownerMapping.getNumberOfDatastoreMappings()];
for (int k = 0; k < paramPositions.length; 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 {
if (elementsAreEmbedded || elementsAreSerialised) {
// No ResultObjectFactory needed - handled by SetStoreIterator
return new CollectionStoreIterator(ownerOP, rs, null, this);
} else if (elementMapping instanceof ReferenceMapping) {
// No ResultObjectFactory needed - handled by SetStoreIterator
return new CollectionStoreIterator(ownerOP, rs, null, this);
} else {
ResultObjectFactory 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);
}
}
use of org.datanucleus.store.rdbms.exceptions.MappedDatastoreException in project datanucleus-rdbms by datanucleus.
the class MapValueCollectionStore method iterator.
/**
* Accessor for an iterator for the set.
* @param ownerOP ObjectProvider for the set.
* @return Iterator for the set.
*/
public Iterator<V> iterator(ObjectProvider ownerOP) {
ExecutionContext ec = ownerOP.getExecutionContext();
if (iteratorStmtLocked == 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 = getSQLStatementForIterator(ownerOP);
iteratorStmtUnlocked = sqlStmt.getSQLText().toSQL();
sqlStmt.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE, true);
iteratorStmtLocked = sqlStmt.getSQLText().toSQL();
}
}
Transaction tx = ec.getTransaction();
String stmt = (tx.getSerializeRead() != null && tx.getSerializeRead() ? iteratorStmtLocked : iteratorStmtUnlocked);
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
// Create the statement and set the owner
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
StatementMappingIndex ownerIdx = iteratorMappingParams.getMappingForParameter("owner");
int numParams = ownerIdx.getNumberOfParameterOccurrences();
for (int paramInstance = 0; paramInstance < numParams; paramInstance++) {
ownerIdx.getMapping().setObject(ec, ps, ownerIdx.getParameterPositionsForOccurrence(paramInstance), ownerOP.getObject());
}
try {
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
try {
ResultObjectFactory rof = null;
if (elementsAreEmbedded || elementsAreSerialised) {
// No ResultObjectFactory needed - handled by SetStoreIterator
return new CollectionStoreIterator(ownerOP, rs, null, this);
}
rof = new PersistentClassROF(ec, rs, false, iteratorMappingDef, elementCmd, clr.classForName(elementType));
return new CollectionStoreIterator(ownerOP, rs, rof, this);
} finally {
rs.close();
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056006", stmt), e);
} catch (MappedDatastoreException e) {
throw new NucleusDataStoreException(Localiser.msg("056006", stmt), e);
}
}
Aggregations