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;
}
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;
}
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());
}
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[]
}
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[]
}
Aggregations