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(); } }
/** * Mark the specified action as completed * * @param transactionId the unique transaction id for this action * @param scheduledActionUuid the unique name of an action */ private void markAsCompleted(String transactionId, String scheduledActionUuid) { try { Ebean.beginTransaction(); SchedulerState schedulerState = SchedulerState.getRunningSchedulerStateFromTransactionId(transactionId); if (schedulerState == null) { log.error( String.format( "Strange ... No running scheduled action for %s with transaction id %s while one was running and mark as completed is requested", scheduledActionUuid, transactionId)); } else { schedulerState.isRunning = false; schedulerState.save(); if (log.isDebugEnabled()) { log.debug( String.format( "Scheduled action for %s with transaction id %s completed", scheduledActionUuid, transactionId)); } } Ebean.commitTransaction(); } catch (Exception e) { log.error("Failed to mark as complete", e); rollbackTransactionSilent(); } finally { endTransactionSilent(); } }
@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(); } }
@Override protected Result hook_call(Context ctx) throws Throwable { // TODO make it possible to get an extra transaction (either nested, or separate) // TODO allow for transactions across multiple requests boolean successful = false; try { Ebean.beginTransaction(); Result result = delegate.call(ctx); successful = true; return result; } finally { // if running tests, don't commit if (AppContext.Mode.isRunningTests() || !successful) { Ebean.rollbackTransaction(); } else { // TODO do retry logic Ebean.commitTransaction(); } } }
@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(); } }
/** * Mark the specified action as completed * * @param transactionId the unique transaction id for this action * @param scheduledActionUuid the unique name of an action */ private void markAsStarted(String transactionId, String scheduledActionUuid) { try { Ebean.beginTransaction(); SchedulerState schedulerState = new SchedulerState(); schedulerState.actionUuid = scheduledActionUuid; schedulerState.transactionId = transactionId; schedulerState.isRunning = true; schedulerState.save(); if (log.isDebugEnabled()) { log.debug( String.format( "Scheduled action for %s with transaction id %s started", scheduledActionUuid, transactionId)); } Ebean.commitTransaction(); } catch (Exception e) { log.error("Failed to mark as started", e); rollbackTransactionSilent(); } finally { endTransactionSilent(); } }