Example usage for org.hibernate Session lock

List of usage examples for org.hibernate Session lock

Introduction

In this page you can find the example usage for org.hibernate Session lock.

Prototype

void lock(Object object, LockMode lockMode);

Source Link

Document

Obtain the specified lock level upon the given object.

Usage

From source file:com.cloud.bridge.service.core.s3.S3Engine.java

License:Open Source License

/**
 * If acl is set then the cannedAccessPolicy parameter should be null and is ignored.   
 * The cannedAccessPolicy parameter is for REST Put requests only where a simple set of ACLs can be
 * created with a single header value.  Note that we do not currently support "anonymous" un-authenticated 
 * access in our implementation.//from   w  ww.j  ava  2  s.co  m
 * 
 * @throws IOException 
 */
@SuppressWarnings("deprecation")
public OrderedPair<SObject, SObjectItem> allocObjectItem(SBucket bucket, String nameKey, S3MetaDataEntry[] meta,
        S3AccessControlList acl, String cannedAccessPolicy) {
    SObjectDao objectDao = new SObjectDao();
    SObjectItemDao objectItemDao = new SObjectItemDao();
    SMetaDao metaDao = new SMetaDao();
    SAclDao aclDao = new SAclDao();
    SObjectItem item = null;
    int versionSeq = 1;
    int versioningStatus = bucket.getVersioningStatus();

    Session session = PersistContext.getSession();

    // [A] To write into a bucket the user must have write permission to that bucket
    S3PolicyContext context = new S3PolicyContext(PolicyActions.PutObject, bucket.getName());
    context.setKeyName(nameKey);
    context.setEvalParam(ConditionKeys.Acl, cannedAccessPolicy);

    verifyAccess(context, "SBucket", bucket.getId(), SAcl.PERMISSION_WRITE); // TODO - check this validates plain POSTs

    // [B] If versioning is off them we over write a null object item
    SObject object = objectDao.getByNameKey(bucket, nameKey);
    if (object != null) {
        // -> if versioning is on create new object items
        if (SBucket.VERSIONING_ENABLED == versioningStatus) {
            session.lock(object, LockMode.UPGRADE);
            versionSeq = object.getNextSequence();
            object.setNextSequence(versionSeq + 1);
            session.save(object);

            item = new SObjectItem();
            item.setTheObject(object);
            object.getItems().add(item);
            item.setVersion(String.valueOf(versionSeq));
            Date ts = DateHelper.currentGMTTime();
            item.setCreateTime(ts);
            item.setLastAccessTime(ts);
            item.setLastModifiedTime(ts);
            session.save(item);
        } else { // -> find an object item with a null version, can be null
                 //    if bucket started out with versioning enabled and was then suspended
            item = objectItemDao.getByObjectIdNullVersion(object.getId());
            if (item == null) {
                item = new SObjectItem();
                item.setTheObject(object);
                object.getItems().add(item);
                Date ts = DateHelper.currentGMTTime();
                item.setCreateTime(ts);
                item.setLastAccessTime(ts);
                item.setLastModifiedTime(ts);
                session.save(item);
            }
        }
    } else { // -> there is no object nor an object item
        object = new SObject();
        object.setBucket(bucket);
        object.setNameKey(nameKey);
        object.setNextSequence(2);
        object.setCreateTime(DateHelper.currentGMTTime());
        object.setOwnerCanonicalId(UserContext.current().getCanonicalUserId());
        session.save(object);

        item = new SObjectItem();
        item.setTheObject(object);
        object.getItems().add(item);
        if (SBucket.VERSIONING_ENABLED == versioningStatus)
            item.setVersion(String.valueOf(versionSeq));
        Date ts = DateHelper.currentGMTTime();
        item.setCreateTime(ts);
        item.setLastAccessTime(ts);
        item.setLastModifiedTime(ts);
        session.save(item);
    }

    // [C] We will use the item DB id as the file name, MD5/contentLength will be stored later
    String suffix = null;
    int dotPos = nameKey.lastIndexOf('.');
    if (dotPos >= 0)
        suffix = nameKey.substring(dotPos);
    if (suffix != null)
        item.setStoredPath(String.valueOf(item.getId()) + suffix);
    else
        item.setStoredPath(String.valueOf(item.getId()));

    metaDao.save("SObjectItem", item.getId(), meta);

    // [D] Are we setting an ACL along with the object
    //  -> the ACL is ALWAYS set on a particular instance of the object (i.e., a version)
    if (null != cannedAccessPolicy) {
        setCannedAccessControls(cannedAccessPolicy, "SObjectItem", item.getId(), bucket);
    } else if (null == acl || 0 == acl.size()) {
        // -> this is termed the "private" or default ACL, "Owner gets FULL_CONTROL"
        setSingleAcl("SObjectItem", item.getId(), SAcl.PERMISSION_FULL);
    } else if (null != acl) {
        aclDao.save("SObjectItem", item.getId(), acl);
    }

    session.update(item);
    return new OrderedPair<SObject, SObjectItem>(object, item);
}

