public static void update(Long idGroup, List<SqlRow> gpms) { Group group = Group.findById(idGroup); if (group == null) return; Ebean.beginTransaction(); try { deleteForGroup(idGroup); Integer i = 1; for (SqlRow man : gpms) { Profile prof = Profile.lastProfileByGpmId(man.getLong("gpm")); if (prof == null) continue; CacheClassifier cc = new CacheClassifier( group, i++, prof.gpm.idGpm, prof.name, prof.image, (prof.gender == null) ? null : prof.gender.value, (prof.relationshipStatus == null) ? null : prof.relationshipStatus.status, prof.nFollowers); if (cc.name == null || cc.name.isEmpty()) cc.name = Profile.getLastNotEmptyField(prof.gpm.id, "name"); cc.save(); } Ebean.commitTransaction(); } finally { Ebean.endTransaction(); } }
@Override public void deleteCarouselImage(Long id) throws NotFoundException { // We use explicit transactions, since we are going to execute more than // 1 query in the same transaction Ebean.beginTransaction(); try { // We see if the carouselImage really exists if (finder.byId(id) == null) throw new NotFoundException("La imagen no existe asi que no se puede borrar"); // We delete it finder.byId(id).delete(); // Now we need to reorder all the carouselImages for (CarouselImage ci : finder.orderBy("id").findList()) { if (ci.getId() > id) { // We insert a copy of the ci in the previous position CarouselImage ciCopy = (CarouselImage) ci._ebean_createCopy(); ciCopy.setId(ci.getId() - 1); ciCopy.save(); // Then we delete the ci ci.delete(); } } Ebean.commitTransaction(); } finally { Ebean.endTransaction(); } }
@Override public void addCarouselImage(CarouselImage carouselImage) { // We use explicit transactions, since we are going to execute more than // 1 query in the same transaction Ebean.beginTransaction(); try { // We set atomatically the caroueselImage id, in order to be the // first one carouselImage.setId((long) 1); List<CarouselImage> carouselImages = finder.orderBy().desc("id").findList(); for (CarouselImage ci : carouselImages) { // We insert a copy of the ci in the next position CarouselImage ciCopy = (CarouselImage) ci._ebean_createCopy(); ciCopy.setId(ci.getId() + 1); ciCopy.save(); // Then we delete the ci ci.delete(); } // Then we save carouselImage.save(); Ebean.commitTransaction(); } finally { Ebean.endTransaction(); } }
@Test public void test() { LoggedSqlCollector.start(); Transaction txn = Ebean.beginTransaction(); try { txn.setBatch(PersistBatch.ALL); EdParent parent = new EdParent(); parent.setName("MyComputer"); EdChild child = new EdChild(); child.setName("Harddisk 123"); child.setParent(parent); ArrayList<EdChild> children = new ArrayList<EdChild>(); children.add(child); parent.setChildren(children); Ebean.save(parent); // nothing flushed yet List<String> loggedSql0 = LoggedSqlCollector.start(); assertEquals(0, loggedSql0.size()); parent.setName("MyDesk"); Ebean.save(parent); // nothing flushed yet assertEquals(0, LoggedSqlCollector.start().size()); Ebean.commitTransaction(); // insert statements for EdExtendedParent List<String> loggedSql2 = LoggedSqlCollector.start(); assertEquals(2, loggedSql2.size()); } finally { Ebean.endTransaction(); } }
@Test public void test_countUsingForegound() throws ExecutionException, InterruptedException { ResetBasicData.reset(); PagedList<Order> pagedList = Ebean.find(Order.class).findPagedList(0, 6); LoggedSqlCollector.start(); // kinda not normal but just wrap in a transaction to assert // the background fetch does not occur (which explicitly creates // its own transaction) ... so a bit naughty with the test here Ebean.beginTransaction(); try { List<Order> orders = pagedList.getList(); int totalRowCount = pagedList.getTotalRowCount(); // invoke it again but cached... int totalRowCountAgain = pagedList.getTotalRowCount(); List<String> loggedSql = LoggedSqlCollector.stop(); assertTrue(orders.size() < totalRowCount); assertEquals(2, loggedSql.size()); assertEquals(totalRowCount, totalRowCountAgain); String firstTxn = loggedSql.get(0).substring(0, 10); String secTxn = loggedSql.get(1).substring(0, 10); assertEquals(firstTxn, secTxn); } finally { Ebean.endTransaction(); } }
private void endTransactionSilent() { try { Ebean.endTransaction(); } catch (Exception e) { } }