Example usage for javax.persistence LockModeType NONE

List of usage examples for javax.persistence LockModeType NONE

Introduction

In this page you can find the example usage for javax.persistence LockModeType NONE.

Prototype

LockModeType NONE

To view the source code for javax.persistence LockModeType NONE.

Click Source Link

Document

No lock.

Usage

From source file:com.confighub.core.store.Store.java

public Collection<CtxLevel> getLevelsForDepth(final Repository repository, final UserAccount user,
        final Depth depth) throws ConfigException {
    if (null == repository) {
        throw new ConfigException(Error.Code.MISSING_PARAMS);
    }/*from w  w  w .  j  a  va 2  s .co m*/

    if (!repository.hasReadAccess(user)) {
        throw new ConfigException(Error.Code.USER_ACCESS_DENIED);
    }

    try {
        return em.createNamedQuery("Level.getForDepth").setLockMode(LockModeType.NONE)
                .setParameter("repository", repository).setParameter("depth", depth).getResultList();
    } catch (NoResultException e) {
        return null;
    } catch (Exception e) {
        handleException(e);
    }

    return null;
}

From source file:com.confighub.core.store.Store.java

public CtxLevel getLevel(final String levelName, final Depth depth, final Repository repository,
        final Date time) throws ConfigException {
    if (Utils.anyNull(levelName, repository)) {
        return null;
    }/*from  w  w w .  ja va2 s. c  o m*/

    if (null == time) {
        try {
            String levelUpper = levelName.toUpperCase();

            return (CtxLevel) em.createNamedQuery("Level.getByName").setLockMode(LockModeType.NONE)
                    .setParameter("repository", repository).setParameter("name", levelUpper)
                    .setParameter("depth", depth).getSingleResult();
        } catch (NoResultException e) {
            return null;
        } catch (Exception e) {
            handleException(e);
        }

        return null;
    }

    AuditReader reader = AuditReaderFactory.get(em);
    Number rev = reader.getRevisionNumberForDate(time);

    // ToDo will not return deleted level
    AuditQuery query = reader.createQuery().forEntitiesAtRevision(CtxLevel.class, rev);
    query.add(AuditEntity.property("repository").eq(repository));
    query.add(AuditEntity.property("name").eq(levelName));
    query.add(AuditEntity.property("depth").eq(depth));

    try {
        return (CtxLevel) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    } catch (Exception e) {
        handleException(e);
        return null;
    }
}

From source file:com.confighub.core.store.Store.java

public Property getProperty(final UserAccount user, final Repository repository, final Long propertyId)
        throws ConfigException {
    if (Utils.anyNull(user, repository, propertyId)) {
        return null;
    }/*ww  w  . j a  v a2  s.  c om*/

    if (!repository.hasWriteAccess(user)) {
        throw new ConfigException(Error.Code.USER_ACCESS_DENIED);
    }

    try {
        return (Property) em.createNamedQuery("Property.get").setLockMode(LockModeType.NONE)
                .setParameter("repository", repository).setParameter("id", propertyId).getSingleResult();
    } catch (NoResultException e) {
        return null;
    } catch (Exception e) {
        handleException(e);
        return null;
    }
}

From source file:org.apache.openjpa.persistence.EntityManagerImpl.java

/**
 * Populate the given FetchPlan with the given properties. 
 * Optionally overrides the given lock mode.
 *//*from  ww  w. j  av a  2  s. c o  m*/
private void configureCurrentFetchPlan(FetchPlan fetch, Map<String, Object> properties, LockModeType lock,
        boolean requiresTxn) {
    // handle properties in map first
    if (properties != null) {
        for (Map.Entry<String, Object> entry : properties.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (key.equals("javax.persistence.lock.scope")) {
                fetch.setLockScope((PessimisticLockScope) value);
            } else
                fetch.setHint(key, value);
        }
    }
    // override with the specific lockMode, if needed.
    if (lock != null && lock != LockModeType.NONE) {
        if (requiresTxn) {
            _broker.assertActiveTransaction();
        }
        // Override read lock level
        LockModeType curReadLockMode = fetch.getReadLockMode();
        if (lock != curReadLockMode)
            fetch.setReadLockMode(lock);
    }
}

