Example usage for org.hibernate Session buildLockRequest

List of usage examples for org.hibernate Session buildLockRequest

Introduction

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

Prototype

LockRequest buildLockRequest(LockOptions lockOptions);

Source Link

Document

Build a LockRequest that specifies the LockMode, pessimistic lock timeout and lock scope.

Usage

From source file:ca.mcgill.cs.swevo.qualyzer.model.PersistenceManager.java

License:Open Source License

/**
 * //from  w  w w  .j  av  a  2 s . co  m
 * Does something.
 * 
 * @param project
 * @return
 */
public void initializeDocument(IAnnotatedDocument document) {
    HibernateDBManager dbManager = fActivator.getHibernateDBManagers()
            .get(document.getProject().getFolderName());

    Session session = dbManager.openSession();
    try {
        // Reattach
        session.buildLockRequest(LockOptions.NONE).lock(document);
        Hibernate.initialize(document.getParticipants());
        Hibernate.initialize(document.getFragments());
    } finally {
        HibernateUtil.quietClose(session);
    }
}

From source file:ch.algotrader.dao.AbstractDao.java

License:Open Source License

public void lock(final E entity, final LockOptions lockOptions) {

    Session currentSession = getCurrentSession();
    Session.LockRequest lockRequest = currentSession.buildLockRequest(lockOptions);
    lockRequest.lock(entity);/*  w ww  . ja  v  a2  s .  c om*/
}

From source file:com.algoTrader.service.TransactionServiceImpl.java

License:Open Source License

protected void handleCreateTransaction(Fill fill) throws Exception {

    Strategy strategy = fill.getParentOrder().getStrategy();
    Security security = fill.getParentOrder().getSecurity();

    // lock and initialize the security & strategy
    Session session = this.getSessionFactory().getCurrentSession();
    session.buildLockRequest(LockOptions.NONE).lock(security);
    session.buildLockRequest(LockOptions.NONE).lock(strategy);
    Hibernate.initialize(security);//from  w  w w.  ja  v a  2  s  . c  om
    Hibernate.initialize(strategy);

    TransactionType transactionType = Side.BUY.equals(fill.getSide()) ? TransactionType.BUY
            : TransactionType.SELL;
    long quantity = Side.BUY.equals(fill.getSide()) ? fill.getQuantity() : -fill.getQuantity();

    Transaction transaction = new TransactionImpl();
    transaction.setDateTime(fill.getDateTime());
    transaction.setQuantity(quantity);
    transaction.setPrice(fill.getPrice());
    transaction.setType(transactionType);
    transaction.setSecurity(security);
    transaction.setStrategy(strategy);
    transaction.setCurrency(security.getSecurityFamily().getCurrency());
    transaction.setCommission(fill.getCommission());

    // Strategy
    strategy.getTransactions().add(transaction);

    // Position
    Position position = getPositionDao().findBySecurityAndStrategy(security.getId(), strategy.getName());
    if (position == null) {

        position = new PositionImpl();
        position.setQuantity(transaction.getQuantity());

        position.setExitValue(null);
        position.setMaintenanceMargin(null);

        position.setSecurity(security);
        security.getPositions().add(position);

        position.getTransactions().add(transaction);
        transaction.setPosition(position);

        position.setStrategy(strategy);
        strategy.getPositions().add(position);

        getPositionDao().create(position);

    } else {

        position.setQuantity(position.getQuantity() + transaction.getQuantity());

        if (!position.isOpen()) {
            position.setExitValue(null);
            position.setMaintenanceMargin(null);
        }

        position.getTransactions().add(transaction);
        transaction.setPosition(position);

        getPositionDao().update(position);
    }

    getTransactionDao().create(transaction);
    getStrategyDao().update(strategy);
    getSecurityDao().update(security);

    // progapate the order to all corresponding esper engines
    propagateTransaction(transaction);

    String logMessage = "executed transaction type: " + transactionType + " quantity: "
            + transaction.getQuantity() + " of " + security.getSymbol() + " price: " + transaction.getPrice()
            + " commission: " + transaction.getCommission();

    logger.info(logMessage);
}

From source file:com.algoTrader.util.HibernateUtil.java

License:Open Source License

