Example usage for org.hibernate.engine.spi RowSelection setTimeout

List of usage examples for org.hibernate.engine.spi RowSelection setTimeout

Introduction

In this page you can find the example usage for org.hibernate.engine.spi RowSelection setTimeout.

Prototype

public void setTimeout(int timeout) 

Source Link

Usage

From source file:com.blazebit.persistence.integration.hibernate.base.HibernateExtendedQuerySupport.java

License:Apache License

private QueryParamEntry createQueryParameters(EntityManager em, List<Query> participatingQueries,
        List<String> queryStrings, Set<String> querySpaces) {
    List<ParameterSpecification> parameterSpecifications = new ArrayList<ParameterSpecification>();

    List<Type> types = new ArrayList<Type>();
    List<Object> values = new ArrayList<Object>();
    Map<String, TypedValue> namedParams = new LinkedHashMap<String, TypedValue>();
    Serializable collectionKey = null;
    LockOptions lockOptions = new LockOptions();
    RowSelection rowSelection = new RowSelection();
    boolean readOnly = false; // TODO: readonly?
    boolean cacheable = false; // TODO: cacheable?
    String cacheRegion = null;//ww w .  j a va 2  s. c  o m
    String comment = null;
    List<String> queryHints = null;

    for (QueryParamEntry queryParamEntry : getQueryParamEntries(em, participatingQueries, querySpaces)) {
        queryStrings.add(queryParamEntry.queryString);

        QueryParameters participatingQueryParameters = queryParamEntry.queryParameters;
        // Merge parameters
        Collections.addAll(types, participatingQueryParameters.getPositionalParameterTypes());
        Collections.addAll(values, participatingQueryParameters.getPositionalParameterValues());
        namedParams.putAll(participatingQueryParameters.getNamedParameters());
        parameterSpecifications.addAll(queryParamEntry.specifications);

        // Merge row selections
        if (participatingQueryParameters.hasRowSelection()) {
            RowSelection original = queryParamEntry.queryParameters.getRowSelection();
            // Check for defaults

            /***************************************************************************
             * TODO: Either we do it like this, or let these values be passed in separately
             **************************************************************************/

            if (rowSelection.getFirstRow() == null || rowSelection.getFirstRow() < 1) {
                rowSelection.setFirstRow(original.getFirstRow());
            } else if (original.getFirstRow() != null && original.getFirstRow() > 0
                    && !original.getFirstRow().equals(rowSelection.getFirstRow())) {
                throw new IllegalStateException("Multiple row selections not allowed!");
            }
            if (rowSelection.getMaxRows() == null || rowSelection.getMaxRows() == Integer.MAX_VALUE) {
                rowSelection.setMaxRows(original.getMaxRows());
            } else if (original.getMaxRows() != null && original.getMaxRows() != Integer.MAX_VALUE
                    && !original.getMaxRows().equals(rowSelection.getMaxRows())) {
                throw new IllegalStateException("Multiple row selections not allowed!");
            }

            if (rowSelection.getFetchSize() == null) {
                rowSelection.setFetchSize(original.getFetchSize());
            } else if (original.getFetchSize() != null
                    && !original.getFetchSize().equals(rowSelection.getFetchSize())) {
                throw new IllegalStateException("Multiple row selections not allowed!");
            }
            if (rowSelection.getTimeout() == null) {
                rowSelection.setTimeout(original.getTimeout());
            } else if (original.getTimeout() != null
                    && !original.getTimeout().equals(rowSelection.getTimeout())) {
                throw new IllegalStateException("Multiple row selections not allowed!");
            }
        }

        // Merge lock options
        LockOptions originalLockOptions = participatingQueryParameters.getLockOptions();
        if (originalLockOptions.getScope()) {
            lockOptions.setScope(true);
        }
        if (originalLockOptions.getLockMode() != LockMode.NONE) {
            if (lockOptions.getLockMode() != LockMode.NONE
                    && lockOptions.getLockMode() != originalLockOptions.getLockMode()) {
                throw new IllegalStateException("Multiple different lock modes!");
            }
            lockOptions.setLockMode(originalLockOptions.getLockMode());
        }
        if (originalLockOptions.getTimeOut() != -1) {
            if (lockOptions.getTimeOut() != -1
                    && lockOptions.getTimeOut() != originalLockOptions.getTimeOut()) {
                throw new IllegalStateException("Multiple different lock timeouts!");
            }
            lockOptions.setTimeOut(originalLockOptions.getTimeOut());
        }
        @SuppressWarnings("unchecked")
        Iterator<Map.Entry<String, LockMode>> aliasLockIter = participatingQueryParameters.getLockOptions()
                .getAliasLockIterator();
        while (aliasLockIter.hasNext()) {
            Map.Entry<String, LockMode> entry = aliasLockIter.next();
            lockOptions.setAliasSpecificLockMode(entry.getKey(), entry.getValue());
        }
    }

    QueryParameters queryParameters = hibernateAccess.createQueryParameters(
            types.toArray(new Type[types.size()]), values.toArray(new Object[values.size()]), namedParams,
            lockOptions, rowSelection, true, readOnly, cacheable, cacheRegion, comment, queryHints,
            collectionKey == null ? null : new Serializable[] { collectionKey });

    return new QueryParamEntry(null, queryParameters, parameterSpecifications);
}

