Example usage for org.hibernate LockMode READ

List of usage examples for org.hibernate LockMode READ

Introduction

In this page you can find the example usage for org.hibernate LockMode READ.

Prototype

LockMode READ

To view the source code for org.hibernate LockMode READ.

Click Source Link

Document

A shared lock.

Usage

From source file:org.projectforge.user.GroupDao.java

License:Open Source License

/**
 * Assigns groups to and unassigns groups from given user.
 * @param user/* w  w w  .  ja  va2  s .  c o m*/
 * @param groupsToAssign Groups to assign (nullable).
 * @param groupsToUnassign Groups to unassign (nullable).
 * @throws AccessException
 */
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, isolation = Isolation.REPEATABLE_READ)
public void assignGroups(final PFUserDO user, final Set<GroupDO> groupsToAssign,
        final Set<GroupDO> groupsToUnassign) throws AccessException {
    getHibernateTemplate().refresh(user, LockMode.READ);
    final List<GroupDO> assignedGroups = new ArrayList<GroupDO>();
    if (groupsToAssign != null) {
        for (final GroupDO group : groupsToAssign) {
            final GroupDO dbGroup = getHibernateTemplate().get(clazz, group.getId(),
                    LockMode.PESSIMISTIC_WRITE);
            Set<PFUserDO> assignedUsers = dbGroup.getAssignedUsers();
            if (assignedUsers == null) {
                assignedUsers = new HashSet<PFUserDO>();
                dbGroup.setAssignedUsers(assignedUsers);
            }
            if (assignedUsers.contains(user) == false) {
                log.info("Assigning user '" + user.getUsername() + "' to group '" + dbGroup.getName() + "'.");
                assignedUsers.add(user);
                assignedGroups.add(dbGroup);
                dbGroup.setLastUpdate(); // Needed, otherwise GroupDO is not detected for hibernate history!
            } else {
                log.info("User '" + user.getUsername() + "' already assigned to group '" + dbGroup.getName()
                        + "'.");
            }
        }
    }
    final List<GroupDO> unassignedGroups = new ArrayList<GroupDO>();
    if (groupsToUnassign != null) {
        for (final GroupDO group : groupsToUnassign) {
            final GroupDO dbGroup = getHibernateTemplate().get(clazz, group.getId(),
                    LockMode.PESSIMISTIC_WRITE);
            final Set<PFUserDO> assignedUsers = dbGroup.getAssignedUsers();
            if (assignedUsers != null && assignedUsers.contains(user) == true) {
                log.info("Unassigning user '" + user.getUsername() + "' from group '" + dbGroup.getName()
                        + "'.");
                assignedUsers.remove(user);
                unassignedGroups.add(dbGroup);
                dbGroup.setLastUpdate(); // Needed, otherwise GroupDO is not detected for hibernate history!
            } else {
                log.info("User '" + user.getUsername() + "' is not assigned to group '" + dbGroup.getName()
                        + "' (can't unassign).");
            }
        }
    }
    getSession().flush();
    createHistoryEntry(user, unassignedGroups, assignedGroups);
    userGroupCache.setExpired();
}

From source file:org.seasar.hibernate3.dao.impl.LoadCommand.java

License:Apache License

public void setLockMode(String lockMode) {
    final String NONE = "NONE";
    final String READ = "READ";
    final String UPGRADE = "UPGRADE";

    if (lockMode.equals(NONE)) {
        lockMode_ = LockMode.NONE;/*from  w w w  .  ja  va  2s . co  m*/

    } else if (lockMode.equals(READ)) {
        lockMode_ = LockMode.READ;

    } else if (lockMode.equals(UPGRADE)) {
        lockMode_ = LockMode.UPGRADE;

    } else {
        lockMode_ = null;
    }
}

From source file:org.wso2.mercury.persistence.hibernate.HibernatePersistenceManager.java

License:Apache License

public RMSSequenceDto getRMSSquenceWithID(long id) throws PersistenceException {
    Session session = null;//from  w  w  w  . ja v a2s  .c  om
    try {
        session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        RMSSequenceDto rmsSequenceDto = (RMSSequenceDto) session.load(RMSSequenceDto.class, id, LockMode.READ);
        session.getTransaction().commit();
        return rmsSequenceDto;

    } catch (HibernateException e) {
        if (session.getTransaction().isActive()) {
            session.getTransaction().rollback();
        }
        log.error("Can not load the RMSSeqence objects with id ==> " + id, e);
        throw new PersistenceException("Can not load the RMSSeqence objects with id ==> " + id, e);
    }
}

From source file:org.wso2.mercury.persistence.hibernate.HibernatePersistenceManager.java

License:Apache License

public Axis2InfoDto getAxis2InfoID(long id) throws PersistenceException {
    Session session = null;/*from w  w  w .  j  av  a 2s  . c o m*/
    try {
        session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        Axis2InfoDto axis2InfoDto = (Axis2InfoDto) session.load(Axis2InfoDto.class, id, LockMode.READ);
        session.getTransaction().commit();
        return axis2InfoDto;

    } catch (HibernateException e) {
        if (session.getTransaction().isActive()) {
            session.getTransaction().rollback();
        }
        log.error("Can not load the axis2InfoDto objects with id ==> " + id, e);
        throw new PersistenceException("Can not load the axis2InfoDto objects with id ==> " + id, e);
    }
}

From source file:org.wso2.mercury.persistence.hibernate.HibernatePersistenceManager.java

License:Apache License

public InternalKeyDto getInternalKeyWithID(long id) throws PersistenceException {
    Session session = null;/*from   ww  w  . j ava  2  s. c  om*/
    try {
        session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        InternalKeyDto internalKeyDto = (InternalKeyDto) session.load(InternalKeyDto.class, id, LockMode.READ);
        session.getTransaction().commit();
        return internalKeyDto;

    } catch (HibernateException e) {
        if (session.getTransaction().isActive()) {
            session.getTransaction().rollback();
        }
        log.error("Can not load the InternalKey object with internal key id ==> " + id, e);
        throw new PersistenceException("Can not load the Internalkey object with internal key id==> " + id, e);
    }
}

From source file:sncode.java.hibernate.dao.GenericDao.java

License:MIT License

/**
 * Find mapped data object by primary key
 * @param id_       Serializable with mapped object od field
 * @param objName_  String with mapped object name
 * @param session_  Hibernate session object
 * @return          Mapped object with query result
 * @throws DaoException /*from   w ww  . j av a  2s .co m*/
 */
public Object findByPK(Serializable id_, String objName_, Session session_) throws DaoException {
    Transaction tx = null;
    Object result = null;
    boolean globaltx = true;

    try {
        if (session_ == null) {
            session_ = this.startSession();
        }
        if (session_.getTransaction() == null || !session_.getTransaction().isActive()) {
            tx = session_.beginTransaction();
            globaltx = false;
        }

        result = session_.load(objName_, id_, LockMode.READ); // load has been deprecated

        if (!globaltx) {
            tx.commit();
        }
    } catch (Exception e) {
        logger.error("Error retriving by PK: " + stacktraceError(e));
        throw new DaoException("Error retriving by PK: " + stacktraceError(e));
    } finally {
        if (!globaltx) {
            if (session_ != null && session_.isOpen()) {
                if (tx != null && tx.isActive()) {
                    session_.flush();
                }
                session_.close();
                session_ = null;
            }
        }
    }

    return result;
}