List of usage examples for org.hibernate Criteria setTimeout
public Criteria setTimeout(int timeout);
From source file:com.heliosapm.aa4h.parser.XMLQueryParser.java
License:Apache License
/** * Initializes a Criteria Query./*from w w w .j av a 2 s. co m*/ * Mandatory Attributes:<ul> * <li><b>name</b>: The unqualified class name driving the criteria query.</li> * </ul> * Optional Attributes:<ul> * <li><b>prefix</b>: The package name of the class driving the criteria query. If null, no package is assumed.</li> * <li><b>maxSize</b>: The maximum number of rows to return from the database.</li> * <li><b>fetchSize</b>: The number of rows to fetch when rows are requested. Usually not useful for AA4H.</li> * <li><b>cacheEnabled</b>: Enables or disables caching for the queried objects.</li> * <li><b>cacheMode</b>: The cache options for the queried objects.</li> * <li><b>flushMode</b>: The session flush options.</li> * <li><b>fetchMode</b>: The collection fetch options for the query.</li> * <li><b>lockMode</b>: The row lock options for the queried rows.</li> * <li><b>timeOut</b>: The query timeout option.</li> * <li><b>rowCountOnly</b>: Returns a count of the query rows only.</li> * </ul> * @param attrs The attributes of the processed node. * @return An appended or new CriteriaSpecification * @throws SAXException */ protected CriteriaSpecification processCriteria(Attributes attrs) throws SAXException { if (inDetached) { return criteriaStack.peek(); } String name = attrs.getValue("name"); String prefix = attrs.getValue("prefix"); if (prefix != null) { className = prefix + "." + name; } else { className = name; } String maxSize = attrs.getValue("maxSize"); String fetchSize = attrs.getValue("fetchSize"); String firstResult = attrs.getValue("firstResult"); String cacheEnabled = attrs.getValue("cacheEnabled"); String cacheMode = attrs.getValue("cacheMode"); String flushMode = attrs.getValue("flushMode"); String fetchMode = attrs.getValue("fetchMode"); String lockMode = attrs.getValue("lockMode"); String timeOut = attrs.getValue("timeOut"); String rowCountOnly = attrs.getValue("rowCountOnly"); Criteria newCriteria = null; try { if (criteriaStack.size() == 0) { newCriteria = session.createCriteria(className); } else { newCriteria = ((Criteria) criteriaStack.peek()).createCriteria(className); } criteriaStack.push(newCriteria); if ("true".equalsIgnoreCase(rowCountOnly)) { newCriteria.setProjection(Projections.projectionList().add(Projections.rowCount()) ); setRowCountOnly(true); } if (maxSize != null && isRowCountOnly() == false) { newCriteria.setMaxResults(Integer.parseInt(maxSize)); } if (fetchSize != null && isRowCountOnly() == false) { newCriteria.setFetchSize(Integer.parseInt(fetchSize)); } if (firstResult != null && isRowCountOnly() == false) { newCriteria.setFirstResult(Integer.parseInt(firstResult)); } if (timeOut != null) { newCriteria.setTimeout(Integer.parseInt(timeOut)); } if ("true".equalsIgnoreCase(cacheEnabled)) { newCriteria.setCacheable(true); } else if ("false".equalsIgnoreCase(cacheEnabled)) { newCriteria.setCacheable(false); } if (fetchMode != null && fetchMode.length() > 0) { if ("JOIN".equalsIgnoreCase(fetchMode)) { newCriteria.setFetchMode(name, FetchMode.JOIN); } else if ("SELECT".equalsIgnoreCase(fetchMode)) { newCriteria.setFetchMode(name, FetchMode.SELECT); } else { newCriteria.setFetchMode(name, FetchMode.DEFAULT); } } else { newCriteria.setFetchMode(name, FetchMode.DEFAULT); } if (cacheMode != null && cacheMode.length() > 0) { if ("GET".equalsIgnoreCase(cacheMode)) { newCriteria.setCacheMode(CacheMode.GET); } else if ("IGNORE".equalsIgnoreCase(cacheMode)) { newCriteria.setCacheMode(CacheMode.IGNORE); } else if ("NORMAL".equalsIgnoreCase(cacheMode)) { newCriteria.setCacheMode(CacheMode.NORMAL); } else if ("PUT".equalsIgnoreCase(cacheMode)) { newCriteria.setCacheMode(CacheMode.PUT); } else if ("REFRESH".equalsIgnoreCase(cacheMode)) { newCriteria.setCacheMode(CacheMode.REFRESH); } else { newCriteria.setCacheMode(CacheMode.NORMAL); } } if (lockMode != null && lockMode.length() > 0) { if ("NONE".equalsIgnoreCase(lockMode)) { newCriteria.setLockMode(LockMode.NONE); } else if ("READ".equalsIgnoreCase(lockMode)) { newCriteria.setLockMode(LockMode.READ); } else if ("UPGRADE".equalsIgnoreCase(lockMode)) { newCriteria.setLockMode(LockMode.UPGRADE); } else if ("UPGRADE_NOWAIT".equalsIgnoreCase(lockMode)) { newCriteria.setLockMode(LockMode.UPGRADE_NOWAIT); } else if ("WRITE".equalsIgnoreCase(lockMode)) { newCriteria.setLockMode(LockMode.WRITE); } else { throw new SAXException("lockMode[" + lockMode + "] Not Recognized"); } } if (flushMode != null && flushMode.length() > 0) { if ("ALWAYS".equalsIgnoreCase(flushMode)) { newCriteria.setFlushMode(FlushMode.ALWAYS); } else if ("AUTO".equalsIgnoreCase(flushMode)) { newCriteria.setFlushMode(FlushMode.AUTO); } else if ("COMMIT".equalsIgnoreCase(flushMode)) { newCriteria.setFlushMode(FlushMode.COMMIT); } else if ("NEVER".equalsIgnoreCase(flushMode)) { // NEVER is deprecated, so we won't throw an exception but we'll ignore it. } else { throw new SAXException("flushMode[" + flushMode + "] Not Recognized"); } } return newCriteria; } catch (Exception e) { throw new SAXException("Unable to configure class " + className, e); } }
From source file:lucee.runtime.orm.hibernate.HibernateORMSession.java
License:Open Source License
private Object load(PageContext pc, String cfcName, Struct filter, Struct options, String order, boolean unique) throws PageException { Component cfc = data.getEngine().create(pc, this, cfcName, false); Key dsn = KeyImpl.init(ORMUtil.getDataSourceName(pc, cfc)); Session sess = getSession(dsn);//from w ww .j a v a 2 s . c o m String name = HibernateCaster.getEntityName(cfc); ClassMetadata metaData = null; Object rtn; try { //trans.begin(); Criteria criteria = sess.createCriteria(name); // filter if (filter != null && !filter.isEmpty()) { metaData = sess.getSessionFactory().getClassMetadata(name); Object value; Entry<Key, Object> entry; Iterator<Entry<Key, Object>> it = filter.entryIterator(); String colName; while (it.hasNext()) { entry = it.next(); colName = HibernateUtil.validateColumnName(metaData, CommonUtil.toString(entry.getKey())); Type type = HibernateUtil.getPropertyType(metaData, colName, null); value = entry.getValue(); if (!(value instanceof Component)) value = HibernateCaster.toSQL(type, value, null); if (value != null) criteria.add(Restrictions.eq(colName, value)); else criteria.add(Restrictions.isNull(colName)); } } // options boolean ignoreCase = false; if (options != null && !options.isEmpty()) { // ignorecase Boolean ignorecase = CommonUtil.toBoolean(options.get("ignorecase", null), null); if (ignorecase != null) ignoreCase = ignorecase.booleanValue(); // offset int offset = CommonUtil.toIntValue(options.get("offset", null), 0); if (offset > 0) criteria.setFirstResult(offset); // maxResults int max = CommonUtil.toIntValue(options.get("maxresults", null), -1); if (max > -1) criteria.setMaxResults(max); // cacheable Boolean cacheable = CommonUtil.toBoolean(options.get("cacheable", null), null); if (cacheable != null) criteria.setCacheable(cacheable.booleanValue()); // MUST cacheName ? // maxResults int timeout = CommonUtil.toIntValue(options.get("timeout", null), -1); if (timeout > -1) criteria.setTimeout(timeout); } // order if (!Util.isEmpty(order)) { if (metaData == null) metaData = sess.getSessionFactory().getClassMetadata(name); String[] arr = CommonUtil.toStringArray(order, ','); CommonUtil.trimItems(arr); String[] parts; String col; boolean isDesc; Order _order; //ColumnInfo ci; for (int i = 0; i < arr.length; i++) { parts = CommonUtil.toStringArray(arr[i], " \t\n\b\r"); CommonUtil.trimItems(parts); col = parts[0]; col = HibernateUtil.validateColumnName(metaData, col); isDesc = false; if (parts.length > 1) { if (parts[1].equalsIgnoreCase("desc")) isDesc = true; else if (!parts[1].equalsIgnoreCase("asc")) { throw ExceptionUtil.createException((ORMSession) null, null, "invalid order direction defintion [" + parts[1] + "]", "valid values are [asc, desc]"); } } _order = isDesc ? Order.desc(col) : Order.asc(col); if (ignoreCase) _order.ignoreCase(); criteria.addOrder(_order); } } // execute if (!unique) { rtn = HibernateCaster.toCFML(criteria.list()); } else { rtn = HibernateCaster.toCFML(criteria.uniqueResult()); } } catch (Throwable t) { throw CommonUtil.toPageException(t); } return rtn; }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil.java
License:Apache License
/** * Populates criteria arguments for the given target class and arguments map * * @param grailsApplication the GrailsApplication instance * @param targetClass The target class/*w w w . j a va2 s . com*/ * @param c The criteria instance * @param argMap The arguments map * */ @SuppressWarnings("rawtypes") public static void populateArgumentsForCriteria(GrailsApplication grailsApplication, Class<?> targetClass, Criteria c, Map argMap) { Integer maxParam = null; Integer offsetParam = null; SimpleTypeConverter converter = new SimpleTypeConverter(); if (argMap.containsKey(ARGUMENT_MAX)) { maxParam = converter.convertIfNecessary(argMap.get(ARGUMENT_MAX), Integer.class); } if (argMap.containsKey(ARGUMENT_OFFSET)) { offsetParam = converter.convertIfNecessary(argMap.get(ARGUMENT_OFFSET), Integer.class); } if (argMap.containsKey(ARGUMENT_FETCH_SIZE)) { c.setFetchSize(converter.convertIfNecessary(argMap.get(ARGUMENT_FETCH_SIZE), Integer.class)); } if (argMap.containsKey(ARGUMENT_TIMEOUT)) { c.setTimeout(converter.convertIfNecessary(argMap.get(ARGUMENT_TIMEOUT), Integer.class)); } if (argMap.containsKey(ARGUMENT_FLUSH_MODE)) { c.setFlushMode(converter.convertIfNecessary(argMap.get(ARGUMENT_FLUSH_MODE), FlushMode.class)); } if (argMap.containsKey(ARGUMENT_READ_ONLY)) { c.setReadOnly(GrailsClassUtils.getBooleanFromMap(ARGUMENT_READ_ONLY, argMap)); } String orderParam = (String) argMap.get(ARGUMENT_ORDER); Object fetchObj = argMap.get(ARGUMENT_FETCH); if (fetchObj instanceof Map) { Map fetch = (Map) fetchObj; for (Object o : fetch.keySet()) { String associationName = (String) o; c.setFetchMode(associationName, getFetchMode(fetch.get(associationName))); } } final String sort = (String) argMap.get(ARGUMENT_SORT); final String order = ORDER_DESC.equalsIgnoreCase(orderParam) ? ORDER_DESC : ORDER_ASC; final int max = maxParam == null ? -1 : maxParam; final int offset = offsetParam == null ? -1 : offsetParam; if (max > -1) { c.setMaxResults(max); } if (offset > -1) { c.setFirstResult(offset); } if (GrailsClassUtils.getBooleanFromMap(ARGUMENT_CACHE, argMap)) { c.setCacheable(true); } if (GrailsClassUtils.getBooleanFromMap(ARGUMENT_LOCK, argMap)) { c.setLockMode(LockMode.PESSIMISTIC_WRITE); } else { if (argMap.get(ARGUMENT_CACHE) == null) { cacheCriteriaByMapping(targetClass, c); } } if (sort != null) { boolean ignoreCase = true; Object caseArg = argMap.get(ARGUMENT_IGNORE_CASE); if (caseArg instanceof Boolean) { ignoreCase = (Boolean) caseArg; } addOrderPossiblyNested(grailsApplication, c, targetClass, sort, order, ignoreCase); } else { Mapping m = GrailsDomainBinder.getMapping(targetClass); if (m != null && !StringUtils.isBlank(m.getSort())) { addOrderPossiblyNested(grailsApplication, c, targetClass, m.getSort(), m.getOrder(), true); } } }
From source file:org.grails.orm.hibernate.cfg.GrailsHibernateUtil.java
License:Apache License
/** * Populates criteria arguments for the given target class and arguments map * * @param datastore the GrailsApplication instance * @param targetClass The target class//from w ww . j ava 2 s. c o m * @param c The criteria instance * @param argMap The arguments map */ @SuppressWarnings("rawtypes") public static void populateArgumentsForCriteria(AbstractHibernateDatastore datastore, Class<?> targetClass, Criteria c, Map argMap, ConversionService conversionService, boolean useDefaultMapping) { Integer maxParam = null; Integer offsetParam = null; if (argMap.containsKey(ARGUMENT_MAX)) { maxParam = conversionService.convert(argMap.get(ARGUMENT_MAX), Integer.class); } if (argMap.containsKey(ARGUMENT_OFFSET)) { offsetParam = conversionService.convert(argMap.get(ARGUMENT_OFFSET), Integer.class); } if (argMap.containsKey(ARGUMENT_FETCH_SIZE)) { c.setFetchSize(conversionService.convert(argMap.get(ARGUMENT_FETCH_SIZE), Integer.class)); } if (argMap.containsKey(ARGUMENT_TIMEOUT)) { c.setTimeout(conversionService.convert(argMap.get(ARGUMENT_TIMEOUT), Integer.class)); } if (argMap.containsKey(ARGUMENT_FLUSH_MODE)) { c.setFlushMode(convertFlushMode(argMap.get(ARGUMENT_FLUSH_MODE))); } if (argMap.containsKey(ARGUMENT_READ_ONLY)) { c.setReadOnly(ClassUtils.getBooleanFromMap(ARGUMENT_READ_ONLY, argMap)); } String orderParam = (String) argMap.get(ARGUMENT_ORDER); Object fetchObj = argMap.get(ARGUMENT_FETCH); if (fetchObj instanceof Map) { Map fetch = (Map) fetchObj; for (Object o : fetch.keySet()) { String associationName = (String) o; c.setFetchMode(associationName, getFetchMode(fetch.get(associationName))); } } final int max = maxParam == null ? -1 : maxParam; final int offset = offsetParam == null ? -1 : offsetParam; if (max > -1) { c.setMaxResults(max); } if (offset > -1) { c.setFirstResult(offset); } if (ClassUtils.getBooleanFromMap(ARGUMENT_LOCK, argMap)) { c.setLockMode(LockMode.PESSIMISTIC_WRITE); c.setCacheable(false); } else { if (argMap.containsKey(ARGUMENT_CACHE)) { c.setCacheable(ClassUtils.getBooleanFromMap(ARGUMENT_CACHE, argMap)); } else { cacheCriteriaByMapping(targetClass, c); } } final Object sortObj = argMap.get(ARGUMENT_SORT); if (sortObj != null) { boolean ignoreCase = true; Object caseArg = argMap.get(ARGUMENT_IGNORE_CASE); if (caseArg instanceof Boolean) { ignoreCase = (Boolean) caseArg; } if (sortObj instanceof Map) { Map sortMap = (Map) sortObj; for (Object sort : sortMap.keySet()) { final String order = ORDER_DESC.equalsIgnoreCase((String) sortMap.get(sort)) ? ORDER_DESC : ORDER_ASC; addOrderPossiblyNested(datastore, c, targetClass, (String) sort, order, ignoreCase); } } else { final String sort = (String) sortObj; final String order = ORDER_DESC.equalsIgnoreCase(orderParam) ? ORDER_DESC : ORDER_ASC; addOrderPossiblyNested(datastore, c, targetClass, sort, order, ignoreCase); } } else if (useDefaultMapping) { Mapping m = GrailsDomainBinder.getMapping(targetClass); if (m != null) { Map sortMap = m.getSort().getNamesAndDirections(); for (Object sort : sortMap.keySet()) { final String order = ORDER_DESC.equalsIgnoreCase((String) sortMap.get(sort)) ? ORDER_DESC : ORDER_ASC; addOrderPossiblyNested(datastore, c, targetClass, (String) sort, order, true); } } } }
From source file:org.grails.orm.hibernate.GrailsHibernateTemplate.java
License:Apache License
/** * Prepare the given Criteria object, applying cache settings and/or a * transaction timeout./* w w w.j a v a 2s. co m*/ * * @param criteria the Criteria object to prepare */ protected void prepareCriteria(Criteria criteria) { if (cacheQueries) { criteria.setCacheable(true); } if (shouldPassReadOnlyToHibernate()) { criteria.setReadOnly(true); } SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory); if (sessionHolder != null && sessionHolder.hasTimeout()) { criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds()); } }
From source file:org.onecmdb.core.internal.storage.hibernate.HibernateDataSource.java
License:Open Source License
public List evaluate(IExpression e) { Profiler.start("EvaluateList(" + e.toString() + ")"); try {//w ww . ja v a2 s. c o m Session s = sf.openSession(); Criteria criteria = createHibernateCriteria(s, e); if (e.getPageInfo() != null) { criteria.setMaxResults(e.getPageInfo().getPageSize()); criteria.setFirstResult(e.getPageInfo().getPage() * e.getPageInfo().getPageSize()); } criteria.setTimeout(JDBC_TIMEOUT_IN_SEC); if (e.getOrderInfo() != null) { criteria.addOrder((Order) e.getOrderInfo()); } List result = criteria.list(); return (result); } finally { Profiler.stop(); } }
From source file:org.openbravo.service.db.QueryTimeOutUtil.java
License:Open Source License
/** * Sets a timeout for a hibernate criteria (i.e. OBCriteria), if possible * //from www . j a v a 2 s . c o m * @param criteria * @param type * query type, it will be used to fetch the proper timeout */ public void setQueryTimeOut(Criteria criteria, String type) { if (canApplyTimeOut && checkQueryType(type)) { criteria.setTimeout(queryTimeOutMap.get(type)); } }
From source file:org.openbravo.service.db.QueryTimeOutUtil.java
License:Open Source License
/** * Sets the 0 the timeout of a hibernate criteria *//*from w w w. j a v a 2s .co m*/ public static void resetQueryTimeOut(Criteria criteria) { criteria.setTimeout(0); }
From source file:org.springframework.orm.hibernate3.SessionFactoryUtils.java
License:Apache License
/** * Apply the current transaction timeout, if any, to the given * Hibernate Criteria object.//from w w w. j av a 2s. c o m * @param criteria the Hibernate Criteria object * @param sessionFactory Hibernate SessionFactory that the Criteria was created for * @see org.hibernate.Criteria#setTimeout */ public static void applyTransactionTimeout(Criteria criteria, SessionFactory sessionFactory) { Assert.notNull(criteria, "No Criteria object specified"); if (sessionFactory != null) { SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager .getResource(sessionFactory); if (sessionHolder != null && sessionHolder.hasTimeout()) { criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds()); } } }
From source file:org.springframework.orm.hibernate4.HibernateTemplate.java
License:Apache License
/** * Prepare the given Criteria object, applying cache settings and/or * a transaction timeout.//from w w w .j a v a2 s . c o m * @param criteria the Criteria object to prepare * @see #setCacheQueries * @see #setQueryCacheRegion */ protected void prepareCriteria(Criteria criteria) { if (isCacheQueries()) { criteria.setCacheable(true); if (getQueryCacheRegion() != null) { criteria.setCacheRegion(getQueryCacheRegion()); } } if (getFetchSize() > 0) { criteria.setFetchSize(getFetchSize()); } if (getMaxResults() > 0) { criteria.setMaxResults(getMaxResults()); } SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager .getResource(getSessionFactory()); if (sessionHolder != null && sessionHolder.hasTimeout()) { criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds()); } }