List of usage examples for org.hibernate LockMode PESSIMISTIC_WRITE
LockMode PESSIMISTIC_WRITE
To view the source code for org.hibernate LockMode PESSIMISTIC_WRITE.
Click Source Link
From source file:grails.orm.HibernateCriteriaBuilder.java
License:Apache License
/** * Whether a pessimistic lock should be obtained. * * @param shouldLock True if it should// ww w. j av a2 s. c o m */ public void lock(boolean shouldLock) { String lastAlias = getLastAlias(); if (shouldLock) { if (lastAlias != null) { criteria.setLockMode(lastAlias, LockMode.PESSIMISTIC_WRITE); } else { criteria.setLockMode(LockMode.PESSIMISTIC_WRITE); } } else { if (lastAlias != null) { criteria.setLockMode(lastAlias, LockMode.NONE); } else { criteria.setLockMode(LockMode.NONE); } } }
From source file:io.jeandavid.projects.vod.service.DvdOrderFacadeREST.java
License:Open Source License
@POST @Path("{id}/dvd") @Consumes(MediaType.APPLICATION_JSON)//from w ww . j a va 2 s . com public void addDvd(@PathParam("id") Long id, Dvd dvd) { Session session = this.getSessionFactory().openSession(); DvdOrder order = (DvdOrder) session.load(DvdOrder.class, id, LockMode.PESSIMISTIC_WRITE); if (order.getInternalState() != DvdOrder.CREATED) { session.close(); return; } int quantity = dvd.getQuantity(); session.refresh(dvd); Transaction tr = session.beginTransaction(); session.refresh(order); DvdOrderDvd temp = new DvdOrderDvd(); temp.setQuantity(quantity); session.persist(temp); dvd.addDvdOrderDvd(temp); order.addDvdOrderDvd(temp); session.saveOrUpdate(temp); session.saveOrUpdate(dvd); float price = temp.computePrice(); temp.setPrice(price); session.saveOrUpdate(temp); order.updatePrice(order.getPrice() + price); session.save(session.merge(order)); session.flush(); tr.commit(); session.close(); }
From source file:net.e6tech.elements.persist.hibernate.ModifiedTableGenerator.java
License:Apache License
@SuppressWarnings("unchecked") protected String buildSelectQuery(Dialect dialect) { final String alias = "tbl"; final String query = "select " + StringHelper.qualify(alias, valueColumnName) + " from " + renderedTableName + ' ' + alias + " where " + StringHelper.qualify(alias, segmentColumnName) + "=?"; final LockOptions lockOptions = new LockOptions(LockMode.PESSIMISTIC_WRITE); lockOptions.setAliasSpecificLockMode(alias, LockMode.PESSIMISTIC_WRITE); final Map updateTargetColumnsMap = Collections.singletonMap(alias, new String[] { valueColumnName }); return dialect.applyLocksToSql(query, lockOptions, updateTargetColumnsMap); }
From source file:net.kamhon.ieagle.dao.HibernateDao.java
License:Apache License
public T getForUpdate(Class<T> clazz, Serializable serializablekey) { try {//w w w.j a v a 2 s. c o m return getHibernateTemplate().get(clazz, serializablekey, LockMode.PESSIMISTIC_WRITE); } catch (Exception e) { throw new DataException(e); } }
From source file:org.apacheextras.camel.component.hibernate.HibernateConsumer.java
License:Open Source License
/** * A strategy method to lock an object with an exclusive lock so that it can * be processed/*w ww . j ava 2 s. c om*/ * * @param entity the entity to be locked * @param session * @return true if the entity was locked */ protected boolean lockEntity(Object entity, Session session) { if (!getEndpoint().isConsumeDelete() || !getEndpoint().isConsumeLockEntity()) { return true; } try { if (LOG.isDebugEnabled()) { LOG.debug("Acquiring exclusive lock on entity: " + entity); } session.buildLockRequest(LockOptions.UPGRADE).setLockMode(LockMode.PESSIMISTIC_WRITE).setTimeOut(60000) .lock(entity); return true; } catch (Exception e) { if (LOG.isDebugEnabled()) { LOG.debug("Failed to achieve lock on entity: " + entity + ". Reason: " + e, e); } return false; } }
From source file:org.brekka.commons.persistence.dao.hibernate.AbstractIdentifiableEntityHibernateDAO.java
License:Apache License
/** * Borrowed from org.hibernate.ejb.util.LockModeTypeHelper * @param lockMode//www. j a va 2s .co m * @return */ public static LockMode getLockMode(final LockModeType lockMode) { switch (lockMode) { case READ: case OPTIMISTIC: { return LockMode.OPTIMISTIC; } case OPTIMISTIC_FORCE_INCREMENT: case WRITE: { return LockMode.OPTIMISTIC_FORCE_INCREMENT; } case PESSIMISTIC_READ: { return LockMode.PESSIMISTIC_READ; } case PESSIMISTIC_WRITE: { return LockMode.PESSIMISTIC_WRITE; } case PESSIMISTIC_FORCE_INCREMENT: { return LockMode.PESSIMISTIC_FORCE_INCREMENT; } case NONE: { return LockMode.NONE; } default: { throw new IllegalStateException("Unknown LockModeType: " + lockMode); } } }
From source file:org.codehaus.groovy.grails.orm.hibernate.AbstractHibernateSession.java
License:Apache License
public void lock(Object o) { hibernateTemplate.lock(o, LockMode.PESSIMISTIC_WRITE); }
From source file:org.codehaus.groovy.grails.orm.hibernate.AbstractHibernateSession.java
License:Apache License
public <T> T lock(Class<T> type, Serializable key) { return hibernateTemplate.get(type, key, LockMode.PESSIMISTIC_WRITE); }
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// ww w. j av a 2s .c om * @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 w w . jav a 2s . c om*/ * @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); } } } }