Example usage for org.hibernate StatelessSession getTransaction

List of usage examples for org.hibernate StatelessSession getTransaction

Introduction

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

Prototype

Transaction getTransaction();

Source Link

Document

Get the Transaction instance associated with this session.

Usage

From source file:org.rhq.server.metrics.migrator.workers.AggregateDataMigrator.java

License:Open Source License

private void deleteTableData() throws Exception {
    int failureCount = 0;
    while (failureCount < MAX_NUMBER_OF_FAILURES) {
        try {//from   ww w. jav a  2  s . co m
            StatelessSession session = getSQLSession(config);
            session.getTransaction().begin();
            org.hibernate.Query nativeQuery = session.createSQLQuery(this.deleteQuery);
            nativeQuery.executeUpdate();
            session.getTransaction().commit();
            closeSQLSession(session);
            log.info("- " + migrationTable.toString() + " - Cleaned -");
        } catch (Exception e) {
            log.error("Failed to delete " + migrationTable.toString()
                    + " data. Attempting to delete data one more time...");

            failureCount++;
            if (failureCount == MAX_NUMBER_OF_FAILURES) {
                throw e;
            }
        }
    }
}

From source file:org.rhq.server.metrics.migrator.workers.DeleteAllData.java

License:Open Source License

public void migrate() {
    org.hibernate.Query nativeQuery;
    StatelessSession session = getSQLSession(config);

    if (config.isRun1HAggregateDataMigration()) {
        session.getTransaction().begin();
        nativeQuery = session.createSQLQuery(MigrationQuery.DELETE_1H_DATA.toString());
        nativeQuery.executeUpdate();/*from  ww  w. jav  a  2 s . c o  m*/
        session.getTransaction().commit();
        log.info("- RHQ_MEASUREMENT_DATA_NUM_1H - Cleaned -");
    }

    if (config.isRun6HAggregateDataMigration()) {
        session.getTransaction().begin();
        nativeQuery = session.createSQLQuery(MigrationQuery.DELETE_6H_DATA.toString());
        nativeQuery.executeUpdate();
        session.getTransaction().commit();
        log.info("- RHQ_MEASUREMENT_DATA_NUM_6H - Cleaned -");
    }

    if (config.isRun1DAggregateDataMigration()) {
        session.getTransaction().begin();
        nativeQuery = session.createSQLQuery(MigrationQuery.DELETE_1D_DATA.toString());
        nativeQuery.executeUpdate();
        session.getTransaction().commit();
        log.info("- RHQ_MEASUREMENT_DATA_NUM_1D - Cleaned -");
    }

    if (config.isRunRawDataMigration()) {
        for (String table : getRawDataTables()) {
            session.getTransaction().begin();
            String deleteAllData = String.format(MigrationQuery.DELETE_RAW_ALL_DATA.toString(), table);
            nativeQuery = session.createSQLQuery(deleteAllData);
            nativeQuery.executeUpdate();
            session.getTransaction().commit();
            log.info("- " + table + " - Cleaned -");
        }
    }

    closeSQLSession(session);
}

From source file:org.rhq.server.metrics.migrator.workers.RawDataMigrator.java

License:Open Source License

private void deleteTableData(String table) throws Exception {
    String deleteQuery = String.format(MigrationQuery.DELETE_RAW_ENTRY.toString(), table);
    int failureCount = 0;
    while (failureCount < MAX_NUMBER_OF_FAILURES) {
        try {//from www  .j  a v  a2  s. c om
            StatelessSession session = getSQLSession(config);
            session.getTransaction().begin();
            org.hibernate.Query nativeQuery = session.createSQLQuery(deleteQuery);
            nativeQuery.executeUpdate();
            session.getTransaction().commit();
            closeSQLSession(session);
            log.info("- " + table + " - Cleaned -");
        } catch (Exception e) {
            log.error("Failed to delete " + table + " data. Attempting to delete data one more time...");

            failureCount++;
            if (failureCount == MAX_NUMBER_OF_FAILURES) {
                throw e;
            }
        }
    }
}

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