From source file:com.jdon.persistence.hibernate.HibernateTemplate.java

License:Apache License

public void lock(final Object entity, final LockMode lockMode) throws Exception {
    doHibernate(new HibernateCallback() {
        public Object execute(Session session) throws HibernateException {
            session.lock(entity, lockMode);
            return null;
        }//from   w w  w . j a  v  a 2s .  c o m
    });
}

From source file:com.jdon.persistence.hibernate.HibernateTemplate.java

License:Apache License

public void update(final Object entity, final LockMode lockMode) throws Exception {
    doHibernate(new HibernateCallback() {
        public Object execute(Session session) throws HibernateException {
            session.update(entity);//from   ww w . j  ava2 s .co  m
            if (lockMode != null) {
                session.lock(entity, lockMode);
            }
            return null;
        }
    });
}

From source file:com.jdon.persistence.hibernate.HibernateTemplate.java

License:Apache License

public void update(final String entityName, final Object entity, final LockMode lockMode) throws Exception {

    doHibernate(new HibernateCallback() {
        public Object execute(Session session) throws HibernateException {
            session.update(entityName, entity);
            if (lockMode != null) {
                session.lock(entity, lockMode);
            }/* w w w . j  a  v  a  2 s.c om*/
            return null;
        }
    });
}

From source file:com.jdon.persistence.hibernate.HibernateTemplate.java

License:Apache License

public void delete(final Object entity, final LockMode lockMode) throws Exception {
    doHibernate(new HibernateCallback() {
        public Object execute(Session session) throws HibernateException {
            if (lockMode != null) {
                session.lock(entity, lockMode);
            }/*from  ww w .j a  va2s  .c  o m*/
            session.delete(entity);
            return null;
        }
    });
}

From source file:com.knowbout.hibernate.model.PersistentObject.java

License:Apache License

protected void reattach(Object object) {
    Session session = HibernateUtil.currentSession();
    if (!session.contains(object)) {
        session.lock(object, LockMode.NONE);
    }/*www  .  j  a va2 s.  co m*/
}

From source file:com.mg.framework.support.report.ReportUtils.java

License:Open Source License

/**
 * ? ? ?  ?  ??//from   ww  w.j  a v  a 2s .  co  m
 *
 * @param entity ??
 * @return ? ?
 */
public static String createEntityParam(Object entity) {
    if (entity == null)
        return null;
    // ??  ?,       
    //${entity:<entity name>#<identificator value>}
    Session session = (Session) ServerUtils.getPersistentManager().getDelegate();
    session.lock(entity, LockMode.NONE);//reassociate a transient instance with a session
    return createEntityParam(session.getEntityName(entity), session.getIdentifier(entity));
}

From source file:de.nava.informa.utils.manager.hibernate.HibernateUtil.java

License:Open Source License

/**
 * Makes a try to lock object. This will save us great number of SQL statements
 * in several cases. It's not a big problem if locking is not possible.
 *
 * @param o object to lock.//  ww  w.ja v a  2 s.c o  m
 * @param s session to lock object in.
 */
public static void lock(Object o, Session s) {
    try {
        s.lock(o, LockMode.NONE);
    } catch (HibernateException e) {
        // Well, it's possible that object is dirty.
    }
}

From source file:de.tuclausthal.submissioninterface.persistence.dao.impl.GroupDAO.java

License:Open Source License

@Override
public void deleteGroup(Group group) {
    Session session = getSession();
    session.lock(group, LockMode.UPGRADE);
    for (Participation participation : group.getMembers()) {
        participation.setGroup(null);/*ww  w.  j a v  a  2  s  . co  m*/
        session.update(participation);
    }
    session.update(group);
    session.delete(group);
}

From source file:de.tuclausthal.submissioninterface.persistence.dao.impl.PointsDAO.java

License:Open Source License

