public Transaction beginTransaction() { Transaction transaction = this.transactionManager.currentTransaction(); transaction.begin(); LOGGER. info("Started transaction {}.{},", Thread.currentThread().getName(), transaction); return transaction; }
@Override public void begin() { try { nativeTransaction().begin(); } catch (Exception e) { throw new TransactionException("Begin failed",e); } }
protected final void withinTransaction(Task<T> task) throws Exception{ Transaction tx = txManager.currentTransaction(); tx.begin(); boolean failed=false; try { this.logger.info(">> Started '{}'...",task.title); task.execute(this.sut); tx.commit(); this.logger.info(">> Completed '{}'.",task.title); } catch(Exception e) { this.logger.info(">> Failed '{}': {}.",task.title,e.getMessage()); failed=true; throw e; } finally { if(failed) { this.logger.debug("Transaction failed [active: {}]",tx.isActive()); } if(tx.isActive()) { this.logger.info("Attempting rollback..."); try { tx.rollback(); } catch (Exception e) { this.logger.error("Rollback failed",e); } } } }
private <T extends Resource> T publishResource(Class<? extends T> clazz, String templateId, Name<?> resourceName, String path) { Transaction transaction = RuntimeDelegate.getInstance().getTransactionManager().currentTransaction(); transaction.begin(); Resource newResource=this.modelFactory.createResource(this.tms.templateOfId(templateId),resourceName); T resource=clazz.cast(newResource); RuntimeDelegate.getInstance().getResourceRepository().add(resource); Endpoint endpoint=this.modelFactory.createEndpoint(path,resource,new Date(),EntityTag.createStrong(path)); RuntimeDelegate.getInstance().getEndpointRepository().add(endpoint); transaction.commit(); return resource; }
void rollbackSession(final DelegatedWriteSession session) { logLifecycleMessage("Rolling back session..."); UnitOfWork.getCurrent().accept(new RollbackResourceProcessor()); this.transactionManager.currentTransaction().rollback(); }
void commitSession(final DelegatedWriteSession session) { logLifecycleMessage("Commiting session..."); UnitOfWork.getCurrent().accept(new ResourceProcessor(session)); this.transactionManager.currentTransaction().commit(); }
private void commitSession(final Action action, final ResourceSnapshot resource) throws Exception { logAction(Stage.TERMINATION,action,resource); this.uow.accept(new UnitOfWorkInspector()); this.sut.saveChanges(); this.sut.close(); assertThat(this.transaction.isActive(),equalTo(false)); }
@Test public void testCreateSession$happyPath() throws Exception { new Expectations() {{ engine.unwrap(DefaultApplicationEngine.class);result=engine; engine.writeSessionService();result=sessionService; sessionService.createSession((WriteSessionConfiguration)any);result=session; engine.transactionManager();result=transactionManager; transactionManager.currentTransaction();result=transaction; }}; new MockUp<ApplicationEngine>() { @Mock public ApplicationEngine engine() throws ApplicationEngineException { return engine; } }; CoreRuntimeDelegate sut=new CoreRuntimeDelegate(); assertThat(sut.createSession(),notNullValue()); }
public <T> T transactional(Callable<T> callable) throws Exception { Transaction transaction = RuntimeDelegate.getInstance().getTransactionManager().currentTransaction(); transaction.begin(); try { return callable.call(); } catch(Exception e) { e.printStackTrace(); throw e; } finally { try { assertThat(transaction.isActive(),equalTo(false)); } catch (Exception e) { e.printStackTrace(); transaction.rollback(); } } }
@Override public WriteSession createSession() throws ApplicationContextException { try { WriteSessionService sessionService = sessionService(); WriteSession delegate = sessionService. createSession( WriteSessionConfiguration. builder(). build()); Transaction transaction=transactionManager().currentTransaction(); transaction.begin(); return new TransactionalWriteSession(transaction,delegate); } catch (Exception e) { throw new ApplicationContextException("Could not create session",e); } }
private void discardSession(final Action action, final ResourceSnapshot resource) throws Exception { logAction(Stage.TERMINATION,action,resource); this.uow.accept(new UnitOfWorkInspector()); this.sut.discardChanges(); this.sut.close(); assertThat(this.transaction.isActive(),equalTo(false)); }
@Override public void commit() { try { nativeTransaction().commit(); } catch (Exception e) { throw new TransactionException("Commit failed",e); } finally { if(!provider.isActive()) { provider.close(); } } }
private void initialize(Application<T> application) throws ApplicationConfigurationException { Transaction transaction=this.transactionManager.currentTransaction(); transaction.begin(); try { WriteSession session = writeSessionService(). createSession( WriteSessionConfiguration. builder(). build()); Throwable failure=null; try { application.initialize(session); } catch (ApplicationInitializationException e) { failure=e; } finally { failure=closeQuietly(session, failure); } if(failure!=null) { throw new ApplicationConfigurationException(failure); } } finally { if(transaction.isActive()) { transaction.rollback(); } } }
private void doPrepareSession(final org.ldp4j.application.kernel.resource.Resource resource) { this.transaction=RuntimeDelegate.getInstance().getTransactionManager().currentTransaction(); this.transaction.begin(); this.sut = this.writeSessionService.createSession( WriteSessionConfiguration. builder(). withTarget(resource). build() ); this.uow = UnitOfWork.getCurrent(); }
@Override public void rollback() { if(provider.isActive()) { try { nativeTransaction().rollback(); } catch (Exception e) { throw new TransactionException("Rollback failed",e); } finally { provider.close(); } } }
private Resource initialize(final String id, final String path) { final Transaction transaction= RuntimeDelegate. getInstance(). getTransactionManager(). currentTransaction(); transaction.begin(); try { this.uow = UnitOfWork.newCurrent(); final Resource resource= this.modelFactory.createResource( this.templateManagementService.templateOfId("personTemplate"), name(id)); final Endpoint endpoint=this.modelFactory.createEndpoint(path,resource,new Date(),EntityTag.createStrong(path)); RuntimeDelegate.getInstance().getResourceRepository().add(resource); RuntimeDelegate.getInstance().getEndpointRepository().add(endpoint); UnitOfWork.setCurrent(null); transaction.commit(); return resource; } finally { if(transaction.isActive()) { transaction.rollback(); } } }