From source file:org.apache.openjpa.persistence.XMLPersistenceMetaDataParser.java

/**
 * Parse named-query./*from   w  w w.  java 2  s. c om*/
 */
protected boolean startNamedQuery(Attributes attrs) throws SAXException {
    if (!isQueryMode())
        return false;

    String name = attrs.getValue("name");
    Log log = getLog();
    if (log.isTraceEnabled())
        log.trace(_loc.get("parse-query", name));

    QueryMetaData meta = new QueryMetaData(_cls, name, JPQLParser.LANG_JPQL);

    meta.setQueryString(attrs.getValue("query"));
    String lockModeStr = attrs.getValue("lock-mode");
    LockModeType lmt = processNamedQueryLockModeType(log, lockModeStr, name);
    if (lmt != null && lmt != LockModeType.NONE) {
        meta.addHint("openjpa.FetchPlan.ReadLockMode", lmt);
    }
    Locator locator = getLocation().getLocator();
    if (locator != null) {
        meta.setLineNumber(locator.getLineNumber());
        meta.setColNumber(locator.getColumnNumber());
    }
    Object cur = currentElement();
    Object scope = (cur instanceof ClassMetaData) ? ((ClassMetaData) cur).getDescribedType() : null;
    meta.setSource(getSourceFile(), scope, SourceTracker.SRC_XML, locator == null ? "" : locator.getSystemId());
    if (isMetaDataMode())
        meta.setSourceMode(MODE_META);
    else if (isMappingMode())
        meta.setSourceMode(MODE_MAPPING);
    else
        meta.setSourceMode(MODE_QUERY);

    addQueryMetaData(cur, meta);
    pushElement(meta);
    return true;
}

From source file:org.apache.openjpa.persistence.XMLPersistenceMetaDataParser.java

/**
 * A private worker method that calculates the lock mode for an individual NamedQuery. If the NamedQuery is 
 * configured to use the NONE lock mode(explicit or implicit), this method will promote the lock to a READ
 * level lock. This was done to allow for JPA1 apps to function properly under a 2.0 runtime. 
 *///from   w  ww .  j a va 2 s . c om
private LockModeType processNamedQueryLockModeType(Log log, String lockModeString, String queryName) {
    if (lockModeString == null) {
        return null;
    }
    LockModeType lmt = LockModeType.valueOf(lockModeString);
    String lm = _conf.getLockManager();
    boolean optimistic = _conf.getOptimistic();
    if (lm != null) {
        lm = lm.toLowerCase();
        if (lm.contains("pessimistic")) {
            if (lmt == LockModeType.NONE && !optimistic) {
                if (log != null && log.isWarnEnabled() == true) {
                    log.warn(_loc.get("override-named-query-lock-mode",
                            new String[] { "xml", queryName, _cls.getName() }));
                }
                lmt = LockModeType.READ;
            }
        }
    }

    return lmt;
}

From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.java

/**
 * Parse @NamedQuery./* w  w  w. java  2  s  .co m*/
 */