@Override
public Points createPoints(int issuedPoints, Submission submission, Participation participation,
        String publicComment, String internalComment, PointStatus pointStatus, Integer duplicate) {
    Session session = getSession();

    session.lock(submission, LockMode.UPGRADE);
    session.lock(submission.getTask(), LockMode.UPGRADE);
    if (issuedPoints % submission.getTask().getMinPointStep() != 0) {
        issuedPoints = (issuedPoints / submission.getTask().getMinPointStep())
                * submission.getTask().getMinPointStep();
    }/*from w  w w  .  j  a va2 s. c  o  m*/
    if (issuedPoints < 0) {
        issuedPoints = 0;
    } else if (issuedPoints > submission.getTask().getMaxPoints()) {
        issuedPoints = submission.getTask().getMaxPoints();
    }
    Points oldPoints = submission.getPoints();
    Points points = new Points();
    points.setPoints(issuedPoints);
    points.setPointStatus(pointStatus);
    points.setDuplicate(duplicate);
    points.setIssuedBy(participation);
    submission.setPoints(points);
    points.setPublicComment(publicComment);
    points.setInternalComment(internalComment);
    session.save(submission);

    // TODO: Attention: see @MarkApproved.java and below!!!
    if (oldPoints != null) {
        boolean changed = false;
        if (!oldPoints.getPointStatus().equals(points.getPointStatus())) {
            storeInHistory(submission, "status", PointStatus.values()[oldPoints.getPointStatus()].toString(),
                    PointStatus.values()[points.getPointStatus()].toString(), participation);
            if (!((oldPoints.getPointStatus() == PointStatus.NICHT_ABGENOMMEN.ordinal()
                    && points.getPointStatus() == PointStatus.ABGENOMMEN.ordinal())
                    || (oldPoints.getPointStatus() == PointStatus.NICHT_ABGENOMMEN.ordinal()
                            && points.getPointStatus() == PointStatus.ABGENOMMEN_FAILED.ordinal()))) {
                changed = true;
            }
        }
        if (oldPoints.getDuplicate() != null || points.getDuplicate() != null) {
            if (oldPoints.getDuplicate() == null && points.getDuplicate() != null) {
                storeInHistory(submission, "duplicate", "", points.getDuplicate() + "", participation);
                changed = true;
            } else if (oldPoints.getDuplicate() != null && points.getDuplicate() == null) {
                storeInHistory(submission, "duplicate", oldPoints.getDuplicate() + "", "", participation);
                changed = true;
            } else if (!oldPoints.getDuplicate().equals(points.getDuplicate())) {
                storeInHistory(submission, "duplicate", oldPoints.getDuplicate() + "",
                        points.getDuplicate() + "", participation);
                changed = true;
            }
        }
        if (!oldPoints.getPoints().equals(points.getPoints())) {
            storeInHistory(submission, "points", Util.showPoints(oldPoints.getPoints()),
                    Util.showPoints(points.getPoints()), participation);
            changed = true;
        }
        if (oldPoints.getInternalComment() != null
                && !oldPoints.getInternalComment().equals(points.getInternalComment())) {
            storeInHistory(submission, "internalComment", oldPoints.getInternalComment(),
                    points.getInternalComment(), participation);
            changed = true;
        }
        if (oldPoints.getPublicComment() != null
                && !oldPoints.getPublicComment().equals(points.getPublicComment())) {
            storeInHistory(submission, "publicComment", oldPoints.getPublicComment(), points.getPublicComment(),
                    participation);
            changed = true;
        }
        if (changed && oldPoints.getIssuedBy().getUser().getUid() != participation.getUser().getUid()) {
            // HACK hardcoded URL
            MailSender.sendMail(oldPoints.getIssuedBy().getUser().getFullEmail(), "Mark-Change Notification",
                    "Hallo,\n\n" + participation.getUser().getFullName()
                            + " hat Deine Bewertung von <https://si.in.tu-clausthal.de/submissionsystem/servlets/ShowSubmission?sid="
                            + submission.getSubmissionid() + "> verndert.\n\n-- \nReply is not possible.");
        }
    } else {
        if (points.getPointStatus() != null) {
            storeInHistory(submission, "status", "", PointStatus.values()[points.getPointStatus()].toString(),
                    participation);
        }
        if (points.getDuplicate() != null) {
            storeInHistory(submission, "duplicate", "", points.getDuplicate() + "", participation);
        }
        if (points.getPoints() != null) {
            storeInHistory(submission, "points", "", Util.showPoints(points.getPoints()), participation);
        }
        if (points.getInternalComment() != null && !"".equals(points.getInternalComment())) {
            storeInHistory(submission, "internalComment", "", points.getInternalComment(), participation);
        }
        if (points.getPublicComment() != null && !"".equals(points.getPublicComment())) {
            storeInHistory(submission, "publicComment", "", points.getPublicComment(), participation);
        }
    }
    return points;
}