Example usage for org.hibernate LockMode UPGRADE

List of usage examples for org.hibernate LockMode UPGRADE

Introduction

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

Prototype

LockMode UPGRADE

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

Click Source Link

Document

An upgrade lock.

Usage

From source file:org.hyperic.hq.dao.HibernateDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
public T findById(Serializable id, boolean lock) {
    return lock ? (T) getSession().load(getPersistentClass(), id, LockMode.UPGRADE)
            : (T) getSession().load(getPersistentClass(), id);
}

From source file:org.javamexico.dao.hib3.ForumDAO.java

License:Open Source License

public VotoForo vota(Usuario user, Foro foro, boolean up) throws PrivilegioInsuficienteException {
    if ((up && user.getReputacion() < minRepVotaFu) || (!up && user.getReputacion() < minRepVotaFd)) {
        throw new PrivilegioInsuficienteException(
                "El usuario no tiene privilegio suficiente para votar por un foro");
    }/*from   w  w w. j av a 2 s  .com*/
    Session sess = sfact.getCurrentSession();
    //Buscamos el voto a ver si ya se hizo
    VotoForo voto = findVoto(user, foro);
    int uprep = 0;
    if (voto == null) {
        //Si no existe lo creamos
        voto = new VotoForo();
        voto.setFecha(new Date());
        voto.setForo(foro);
        voto.setUp(up);
        voto.setUser(user);
        sess.save(voto);
        uprep = up ? 1 : -1;
    } else if (voto.isUp() != up) {
        //Si ya existe pero quieren cambio, se actualiza
        voto.setFecha(new Date());
        voto.setUp(up);
        sess.update(voto);
        uprep = up ? 2 : -2;
    }
    if (uprep != 0) {
        sess.refresh(foro);
        //Esto no es nada intuitivo pero si no lo hago asi, se arroja una excepcion marciana de Hib
        user = (Usuario) sess.merge(foro.getAutor());
        sess.lock(user, LockMode.UPGRADE);
        sess.refresh(user);
        user.setReputacion(user.getReputacion() + uprep);
        sess.update(user);
        sess.flush();
    }
    return voto;
}

From source file:org.javamexico.dao.hib3.ForumDAO.java

License:Open Source License

public VotoComentForo vota(Usuario user, ComentForo coment, boolean up) throws PrivilegioInsuficienteException {
    if ((up && user.getReputacion() < minRepVotaCu) || (!up && user.getReputacion() < minRepVotaCd)) {
        throw new PrivilegioInsuficienteException(
                "El usuario no tiene privilegio suficiente para votar por un comentario");
    }/*from   ww  w . jav a  2s. c  o m*/
    Session sess = sfact.getCurrentSession();
    VotoComentForo voto = findVoto(user, coment);
    int uprep = 0;
    if (voto == null) {
        voto = new VotoComentForo();
        voto.setFecha(new Date());
        voto.setComentario(coment);
        voto.setUp(up);
        voto.setUser(user);
        sess.save(voto);
        uprep = up ? 1 : -1;
    } else if (voto.isUp() != up) {
        voto.setUp(up);
        voto.setFecha(new Date());
        sess.update(voto);
        uprep = up ? 2 : -2;
    }
    if (uprep != 0) {
        sess.refresh(coment);
        user = (Usuario) sess.merge(coment.getAutor());
        sess.lock(user, LockMode.UPGRADE);
        sess.refresh(user);
        user.setReputacion(user.getReputacion() + uprep);
        sess.update(user);
        sess.flush();
    }
    return voto;
}

From source file:org.javamexico.dao.hib3.QuestionDAO.java

License:Open Source License

public VotoPregunta vota(Usuario user, Pregunta pregunta, boolean up) throws PrivilegioInsuficienteException {
    if ((up && user.getReputacion() < minRepVotaPu) || (!up && user.getReputacion() < minRepVotaPd)) {
        throw new PrivilegioInsuficienteException(
                "El usuario no tiene privilegio suficiente para votar por una pregunta");
    }//from  w w  w  . j a  v a2 s.c o  m
    Session sess = sfact.getCurrentSession();
    VotoPregunta v = findVoto(user, pregunta);
    int updateRep = 0;
    if (v == null) {
        //Insertamos un nuevo voto
        v = new VotoPregunta();
        v.setFecha(new Date());
        v.setPregunta(pregunta);
        v.setUp(up);
        v.setUser(user);
        sess.save(v);
        updateRep = up ? 1 : -1;
    } else if (v.isUp() != up) {
        //Modificamos el voto existente
        v.setUp(up);
        v.setFecha(new Date());
        sess.update(v);
        //Al cambiar voto, debe afectarse por 2
        updateRep = up ? 2 : -2;
    }
    if (updateRep != 0) {
        //Actualizamos la reputacion del autor de la pregunta
        sess.refresh(pregunta);
        //Esto no es intuitivo pero si no lo hago asi, hib3 arroja excepcion marciana.
        user = (Usuario) sess.merge(pregunta.getAutor());
        sess.lock(user, LockMode.UPGRADE);
        sess.refresh(user);
        user.setReputacion(user.getReputacion() + updateRep);
        sess.update(user);
        sess.flush();
    }
    return v;
}

From source file:org.javamexico.dao.hib3.QuestionDAO.java

License:Open Source License