From source file:org.babyfish.hibernate.hql.XQueryPlan.java

License:Open Source License

public <T> List<T> performList(SessionImplementor session, QueryParameters queryParameters,
        QueryType queryMode) {//from   www  . ja va  2 s.c om
    if (log.isTraceEnabled()) {
        log.trace("find: " + getSourceQuery());
        queryParameters.traceParameters(session.getFactory());
    }
    QueryTranslator[] translators = this.getTranslators();
    boolean hasLimit = queryParameters.getRowSelection() != null
            && queryParameters.getRowSelection().definesLimits();
    boolean needsMemoryLimit = hasLimit && translators.length > 1;
    QueryParameters queryParametersToUse;
    if (needsMemoryLimit) {
        if (!SettingsFactory.isLimitInMemoryEnabled(session.getFactory().getProperties())) {
            throw new QueryException(LAZY_RESOURCE.get().hibernateLimitInMemoryForPolymorphicQueryIsNotEnabled(
                    SettingsFactory.ENABLE_LIMIT_IN_MEMORY));
        }
        log.warn("firstResult/maxResults specified on polymorphic query; applying in memory!");
        RowSelection selection = new RowSelection();
        selection.setFetchSize(queryParameters.getRowSelection().getFetchSize());
        selection.setTimeout(queryParameters.getRowSelection().getTimeout());
        queryParametersToUse = queryParameters.createCopyUsing(selection);
    } else {
        queryParametersToUse = queryParameters;
    }

    List<T> combinedResults = new ArrayList<T>();
    Set<T> distinction = new LinkedHashSet<T>(ReferenceEqualityComparator.getInstance());
    int includedCount = -1;
    translator_loop: for (int i = 0; i < translators.length; i++) {
        List<T> tmp = ((XQueryTranslator) translators[i]).list(session, queryParametersToUse, queryMode);
        if (needsMemoryLimit) {
            // NOTE : firstRow is zero-based
            int first = queryParameters.getRowSelection().getFirstRow() == null ? 0
                    : queryParameters.getRowSelection().getFirstRow().intValue();
            int max = queryParameters.getRowSelection().getMaxRows() == null ? -1
                    : queryParameters.getRowSelection().getMaxRows().intValue();
            final int size = tmp.size();
            for (int x = 0; x < size; x++) {
                final T result = tmp.get(x);
                if (!distinction.add(result)) {
                    continue;
                }
                includedCount++;
                if (includedCount < first) {
                    continue;
                }
                combinedResults.add(result);
                if (max >= 0 && includedCount > max) {
                    break translator_loop; // break the outer loop !!!
                }
            }
        } else {
            if (translators.length == 1) {
                return tmp;
            } else {
                combinedResults.addAll(tmp);
            }
        }
    }
    return combinedResults;
}

From source file:org.babyfish.hibernate.loader.UnlimitedCountLoader.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//w w w  .  j  a  v  a2s.  c o  m
public final List<Long> list(SessionImplementor session, QueryParameters queryParameters)
        throws HibernateException {
    boolean hasLimit = queryParameters.getRowSelection() != null
            && queryParameters.getRowSelection().definesLimits();
    if (hasLimit) {
        RowSelection selection = new RowSelection();
        selection.setFetchSize(queryParameters.getRowSelection().getFetchSize());
        selection.setTimeout(queryParameters.getRowSelection().getTimeout());
        queryParameters = queryParameters.createCopyUsing(selection);
    }
    return super.list(session, queryParameters);
}