List of usage examples for org.springframework.transaction.support TransactionCallbackWithoutResult TransactionCallbackWithoutResult
TransactionCallbackWithoutResult
From source file:org.bitbucket.fbascheper.tutorial.envers.EnversIllustrationTest.java
@Test public void testOne() { // create a couple of events final Event event1 = getTransactionTemplate().execute(new TransactionCallback<Event>() { @Override//from w w w. j a va 2s . co m public Event doInTransaction(TransactionStatus status) { // revision 1 Event event = new Event("Our very first event!", new Date()); getEntityManager().persist(event); return event; } }); final Event event2 = getTransactionTemplate().execute(new TransactionCallback<Event>() { @Override public Event doInTransaction(TransactionStatus status) { // revision 2 Event event = new Event("A follow up event", new Date()); getEntityManager().persist(event); return event; } }); // now lets pull events from the database and list them List<Event> result = getTransactionTemplate().execute(new TransactionCallback<List<Event>>() { @Override public List<Event> doInTransaction(TransactionStatus status) { List<Event> result = getEntityManager().createQuery("select evt from Event evt", Event.class) .getResultList(); for (Event event : result) { System.out.println("Event (" + event.getDate() + ") : " + event.getTitle()); } return result; } }); // verify that id's were generated final Long event1Id = event1.getId(); final Long event2Id = event2.getId(); assertThat(event1Id, notNullValue()); assertThat(event2Id, notNullValue()); // so far the code is the same as we have seen in previous tutorials. Now lets leverage Envers... // first lets create some revisions getTransactionTemplate().execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { // revision 3 Event myEvent = getEntityManager().find(Event.class, event2Id); myEvent.setDate(new Date()); myEvent.setTitle(myEvent.getTitle() + " (rescheduled)"); } }); // and then use an AuditReader to look back through history getTransactionTemplate().execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { Event myEvent = getEntityManager().find(Event.class, event2Id); assertThat("A follow up event (rescheduled)", is(myEvent.getTitle())); AuditReader reader = AuditReaderFactory.get(getEntityManager()); List<? extends Number> event2Revisions = reader.getRevisions(Event.class, event2Id); assertThat(event2Revisions.size(), is(2)); long event2Revision1 = event2Revisions.get(0).longValue(); long event2Revision2 = event2Revisions.get(1).longValue(); assertThat(event2Revision1, is(2L)); assertThat(event2Revision2, is(3L)); Event firstRevision = reader.find(Event.class, event2Id, event2Revision1); assertThat(firstRevision, notNullValue()); assertThat(firstRevision.getTitle(), notNullValue()); assertThat(firstRevision.getTitle(), not(is(myEvent.getTitle()))); assertThat(firstRevision.getDate(), not(is(myEvent.getDate()))); Event secondRevision = reader.find(Event.class, event2Id, event2Revision2); assertThat(secondRevision, notNullValue()); assertThat(secondRevision.getTitle(), notNullValue()); assertThat(secondRevision.getTitle(), is(myEvent.getTitle())); assertThat(secondRevision.getDate(), is(myEvent.getDate())); } }); }
From source file:org.pentaho.custom.authentication.provider.userroledao.hibernate.UserRoleDaoTransactionDecorator.java
public void deleteRole(final IRole roleToDelete) throws NotFoundException, UncategorizedUserRoleDaoException { transactionTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus status) { userRoleDao.deleteRole(roleToDelete); }// w w w .j av a2 s. c o m }); }
From source file:com.github.akiraly.db4j.uow.UowNotPersistedIfNotNeededTest.java
@Test(timeout = 5000) public void testUowNotPersistedIfNotNeeded() { final String uow1User = "u300"; Uow uow1 = new Uow(uow1User); final Foo foo = new Foo("bar", LocalDateTime.now(ZoneOffset.UTC)); EntityWithLongId<Foo> fooWithId = transactionTemplate.execute(s -> { FooDao fooDao = fooDaoFactory.newDao(); UowDao uowDao = uowDaoFactory.newDao(); assertEquals(0, fooDao.count()); assertEquals(0, uowDao.count()); EntityWithLongId<Foo> result = fooDao.lazyPersist(foo); assertEquals(1, result.getId()); assertEquals(1, fooDao.count()); assertEquals(0, uowDao.count()); return result; });//from w w w . jav a 2s .c o m transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { FooDao fooDao = fooDaoFactory.newDao(); assertEquals(1, fooDao.count()); assertEquals("bar", fooDao.lazyFind(fooWithId.getId()).getEntity().getBar()); assertEquals(1, fooDao.deleteAll()); } }); transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { UowDao uowDao = uowDaoFactory.newDao(); AuditedFooDao auditedFooDao = auditedFooDaoFactory.newDao(uowDao); assertEquals(0, uowDao.count()); EntityWithLongId<Uow> uowWithId1 = uowDao.lazyPersist(uow1); AuditedFoo auditedFoo = new AuditedFoo("bar", uowWithId1); EntityWithLongId<AuditedFoo> fooWithId = auditedFooDao.lazyPersist(auditedFoo); assertEquals("bar", fooWithId.getEntity().getBar()); assertEquals(0, uowDao.count()); } }); transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { UowDao uowDao = uowDaoFactory.newDao(); AuditedFooUuidDao auditedFooUuidDao = auditedFooUuidDaoFactory.newDao(uowDao); assertEquals(0, uowDao.count()); EntityWithLongId<Uow> uowWithId1 = uowDao.lazyPersist(uow1); AuditedFoo auditedFoo = new AuditedFoo("bar", uowWithId1); EntityWithUuid<AuditedFoo> fooWithId = auditedFooUuidDao.lazyPersist(auditedFoo); assertEquals("bar", fooWithId.getEntity().getBar()); assertEquals(0, uowDao.count()); } }); }
From source file:org.openxdata.server.service.impl.SchedulerServiceImpl.java
@Override public void start() { if (scheduler == null) return;//from w ww . j av a2 s . co m /* * Use programatic transaction management because * this method is called by spring via reflection. * * AOP based transaction management does not work in * conjunction with reflection. * * @see http://www.eclipse.org/aspectj/doc/released/faq.php#q:reflectiveCalls * */ this.transactionTemplate = new TransactionTemplate(transactionManager); this.transactionTemplate.setReadOnly(true); transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { try { List<TaskDef> taskDefs = taskDAO.getTasks(); if (taskDefs != null) { for (TaskDef taskDef : taskDefs) { scheduleTask(taskDef); } } scheduler.start(); log.info("Started Scheduling Service"); } catch (SchedulerException ex) { status.setRollbackOnly(); throw new UnexpectedException(ex); } } }); }
From source file:edu.vt.middleware.gator.log4j.SocketServerTest.java
/** * Test setup routine called before each test method. * * @throws Exception On errors./*from w w w . java 2 s. c om*/ */ @BeforeTransaction public void setUp() throws Exception { testProject = UnitTestHelper.createProject("p", "a1", "a2", server.getBindAddress(), "127.0.0.2", TEST_CATEGORY); new TransactionTemplate(txManager).execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(final TransactionStatus status) { configManager.save(testProject); } }); }
From source file:eu.databata.engine.util.DBHistoryLogger.java
protected void outputInTransaction(final HistoryLogEntry entry) { if (simulationMode) { return;/*from w w w .j av a 2s . c om*/ } transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { propagationDAO.insertHistoryLog(moduleName, entry); } }); }
From source file:com.formkiq.core.service.AssetServiceS3Default.java
@Transactional @Override//from w w w .ja v a2s . c o m public void afterPropertiesSet() throws Exception { TransactionTemplate tmpl = new TransactionTemplate(this.txManager); tmpl.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(final TransactionStatus status) { reloadProperties(); } }); }
From source file:pl.touk.wonderfulsecurity.utils.PrepareDemoData.java
public void initialize() { if (!initialized) { initialized = true;/*from w ww . ja v a2s . c o m*/ if (mode == MODE_DEMO) { log.info("PREPARE DEMO DATA WORKS IN MODE_DEMO"); hibernateTransactionTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus status) { FixtureUtils.prepareUserTestFixture(wsecUserDao, wsecGroupDao, wsecRoleDao, wsecPermissionDao); FixtureUtils.prepareFixtureForUserGroupAssociation(wsecUserDao, wsecGroupDao); FixtureUtils.prepareFixtureForGroupRoleAssociation(wsecGroupDao, wsecRoleDao); FixtureUtils.prepareFixtureForPermissionCollisionTesting(wsecUserDao, wsecRoleDao, wsecGroupDao, wsecPermissionDao); } }); } if (mode == MODE_PRODUCTION) { log.info("PREPARE DEMO DATA WORKS IN PRODUCTION MODE, NOT INSERTING STARTUP DATA"); } } }
From source file:com.quartzdesk.executor.dao.schema.DatabaseSchemaManager.java
/** * Attempts to initialize, or upgrade the QuartzDesk database schema to the * current application schema version.//from w w w .ja v a 2s . co m */ @PostConstruct protected void maybeInitializeOrUpgradeDatabaseSchema() { final Version desiredSchemaVersion = new Version().withMajor(productVersion.getMajor()) .withMinor(productVersion.getMinor()).withMaintenance(productVersion.getMaintenance()); // we must enclose the logic in a transaction so that the Hibernate session is available // in the invoked DatabaseSchemaDao TransactionTemplate txTemplate = new TransactionTemplate(transactionManager); txTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { SchemaUpdate latestSchemaUpdate = databaseSchemaDao.getLatestSchemaUpdate(); if (latestSchemaUpdate == null) { /* * Schema is empty, apply DB initialization scripts. */ List<URL> scriptUrls = getInitScriptUrls(); log.info("Initializing empty database schema to {} by applying SQL scripts: {}{}", new Object[] { VersionConverter.INSTANCE.toString(desiredSchemaVersion), CommonConst.NL, dumpScriptList(scriptUrls) }); databaseSchemaDao.initializeOrUpgradeSchema(scriptUrls, desiredSchemaVersion); } else { Version currentSchemaVersion = new Version().withMajor(latestSchemaUpdate.getMajor()) .withMinor(latestSchemaUpdate.getMinor()) .withMaintenance(latestSchemaUpdate.getMaintenance()); log.info("Detected database schema version: {}", VersionConverter.INSTANCE.toString(currentSchemaVersion)); /* * Check the current schema version is not > then the desired schema * version. Downgrades are not supported. */ checkSchemaVersion(currentSchemaVersion, desiredSchemaVersion); if (currentSchemaVersion.equals(desiredSchemaVersion)) { log.info("Database schema is up-to-date."); } else { List<URL> scriptUrls = getUpgradeScriptUrls(currentSchemaVersion, desiredSchemaVersion); if (scriptUrls.isEmpty()) { log.info( "Formally upgrading database schema {} -> {} because no SQL upgrade scripts are available for {} -> {}", new Object[] { VersionConverter.INSTANCE.toString(currentSchemaVersion), VersionConverter.INSTANCE.toString(desiredSchemaVersion), VersionConverter.INSTANCE.toString(currentSchemaVersion), VersionConverter.INSTANCE.toString(desiredSchemaVersion) }); // formal upgrade of the QuartzDesk schema version to the current QuartzDesk version SchemaUpdate schemaUpdate = new SchemaUpdate() .withMajor(desiredSchemaVersion.getMajor()) .withMinor(desiredSchemaVersion.getMinor()) .withMaintenance(desiredSchemaVersion.getMaintenance()) .withAppliedAt(Calendar.getInstance()); databaseSchemaDao.insertSchemaUpdate(schemaUpdate); } else { log.info("Upgrading database schema {} -> {} by applying SQL upgrade scripts: {}{}", new Object[] { VersionConverter.INSTANCE.toString(currentSchemaVersion), VersionConverter.INSTANCE.toString(desiredSchemaVersion), CommonConst.NL, dumpScriptList(scriptUrls) }); // applies scripts and inserts a new schema update record databaseSchemaDao.initializeOrUpgradeSchema(scriptUrls, desiredSchemaVersion); } } } } }); }
From source file:com.thoughtworks.go.server.cache.GoCacheIntegrationTest.java
@Test public void put_shouldNotUpdateCacheWhenInTransaction() { transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override//from w w w. java 2s. co m protected void doInTransactionWithoutResult(TransactionStatus status) { Object o = new Object(); goCache.put("someKey", o); } }); assertNull(goCache.get("someKey")); }