public static boolean lock(SessionFactory sessionFactory, Object target) {

    Session session = sessionFactory.getCurrentSession();

    try {/*  w  w w.j a  v a 2s  . c om*/
        session.buildLockRequest(LockOptions.NONE).lock(target);
        return true;
    } catch (NonUniqueObjectException e) {
        //  different object with the same identifier value was already associated with the session
        return false;
    }
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Page.java

License:Apache License

/**
 * Returns a set of categories that this page belongs to.
 *
 * @return The a set of categories that this page belongs to.
 *///  w w  w  .  j a v a  2 s .c  om
public Set<Category> getCategories() {
    Session session = this.wiki.__getHibernateSession();
    session.beginTransaction();
    session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);
    Set<Integer> tmp = new UnmodifiableArraySet<Integer>(hibernatePage.getCategories());
    session.getTransaction().commit();

    Set<Category> categories = new HashSet<Category>();
    for (int pageID : tmp) {
        categories.add(wiki.getCategory(pageID));
    }

    return categories;
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Page.java

License:Apache License

/**
 * Returns the set of pages that have a link pointing to this page. <b>Warning:</b> Do not use
 * this for getting the number of inlinks with getInlinks().size(). This is too slow. Use
 * getNumberOfInlinks() instead./*from  ww w.ja  va  2  s .com*/
 *
 * @return The set of pages that have a link pointing to this page.
 */
public Set<Page> getInlinks() {
    Session session = wiki.__getHibernateSession();
    session.beginTransaction();
    session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);
    // Have to copy links here since getPage later will close the session.
    Set<Integer> pageIDs = new UnmodifiableArraySet<Integer>(hibernatePage.getInLinks());
    session.getTransaction().commit();

    Set<Page> pages = new HashSet<Page>();
    for (int pageID : pageIDs) {
        try {
            pages.add(wiki.getPage(pageID));
        } catch (WikiApiException e) {
            // Silently ignore if a page could not be found
            // There may be inlinks that do not come from an existing page.
            continue;
        }
    }

    return pages;
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Page.java

License:Apache License

/**
 * The result set may also contain links from non-existing pages. It is in the responsibility of
 * the user to check whether the page exists.
 *
 * @return Returns the IDs of the inLinks of this page.
 *///from  w  ww .  j  av a 2 s. co  m
public Set<Integer> getInlinkIDs() {
    Set<Integer> tmpSet = new HashSet<Integer>();

    Session session = wiki.__getHibernateSession();
    session.beginTransaction();
    session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);

    tmpSet.addAll(hibernatePage.getInLinks());

    session.getTransaction().commit();

    return tmpSet;
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Page.java

License:Apache License

/**
 * Returns the set of pages that are linked from this page. Outlinks in a page might also point
 * to non-existing pages. They are not included in the result set. <b>Warning:</b> Do not use
 * this for getting the number of outlinks with getOutlinks().size(). This is too slow. Use
 * getNumberOfOutlinks() instead./* w  ww .ja  v  a2  s.  c  o m*/
 *
 * @return The set of pages that are linked from this page.
 */
public Set<Page> getOutlinks() {
    Session session = wiki.__getHibernateSession();
    session.beginTransaction();
    //      session.lock(hibernatePage, LockMode.NONE);
    session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);
    // Have to copy links here since getPage later will close the session.
    Set<Integer> tmpSet = new UnmodifiableArraySet<Integer>(hibernatePage.getOutLinks());
    session.getTransaction().commit();

    Set<Page> pages = new HashSet<Page>();
    for (int pageID : tmpSet) {
        try {
            pages.add(wiki.getPage(pageID));
        } catch (WikiApiException e) {
            // Silently ignore if a page could not be found.
            // There may be outlinks pointing to non-existing pages.
        }
    }
    return pages;
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Page.java

License:Apache License

/**
 * The result set may also contain links from non-existing pages. It is in the responsibility of
 * the user to check whether the page exists.
 *
 * @return Returns the IDs of the outLinks of this page.
 *//*from   w w  w .ja v a2 s .c  om*/
public Set<Integer> getOutlinkIDs() {
    Set<Integer> tmpSet = new HashSet<Integer>();

    Session session = wiki.__getHibernateSession();
    session.beginTransaction();
    session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);

    tmpSet.addAll(hibernatePage.getOutLinks());

    session.getTransaction().commit();
    return tmpSet;
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Page.java

License:Apache License

/**
 * Returns the set of strings that are redirects to this page.
 *
 * @return The set of redirect strings.//from w  ww  .j  a v  a2  s .c  om
 */
public Set<String> getRedirects() {
    Session session = wiki.__getHibernateSession();
    session.beginTransaction();
    session.buildLockRequest(LockOptions.NONE).lock(hibernatePage);
    Set<String> tmpSet = new HashSet<String>(hibernatePage.getRedirects());
    session.getTransaction().commit();
    return tmpSet;
}