use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class StoredProcedureQueryImpl method buildResultSetMappingQuery.
/**
* Build a ResultSetMappingQuery from the sql result set mappings given
* a stored procedure call.
*
* This is called from a named stored procedure query that employs result
* class name(s). The resultSetMappings are build from these class name(s)
* and are not available from the session.
*/
public static DatabaseQuery buildResultSetMappingQuery(List<SQLResultSetMapping> resultSetMappings, StoredProcedureCall call, Map<String, Object> hints, ClassLoader classLoader, AbstractSession session) {
// apply any query hints
DatabaseQuery hintQuery = applyHints(hints, buildResultSetMappingQuery(resultSetMappings, call), classLoader, session);
// apply any query arguments
applyArguments(call, hintQuery);
return hintQuery;
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class StoredProcedureQueryImpl method close.
/**
* Call this method to close any open connections to the database.
*/
@Override
public void close() {
if (executeCall != null) {
DatabaseQuery query = executeCall.getQuery();
AbstractSession session = query.getSession();
// Release the accessors acquired for the query.
for (Accessor accessor : query.getAccessors()) {
session.releaseReadConnection(accessor);
}
try {
if (executeStatement != null) {
DatabaseAccessor accessor = (DatabaseAccessor) query.getAccessor();
accessor.releaseStatement(executeStatement, query.getSQLString(), executeCall, session);
}
} catch (SQLException exception) {
// Catch the exception and log a message.
session.log(SessionLog.WARNING, SessionLog.CONNECTION, "exception_caught_closing_statement", exception);
}
}
executeCall = null;
executeStatement = null;
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class QueryImpl method setHintInternal.
/**
* Set an implementation-specific hint. If the hint name is not recognized,
* it is silently ignored.
*
* @throws IllegalArgumentException
* if the second argument is not valid for the implementation.
*/
protected void setHintInternal(String hintName, Object value) {
cloneSharedQuery();
ClassLoader loader = getEntityManager().getAbstractSession().getLoader();
DatabaseQuery hintQuery = QueryHintsHandler.apply(hintName, value, getDatabaseQueryInternal(), loader, (AbstractSession) getActiveSession());
if (hintQuery != null) {
setDatabaseQuery(hintQuery);
}
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class QueryImpl method executeReadQuery.
/**
* Execute a ReadQuery by assigning the stored parameter values and running
* it in the database
*
* @return the results of the query execution
*/
protected Object executeReadQuery() {
List<Object> parameterValues = processParameters();
// TODO: the following performFlush() call is a temporary workaround for
// bug 4752493:
// CTS: INMEMORY QUERYING IN EJBQUERY BROKEN DUE TO CHANGE TO USE
// REPORTQUERY.
// Ideally we should only flush in case the selectionExpression can't be
// conformed in memory.
// There are two alternative ways to implement that:
// 1. Try running the query with conformInUOW flag first - if it fails
// with
// QueryException.cannotConformExpression then flush and run the query
// again -
// now without conforming.
// 2. Implement a new isComformable method on Expression which would
// determine whether the expression
// could be conformed in memory, flush only in case it returns false.
// Note that doesConform method currently implemented on Expression
// requires object(s) to be confirmed as parameter(s).
// The new isComformable method should not take any objects as
// parameters,
// it should return false if there could be such an object that
// passed to doesConform causes it to throw
// QueryException.cannotConformExpression -
// and true otherwise.
boolean shouldResetConformResultsInUnitOfWork = false;
DatabaseQuery query = getDatabaseQueryInternal();
boolean isObjectLevelReadQuery = query.isObjectLevelReadQuery();
if (isFlushModeAUTO() && (!isObjectLevelReadQuery || !((ObjectLevelReadQuery) query).isReadOnly())) {
performPreQueryFlush();
if (isObjectLevelReadQuery) {
if (((ObjectLevelReadQuery) query).shouldConformResultsInUnitOfWork()) {
cloneSharedQuery();
query = getDatabaseQueryInternal();
((ObjectLevelReadQuery) query).setCacheUsage(ObjectLevelReadQuery.UseDescriptorSetting);
shouldResetConformResultsInUnitOfWork = true;
}
}
}
// Set a pessimistic locking on the query if specified.
if (this.lockMode != null && !this.lockMode.equals(LockModeType.NONE)) {
// We need to throw TransactionRequiredException if there is no
// active transaction
this.entityManager.checkForTransaction(true);
// The lock mode setters and getters validate the query type
// so should be safe to make the casting.
cloneSharedQuery();
query = getDatabaseQueryInternal();
// we were unable to set the lock mode.
if (((ObjectLevelReadQuery) query).setLockModeType(lockMode.name(), (AbstractSession) getActiveSession())) {
throw new PersistenceException(ExceptionLocalization.buildMessage("ejb30-wrong-lock_called_without_version_locking-index", null));
}
}
Session session = getActiveSession();
try {
// in case it's a user-defined query
if (query.isUserDefined()) {
// and there is an active transaction
if (this.entityManager.checkForTransaction(false) != null) {
// verify whether uow has begun early transaction
if (session.isUnitOfWork() && !((UnitOfWorkImpl) session).wasTransactionBegunPrematurely()) {
// uow begins early transaction in case it hasn't
// already begun.
// TODO: This is not good, it means that no SQL queries
// can ever use the cache,
// using isUserDefined to mean an SQL query is also
// wrong.
((UnitOfWorkImpl) session).beginEarlyTransaction();
}
}
}
// Execute the query and return the result.
return session.executeQuery(query, parameterValues);
} catch (DatabaseException e) {
throw getDetailedException(e);
} catch (RuntimeException e) {
setRollbackOnly();
throw e;
} finally {
this.lockMode = null;
if (shouldResetConformResultsInUnitOfWork) {
((ObjectLevelReadQuery) query).conformResultsInUnitOfWork();
}
}
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class PersistenceContext method buildQuery.
/**
* Builds the query.
*
* @param tenantId the tenant id
* @param name the name
* @param parameters the parameters
* @param hints the hints
* @return the query
*/
public Query buildQuery(Map<String, String> tenantId, String name, Map<?, ?> parameters, Map<String, ?> hints) {
EntityManager em = getEmf().createEntityManager(tenantId);
Query query = em.createNamedQuery(name);
DatabaseQuery dbQuery = ((EJBQueryImpl<?>) query).getDatabaseQuery();
if (parameters != null) {
Iterator<?> i = parameters.entrySet().iterator();
while (i.hasNext()) {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next();
String key = (String) entry.getKey();
Class<?> parameterClass = null;
int index = dbQuery.getArguments().indexOf(key);
if (index >= 0) {
parameterClass = dbQuery.getArgumentTypes().get(index);
}
Object parameter = entry.getValue();
if (parameterClass != null) {
parameter = ConversionManager.getDefaultManager().convertObject(parameter, parameterClass);
}
query.setParameter(key, parameter);
}
}
if (hints != null) {
for (Map.Entry<String, ?> entry : hints.entrySet()) {
query.setHint(entry.getKey(), entry.getValue());
}
}
return query;
}
Aggregations