public VotoRespuesta vota(Usuario user, Respuesta resp, boolean up) throws PrivilegioInsuficienteException {
    if ((up && user.getReputacion() < minRepVotaRu) || (!up && user.getReputacion() < minRepVotaRd)) {
        throw new PrivilegioInsuficienteException(
                "El usuario no tiene privilegio suficiente para votar por una respuesta");
    }//w w w  .ja  v  a  2 s. c  o m
    Session sess = sfact.getCurrentSession();
    VotoRespuesta v = findVoto(user, resp);
    int updateRep = 0;
    if (v == null) {
        v = new VotoRespuesta();
        v.setFecha(new Date());
        v.setRespuesta(resp);
        v.setUp(up);
        v.setUser(user);
        sess.save(v);
        updateRep = up ? 1 : -1;
    } else if (v.isUp() != up) {
        v.setUp(up);
        v.setFecha(new Date());
        sess.update(v);
        updateRep = up ? 2 : -2;
    }
    if (updateRep != 0) {
        sess.refresh(resp);
        user = (Usuario) sess.merge(resp.getAutor());
        sess.lock(user, LockMode.UPGRADE);
        sess.refresh(user);
        user.setReputacion(user.getReputacion() + updateRep);
        sess.update(user);
        sess.flush();
    }
    return v;
}

From source file:org.jboss.ejb3.entity.JTATableIdGenerator.java

License:Open Source License

public void configure(Type type, Properties params, Dialect dialect) {
    this.tableName = PropertiesHelper.getString(TABLE, params, DEFAULT_TABLE_NAME);
    this.columnName = PropertiesHelper.getString(COLUMN, params, DEFAULT_COLUMN_NAME);
    this.allocationSize = PropertiesHelper.getInt(ALLOCATION_SIZE, params, DEFAULT_ALLOCATION_SIZE);
    String schemaName = params.getProperty(SCHEMA);
    String catalogName = params.getProperty(CATALOG);

    if (true)//from  w ww.  j  a va  2  s. c om
        throw new RuntimeException("DOES ANYBODY USE THIS?  It IS CURRENTLY BROKEN");

    /*
    getSchemaSeparator does not exist in hibernate anymore since 3.1 release
            
    // prepare table name
    if (tableName.indexOf(dialect.getSchemaSeparator()) < 0)
    {
       tableName = Table.qualify(catalogName, schemaName, tableName, dialect.getSchemaSeparator());
    }
    */

    // prepare SQL statements
    query = "select " + columnName + " from " + dialect.appendLockHint(LockMode.UPGRADE, tableName)
            + dialect.getForUpdateString();
    update = "update " + tableName + " set " + columnName + " = ? where " + columnName + " = ?";

    // set up transaction manager lookup
    // only JBoss transaction manager is supported
    transactionManagerLookup = new JBossTransactionManagerLookup();

    // set the sequence type that should be returned
    returnClass = type.getReturnedClass();

    // debug chosen configuration
    if (log.isDebugEnabled()) {
        log.debug("configuring id generator: " + this.getClass().getName());
        log.debug("tableName=" + tableName);
        log.debug("columnName=" + columnName);
        log.debug("allocationSize=" + allocationSize);
        log.debug("query=" + query);
        log.debug("update=" + update);
        log.debug("returnClass=" + returnClass);
    }
}

From source file:org.jbpm.bpel.persistence.db.BpelGraphSession.java

License:Open Source License

public void lockToken(Token token) {
    session.lock(token, LockMode.UPGRADE);
}

From source file:org.jbpm.db.GraphSession.java

License:Open Source License

/**
 * locks a process instance in the database.
 *///from  w w  w .jav a  2s. com
public void lockProcessInstance(ProcessInstance processInstance) {
    try {
        session.lock(processInstance, LockMode.UPGRADE);
    } catch (Exception e) {
        log.error(e);
        jbpmSession.handleException();
        throw new JbpmException("couldn't lock process instance '" + processInstance.getId() + "'", e);
    }
}

From source file:org.jbpm.graph.node.Join.java

License:Open Source License

public void read(Element element, JpdlXmlReader jpdlReader) {
    String lock = element.attributeValue("lock");
    if ((lock != null) && (lock.equalsIgnoreCase("pessimistic"))) {
        parentLockMode = LockMode.UPGRADE.toString();
    }/*from   w ww  . j  a v  a2  s .  c  om*/
}

From source file:org.jbpm.graph.node.JoinDbTest.java

License:Open Source License

public void testParentLockMode() {
    ProcessDefinition processDefinition = ProcessDefinition
            .parseXmlString("<process-definition name='lock mode'>" + "  <join name='read' lock='READ' />"
                    + "  <join name='nowait' lock='UPGRADE_NOWAIT' />"
                    + "  <join name='upgrade' lock='pessimistic' />" + "</process-definition>");
    deployProcessDefinition(processDefinition);

    processDefinition = graphSession.findLatestProcessDefinition("lock mode");
    Join join = (Join) processDefinition.getNode("read");
    assertEquals(LockMode.READ.toString(), join.getParentLockMode());
    join = (Join) processDefinition.getNode("nowait");
    assertEquals(LockMode.UPGRADE_NOWAIT.toString(), join.getParentLockMode());
    join = (Join) processDefinition.getNode("upgrade");
    assertEquals(LockMode.UPGRADE.toString(), join.getParentLockMode());
}