List of usage examples for org.hibernate LockOptions setTimeOut
public LockOptions setTimeOut(int timeout)
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. java 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:com.sam.moca.dao.hibernate.AbstractUnknownKeyHibernateDAO.java
License:Open Source License
/** * This will lock the given and update at the same time. Note that any * children objects may possibly be stale and if needed this object * should be reread after locking./*from w w w.j ava 2s . c om*/ * Depending on the database provider there is no guarantee as to whether * the wait argument is paid attention to. * @param obj The object to lock the row on * @param wait Whether to wait for the lock or timeout * @return whether or not the lock was obtained */ public boolean lockRow(T obj, boolean wait) { try { LockOptions opts = new LockOptions(); if (wait) { opts.setLockMode(LockMode.PESSIMISTIC_WRITE); opts.setTimeOut(LockOptions.WAIT_FOREVER); } else { opts.setLockMode(LockMode.UPGRADE_NOWAIT); opts.setTimeOut(LockOptions.NO_WAIT); } HibernateTools.getSession().refresh(obj, opts); return true; } catch (LockAcquisitionException e) { return false; } }
From source file:org.brekka.commons.persistence.dao.hibernate.AbstractIdentifiableEntityHibernateDAO.java
License:Apache License
@SuppressWarnings("unchecked") @Override//from w w w. ja v a 2s. c o m public Entity retrieveById(final ID entityId, final LockModeType lockModeType, final int timeout, final TimeUnit timeUnit) { Session session = getCurrentSession(); LockMode lockMode = getLockMode(lockModeType); LockOptions lockOptions = new LockOptions(lockMode); if (timeout > -1) { lockOptions.setTimeOut((int) timeUnit.toMillis(timeout)); } Entity entity = (Entity) session.get(type(), entityId, lockOptions); preRead(entity); return entity; }
From source file:org.brekka.commons.persistence.dao.hibernate.AbstractIdentifiableEntityHibernateDAO.java
License:Apache License
protected Entity queryById(final ID entityId, final String idFieldName, final String hql, final LockModeType lockModeType, final int timeout, final TimeUnit timeUnit) { LockMode lockMode = getLockMode(lockModeType); LockOptions lockOptions = new LockOptions(lockMode); if (timeout > -1) { lockOptions.setTimeOut((int) timeUnit.toMillis(timeout)); }/* ww w . j av a2s.co m*/ Entity entity = type().cast( getCurrentSession().createQuery(hql).setLockOptions(lockOptions).setParameter(idFieldName, entityId) .setResultTransformer(FirstResultTransformer.INSTANCE).uniqueResult()); preRead(entity); return entity; }
From source file:org.kaaproject.kaa.server.common.dao.impl.sql.HibernateAbstractDao.java
License:Apache License
@Override public Session.LockRequest lockRequest(LockOptions lockOptions) { int timeout = lockOptions.getTimeOut(); if (timeout > MAX_TIMEOUT) { lockOptions.setTimeOut(MAX_TIMEOUT); }/*from w ww . jav a 2 s . co m*/ LOG.debug("Build lock request with options {}", lockOptions); return getSession().buildLockRequest(lockOptions); }
From source file:org.yes.cart.dao.impl.GenericDAOHibernateImpl.java
License:Apache License
/** * {@inheritDoc}//from w w w . ja va2 s . com */ @SuppressWarnings("unchecked") public List<T> findByNamedQueryForUpdate(final String namedQueryName, final int timeout, final Object... parameters) { Query query = sessionFactory.getCurrentSession().getNamedQuery(namedQueryName); LockOptions opts = new LockOptions(LockMode.PESSIMISTIC_WRITE); opts.setTimeOut(timeout); query.setLockOptions(opts); if (parameters != null) { setQueryParameters(query, parameters); } return query.list(); }