Search in sources :

Example 11 with EntityTransaction

use of javax.persistence.EntityTransaction in project Truck-Factor by aserg-ufmg.

the class NewFileInfoDAO method updateLanguageFileInfo.

public int updateLanguageFileInfo(String projectName, String language, List<String> paths) {
    FileType fileType = FileType.getType(language);
    List<Query> queries = new ArrayList<Query>();
    for (String path : paths) {
        if (path.contains("\'"))
            path = path.replace("'", "''");
        String hql = "UPDATE  newfileinfo " + "SET kind = \'" + fileType + "\'  , language = \'" + language + "\', filtered = \'FALSE\' " + ", filterinfo = \'\' " + "WHERE repositoryname = \'" + projectName + "\' AND path =  \'" + path + "\' " + ";";
        queries.add(em.createNativeQuery(hql));
    }
    int rows = 0;
    EntityTransaction tx = this.em.getTransaction();
    try {
        tx.begin();
        for (Query query : queries) {
            rows += query.executeUpdate();
        }
        tx.commit();
    } catch (RuntimeException e) {
        if (tx != null && tx.isActive())
            tx.rollback();
        throw e;
    } finally {
        this.em.clear();
    }
    return rows;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Query(javax.persistence.Query) FileType(aserg.gtf.model.FileType) ArrayList(java.util.ArrayList)

Example 12 with EntityTransaction

use of javax.persistence.EntityTransaction in project Truck-Factor by aserg-ufmg.

the class NewFileInfoDAO method setAllAsNotLinguist.

public int setAllAsNotLinguist() {
    String hql = "UPDATE  newfileinfo " + "SET filtered = \'TRUE', kind = \'NOTIDENTIFIED\', filterinfo = \'#NOTLINGUIST#\', language = \'\';";
    Query q = em.createNativeQuery(hql);
    int rows = 0;
    EntityTransaction tx = this.em.getTransaction();
    try {
        tx.begin();
        rows = q.executeUpdate();
        tx.commit();
    } catch (RuntimeException e) {
        if (tx != null && tx.isActive())
            tx.rollback();
        throw e;
    } finally {
        this.em.clear();
    }
    return rows;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Query(javax.persistence.Query)

Example 13 with EntityTransaction

use of javax.persistence.EntityTransaction in project Truck-Factor by aserg-ufmg.

the class PersistThread method run.

@Override
public void run() {
    System.out.println("Thread iniciada = " + this.getName() + " persisting objects: " + list.size());
    EntityTransaction tx = persistDAO.em.getTransaction();
    try {
        tx.begin();
        for (T t : list) {
            persistDAO.em.persist(t);
        }
        tx.commit();
    } catch (RuntimeException e) {
        if (tx != null && tx.isActive())
            tx.rollback();
        throw e;
    } finally {
        persistDAO.em.clear();
    }
    System.out.println("Thread Finalizada = " + this.getName());
}
Also used : EntityTransaction(javax.persistence.EntityTransaction)

Example 14 with EntityTransaction

use of javax.persistence.EntityTransaction in project hibernate-orm by hibernate.

the class BatchTest method withScroll.

private void withScroll() {
    withBatch();
    //tag::batch-session-scroll-example[]
    EntityManager entityManager = null;
    EntityTransaction txn = null;
    ScrollableResults scrollableResults = null;
    try {
        entityManager = entityManagerFactory().createEntityManager();
        txn = entityManager.getTransaction();
        txn.begin();
        int batchSize = 25;
        Session session = entityManager.unwrap(Session.class);
        scrollableResults = session.createQuery("select p from Person p").setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
        int count = 0;
        while (scrollableResults.next()) {
            Person Person = (Person) scrollableResults.get(0);
            processPerson(Person);
            if (++count % batchSize == 0) {
                //flush a batch of updates and release memory:
                entityManager.flush();
                entityManager.clear();
            }
        }
        txn.commit();
    } catch (RuntimeException e) {
        if (txn != null && txn.isActive())
            txn.rollback();
        throw e;
    } finally {
        if (scrollableResults != null) {
            scrollableResults.close();
        }
        if (entityManager != null) {
            entityManager.close();
        }
    }
//end::batch-session-scroll-example[]
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) ScrollableResults(org.hibernate.ScrollableResults) Person(org.hibernate.userguide.model.Person) StatelessSession(org.hibernate.StatelessSession) Session(org.hibernate.Session)

Example 15 with EntityTransaction

use of javax.persistence.EntityTransaction in project hibernate-orm by hibernate.

the class BatchTest method withoutBatch.

private void withoutBatch() {
    //tag::batch-session-batch-example[]
    EntityManager entityManager = null;
    EntityTransaction txn = null;
    try {
        entityManager = entityManagerFactory().createEntityManager();
        txn = entityManager.getTransaction();
        txn.begin();
        for (int i = 0; i < 100_000; i++) {
            Person Person = new Person(String.format("Person %d", i));
            entityManager.persist(Person);
        }
        txn.commit();
    } catch (RuntimeException e) {
        if (txn != null && txn.isActive())
            txn.rollback();
        throw e;
    } finally {
        if (entityManager != null) {
            entityManager.close();
        }
    }
//end::batch-session-batch-example[]
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Person(org.hibernate.userguide.model.Person)

Aggregations

EntityTransaction (javax.persistence.EntityTransaction)56 EntityManager (javax.persistence.EntityManager)36 Test (org.junit.Test)12 EntityManagerFactory (javax.persistence.EntityManagerFactory)9 Query (javax.persistence.Query)8 UserTestDbo (org.webpieces.plugins.hibernate.app.dbo.UserTestDbo)6 InumSqlEntry (org.gluu.oxtrust.model.sql.InumSqlEntry)4 Person (org.hibernate.userguide.model.Person)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 Transactional (com.google.inject.persist.Transactional)2 Priority (org.hibernate.envers.test.Priority)2 DefaultTransactionAttribute (org.springframework.transaction.interceptor.DefaultTransactionAttribute)2 UserDbo (WEBPIECESxPACKAGE.base.libs.UserDbo)1 UserRole (WEBPIECESxPACKAGE.base.libs.UserRole)1 FileType (aserg.gtf.model.FileType)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1