private void parseNamedQueries(AnnotatedElement el, NamedQuery... queries) {
    for (NamedQuery query : queries) {
        if (StringUtils.isEmpty(query.name()))
            throw new MetaDataException(_loc.get("no-query-name", el));
        if (StringUtils.isEmpty(query.query()))
            throw new MetaDataException(_loc.get("no-query-string", query.name(), el));

        if (_log.isTraceEnabled())
            _log.trace(_loc.get("parse-query", query.name()));

        QueryMetaData meta = new QueryMetaData(_cls, query.name(), JPQLParser.LANG_JPQL);
        addQueryMetaData(el, meta);

        meta.setQueryString(query.query());
        for (QueryHint hint : query.hints())
            meta.addHint(hint.name(), hint.value());
        LockModeType lmt = processNamedQueryLockModeType(query);
        if (lmt != null && lmt != LockModeType.NONE) {
            meta.addHint("openjpa.FetchPlan.ReadLockMode", lmt);
        }

        meta.setSource(getSourceFile(), (el instanceof Class) ? el : null, SourceTracker.SRC_ANNOTATIONS,
                getSourceFile() == null ? "" : getSourceFile().getPath());
        if (isMetaDataMode())
            meta.setSourceMode(MODE_META);
        else if (isMappingMode())
            meta.setSourceMode(MODE_MAPPING);
        else
            meta.setSourceMode(MODE_QUERY);
    }
}

From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.java

/**
 * A private worker method that calculates the lock mode for an individual NamedQuery. If the NamedQuery is 
 * configured to use the NONE lock mode(explicit or implicit), this method will promote the lock to a READ
 * level lock. This was done to allow for JPA1 apps to function properly under a 2.0 runtime. 
 *//* www.j  a va 2 s .com*/
private LockModeType processNamedQueryLockModeType(NamedQuery query) {
    LockModeType lmt = query.lockMode();
    if (query.lockMode() != null) {
        String lm = _conf.getLockManager();
        boolean optimistic = _conf.getOptimistic();
        if (lm != null) {
            lm = lm.toLowerCase();
            if (lm.contains("pessimistic")) {
                if (lmt == LockModeType.NONE && !optimistic) {
                    if (_log.isWarnEnabled() == true) {
                        _log.warn(_loc.get("override-named-query-lock-mode",
                                new String[] { "annotation", query.name(), _cls.getName() }));
                    }
                    lmt = LockModeType.READ;
                }
            }
        }
    }
    return lmt;
}

From source file:com.confighub.core.store.Store.java

/**
 * Returns the key from a specific date/*from   w  w w  .  j  av a 2 s  .  c  o  m*/
 *
 * @param repository
 * @param key
 * @param date
 * @return
 */
public PropertyKey getKey(final Repository repository, final String key, final Date date) {
    if (null == repository || Utils.isBlank(key)) {
        return null;
    }

    try {
        if (null == date) {
            return (PropertyKey) em.createNamedQuery("Key.getByKey").setLockMode(LockModeType.NONE)
                    .setParameter("key", key.toUpperCase()).setParameter("repository", repository)
                    .getSingleResult();
        }

        AuditReader reader = AuditReaderFactory.get(em);
        Number rev = reader.getRevisionNumberForDate(null == date ? new Date() : date);

        AuditQuery kq = reader.createQuery().forEntitiesAtRevision(PropertyKey.class, rev);
        kq.add(AuditEntity.property("repository").eq(repository));
        kq.add(AuditEntity.property("key").eq(key));

        return (PropertyKey) kq.getSingleResult();
    } catch (NoResultException e) {
        return null;
    } catch (Exception e) {
        handleException(e);
        return null;
    }
}

From source file:com.confighub.core.store.Store.java

/**
 * @param searchTerm//from  w ww  .j  av  a  2 s  .  c  om
 * @param max
 * @param repository
 * @return
 */
public List<PropertyKey> searchKey(final String searchTerm, int max, Repository repository) {
    if (null == repository || Utils.isBlank(searchTerm)) {
        return null;
    }

    try {
        return em.createNamedQuery("Key.search").setLockMode(LockModeType.NONE)
                .setParameter("searchTerm", "%" + searchTerm.toUpperCase() + "%")
                .setParameter("repository", repository).setMaxResults(max <= 0 ? 10 : (max > 100 ? 100 : max))
                .getResultList();
    } catch (NoResultException e) {
        return null;
    } catch (Exception e) {
        handleException(e);
        return null;
    }
}