use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.
the class JoinSetStore method elementAlreadyContainsOwnerInMtoN.
/**
* Convenience method to check if an element already refers to the owner in an M-N relation (i.e added from other side).
* @param ownerOP ObjectProvider of the owner
* @param element The element
* @return Whether the element contains the owner
*/
private boolean elementAlreadyContainsOwnerInMtoN(ObjectProvider ownerOP, Object element) {
ExecutionContext ec = ownerOP.getExecutionContext();
if (ec.getOperationQueue() != null) {
// TODO This means we always do a "SELECT 1 FROM JOINTABLE" for every addition when optimistic. Should seek to avoid this by updates to operationQueue maybe
if (locate(ownerOP, element)) {
NucleusLogger.DATASTORE.info(Localiser.msg("056040", ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(ownerOP.getObject()), element));
return true;
}
return false;
}
ObjectProvider elementOP = ec.findObjectProvider(element);
if (elementOP != null) {
// Check the collection at the other side whether already added (to avoid the locate call)
AbstractMemberMetaData[] relatedMmds = ownerMemberMetaData.getRelatedMemberMetaData(ec.getClassLoaderResolver());
Object elementColl = elementOP.provideField(relatedMmds[0].getAbsoluteFieldNumber());
if (elementColl != null && elementColl instanceof Collection && elementColl instanceof SCO && ((Collection) elementColl).contains(ownerOP.getObject())) {
NucleusLogger.DATASTORE.info(Localiser.msg("056040", ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(ownerOP.getObject()), element));
return true;
}
} else {
// Element is still detached
if (locate(ownerOP, element)) {
NucleusLogger.DATASTORE.info(Localiser.msg("056040", ownerMemberMetaData.getFullFieldName(), StringUtils.toJVMIDString(ownerOP.getObject()), element));
return true;
}
}
return false;
}
use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.
the class JoinSetStore method getNextIDForOrderColumn.
protected int getNextIDForOrderColumn(ObjectProvider op) {
int nextID;
ExecutionContext ec = op.getExecutionContext();
String stmt = getMaxOrderColumnIdStmt();
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
try {
int jdbcPosition = 1;
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
if (relationDiscriminatorMapping != null) {
BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, this);
}
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
try {
if (!rs.next()) {
nextID = 1;
} else {
nextID = rs.getInt(1) + 1;
}
JDBCUtils.logWarnings(rs);
} finally {
rs.close();
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException(Localiser.msg("056020", stmt), e);
}
return nextID;
}
use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.
the class MapEntrySetStore method size.
public int size(ObjectProvider op) {
int numRows;
String stmt = getSizeStmt();
try {
ExecutionContext ec = op.getExecutionContext();
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try {
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, stmt);
try {
int jdbcPosition = 1;
BackingStoreHelper.populateOwnerInStatement(op, ec, ps, jdbcPosition, this);
ResultSet rs = sqlControl.executeStatementQuery(ec, mconn, stmt, ps);
try {
if (!rs.next()) {
throw new NucleusDataStoreException("Size request returned no result row: " + stmt);
}
numRows = rs.getInt(1);
JDBCUtils.logWarnings(rs);
} finally {
rs.close();
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException("Size request failed: " + stmt, e);
}
return numRows;
}
use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.
the class MapEntrySetStore method iterator.
/**
* Method returning an iterator across the entries in the map for this owner object.
* @param ownerOP ObjectProvider of the owning object
* @return The iterator for the entries (<pre>map.entrySet().iterator()</pre>).
*/
public Iterator<Map.Entry<K, 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 {
return new SetIterator(ownerOP, this, ownerMemberMetaData, rs, iteratorKeyResultCols, iteratorValueResultCols) {
protected boolean next(Object rs) throws MappedDatastoreException {
try {
return ((ResultSet) rs).next();
} catch (SQLException e) {
throw new MappedDatastoreException("SQLException", e);
}
}
};
} finally {
rs.close();
}
} finally {
sqlControl.closeStatement(mconn, ps);
}
} finally {
mconn.release();
}
} catch (SQLException e) {
throw new NucleusDataStoreException("Iteration request failed: " + stmt, e);
} catch (MappedDatastoreException e) {
throw new NucleusDataStoreException("Iteration request failed: " + stmt, e);
}
}
use of org.datanucleus.ExecutionContext in project datanucleus-rdbms by datanucleus.
the class MapKeySetStore method iterator.
/**
* Accessor for an iterator for the set.
* @param ownerOP ObjectProvider for the set.
* @return Iterator for the set.
*/
public Iterator<K> 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