License:Apache License

public static TenantUpdateJob getPendingQueryLock(StatelessSession session, String workerId) throws Exception {

    try {/*from   w w  w.  j  a  v a 2s .c  o  m*/
        session.getTransaction().begin();
        //Update the pending query(order by last_touch_timestamp) status from pending to processing
        String hql = "UPDATE TenantUpdateJob set status= '" + TenantUpdateJobStatus.Processing.toString()
                + "', workerId = '" + workerId + "' WHERE status='" + TenantUpdateJobStatus.Pending.toString()
                + "' AND type ='" + TenantUpdateJobType.queryrouter.toString()
                + "' ORDER BY lastTouchTime LIMIT 1";
        org.hibernate.Query query = session.createQuery(hql);
        int result = query.executeUpdate();
        session.getTransaction().commit();

        //Pending query exists and the status changes from "Pending" to "Processing"
        if (result != 0) {

            //Find the processing query by the thread(workerId)
            Criteria criteria = session.createCriteria(TenantUpdateJob.class);
            criteria.add(Expression.eq("workerId", workerId.toString()));
            criteria.add(Expression.eq("status", TenantUpdateJobStatus.Processing.toString()));
            TenantUpdateJob pendingQueueCandidate = (TenantUpdateJob) criteria.uniqueResult();

            Query q = QueryDAO.getQuery(session, pendingQueueCandidate.getQueryId());

            //Check the availability of the processing Query that could be actual processed
            //If the number of  conflictQuery is more than 0 which means the processing query conflicts with the another processing query(e.g.move_tenant_mppdb_data)
            int conflictQuery = JobQueueDAO.getProcessingQuery(session, q.getTenantMppdbId());

            //No conflict query -> Return the query(TenantUpdateJob Object)
            if (conflictQuery == 0) {

                return pendingQueueCandidate;
            }
            //Conflict query exists -> Status of the processing query turn back to "Pending" and the last_touch_timestamp will be updated
            else {

                try {
                    session.getTransaction().begin();
                    TenantUpdateJob jq = (TenantUpdateJob) session.get(TenantUpdateJob.class,
                            pendingQueueCandidate.getJobQueueId());
                    jq.setStatus(TenantUpdateJobStatus.Pending.toString());
                    jq.setWorkerId("");
                    session.update(jq);
                    session.getTransaction().commit();

                } catch (HibernateException e) {
                    logger.error("Exception: ", e);
                    if (session.getTransaction() != null)
                        session.getTransaction().rollback();
                    e.printStackTrace();
                }
                return null;
            }
        }
        //No pending query exists
        else {
            return null;
        }

    } catch (Exception e) {
        logger.error(e.toString());
        throw e;
    }
}

From source file:vn.edu.vnu.uet.fit.model.GenericModel.java

public void create(T entity) throws JDBCException, Exception {
    StatelessSession session = HibernateUtil.getSessionFactory().openStatelessSession();
    Transaction trans = session.getTransaction();
    try {//  w  ww . jav a2 s  .  c o m
        trans.begin();
        session.insert(entity);
        //session.save(entity);
        trans.commit();
    } catch (JDBCException ex) {
        trans.rollback();
        throw ex;
    } catch (Exception ex) {
        trans.rollback();
        throw ex;
    }
    session.close();
}

From source file:vn.edu.vnu.uet.fit.model.GenericModel.java

public void update(T entity) throws JDBCException, Exception {
    StatelessSession session = HibernateUtil.getSessionFactory().openStatelessSession();
    Transaction trans = session.getTransaction();
    try {//from  www  . ja  v a2 s. co  m
        trans.begin();
        session.update(entity);
        trans.commit();
    } catch (JDBCException ex) {
        trans.rollback();
        throw ex;
    } catch (Exception ex) {
        trans.rollback();
        throw ex;
    }
    session.close();
}