Example usage for org.hibernate StatelessSession beginTransaction

List of usage examples for org.hibernate StatelessSession beginTransaction

Introduction

In this page you can find the example usage for org.hibernate StatelessSession beginTransaction.

Prototype

Transaction beginTransaction();

Source Link

Document

Begin a unit of work and return the associated Transaction object.

Usage

From source file:org.squashtest.tm.service.internal.requirement.coercers.extenders.RequirementLibraryNodePathEdgeExtender.java

License:Open Source License

@Override
public Collection<? extends Serializable> doCoerce(Collection<? extends Serializable> coercedIds) {
    StatelessSession s = hibernateStatelessSessionHelper.openStatelessSession();
    Transaction tx = s.beginTransaction();

    try {//w  w w .  j av  a 2 s .  c  om
        Query q = s.createQuery(
                "select distinct edge.ancestorId from RequirementPathEdge edge where edge.descendantId in (:rlnIds) and depth=1");
        q.setParameterList("rlnIds", coercedIds);
        coercedIds.addAll(q.list());
        return coercedIds;

    } finally {
        tx.commit();
        s.close();
    }
}

From source file:org.squashtest.tm.service.internal.testcase.coercers.extenders.TestCaseLibraryExtender.java

License:Open Source License

@Override
public Collection<? extends Serializable> doCoerce(Collection<? extends Serializable> ids) {
    StatelessSession s = hibernateStatelessSessionHelper.openStatelessSession();
    Transaction tx = s.beginTransaction();

    try {//from   ww w.j  av a2s. c  om
        Query q = s.createQuery(
                "select distinct l.id from TestCaseLibrary l join l.rootContent c where c.id in (:tclnIds) ");
        q.setParameterList("tclnIds", ids);
        return q.list();

    } finally {
        tx.commit();
        s.close();
    }
}

From source file:org.squashtest.tm.service.internal.testcase.coercers.extenders.TestCaseLibraryNodePathEdgeExtender.java

License:Open Source License

@Override
public Collection<? extends Serializable> doCoerce(Collection<? extends Serializable> coercedIds) {
    StatelessSession s = hibernateStatelessSessionHelper.openStatelessSession();
    Transaction tx = s.beginTransaction();

    try {/*from  w  ww  .  j  a  v  a  2 s  .co m*/
        Query q = s.createQuery(
                "select distinct edge.ancestorId from TestCasePathEdge edge where edge.descendantId in (:tclnIds) and depth=1");
        q.setParameterList("tclnIds", coercedIds);
        coercedIds.addAll(q.list());
        return coercedIds;

    } finally {
        tx.commit();
        s.close();
    }
}

From source file:org.transitime.misc.HibernateTest.java

License:Open Source License

/**
 * For comparing timing of doing a large number of inserts. Can either
 * use regular batch mode where there is a specified batch size but
 * the caching and other internal features are still used. Or can
 * use a StatelessSession to bypass all of that internal stuff.
 * This way can easily determine which is the best method.
 * @param cnt//from w  ww .  j a  v  a2  s.c  o  m
 * @param batch
 */
private static void timeBatchStore(int cnt, boolean batch) {
    System.out.println((batch ? "Batch" : "Stateless") + " storing " + cnt + " records");
    IntervalTimer timer = null;

    try {
        SessionFactory sessionFactory = HibernateUtils.getSessionFactory("test");

        // Start timer after session factory is obtained since first time it is
        // obtained it takes a long time. This timer is used to determine how long
        // things take to run.
        timer = new IntervalTimer();

        // Get ready to write
        StatelessSession statelessSession = sessionFactory.openStatelessSession();
        Session batchSession = sessionFactory.openSession();
        System.out.println("Opening session took " + timer.elapsedMsec() + " msec");
        try {
            Transaction tx = batch ? batchSession.beginTransaction() : statelessSession.beginTransaction();

            // Store some AVLReportss
            long initialTime = System.currentTimeMillis();
            for (int i = 0; i < cnt; ++i) {
                AvlReport report = new AvlReport((batch ? "batch" : "stateless") + i, initialTime + i, 1.23,
                        4.56, null);
                if (batch)
                    batchSession.save(report);
                else
                    statelessSession.insert(report);
                if (batch && ((i + 1) % BATCH_SIZE == 0)) {
                    batchSession.flush();
                    batchSession.clear();
                }
            }

            // Finish up writing
            tx.commit();
        } catch (Exception e) {
            // If should rollback the transaction it would be done here
            e.printStackTrace();
        } finally {
            // No matter what, close the session
            batchSession.close();
        }
    } catch (HibernateException e) {
        e.printStackTrace();
    }

    System.out.println((batch ? "Batch" : "Stateless") + " storing " + cnt + " records took "
            + timer.elapsedMsec() + " msec");
}

From source file:vault.queryrouter.models.dao.QueryDAO.java

License:Apache License

/**
 * Find the last statement of `SET search_path `. If no, find the schema......
 * @param session//from w w w  .ja  v a  2 s.c  o m
 * @param connectionId
 * @return
 * @throws Exception
 */
public static String findSearchPath(StatelessSession session, String connectionId) throws Exception {
    try {

        String hql = "SELECT queryBody FROM Query WHERE SUBSTRING(LOWER(REPLACE(queryBody,' ','')),1,14)='setsearch_path' AND connectionId='"
                + connectionId + "' order by startTime asc";

        org.hibernate.Query query = session.createQuery(hql).setFirstResult(0);

        Transaction t = session.beginTransaction();
        List<String> queryLogs = query.list();
        t.commit();
        if (queryLogs.size() == 0)
            return null;
        else {
            return SQLRegexUtil.getSchemaFromSetSearchPath(queryLogs.get(0));
        }

    } catch (Exception e) {
        logger.error("Can't find search path", e);
        throw e;
    }
}

From source file:vault.queryrouter.query.QueryManager.java

License:Apache License

public UUID processQuery(StatelessSession session, String sql, String mppdbId, int userId, int tenantMppdbId,
        String fileName, String commandType, String connectionId) throws Exception {

    UUID queryId = UUID.randomUUID();

    //Add the record to the Query Log
    Transaction tx = null;//  w ww .jav  a 2 s.  c  om
    try {
        tx = session.beginTransaction();
        //startTimestamp will be null, but it will have current timestamp in backend database
        //endTimestamp will be null in backend-database
        String QuerylogID = QueryDAO.addQueryLog(session, queryId.toString(), sql, null, null, commandType,
                userId, tenantMppdbId, QueryLogStatus.Processing.toString(), mppdbId, connectionId);

        StringTokenizer tokenizer = new StringTokenizer(sql);
        String startsWith = tokenizer.nextToken();

        //Deal with the SELECT statement
        if (startsWith.equalsIgnoreCase("SELECT")) {
            Callable<Void> callableTask = new SelectQueryExecution(sql, mppdbId, userId, tenantMppdbId, queryId,
                    fileName, commandType, QuerylogID, connectionId);
            selectExecutor.submit(callableTask);
        }
        //Deal with the UPDATE statement
        else {
            //Append the Update statement to the Job Queue Table, not execute the Update statement
            JobQueueDAO.addQueryToJobQueue(session, sql, TenantUpdateJobStatus.Pending, queryId.toString(),
                    connectionId, tenantMppdbId);
        }
        tx.commit();
        return queryId;
    } catch (Exception e) {
        logger.error("Fail to process query", e);
        tx.rollback();
        throw e;
    }

}