List of usage examples for org.springframework.transaction.support TransactionCallback TransactionCallback
TransactionCallback
From source file:org.jasig.cas.ticket.registry.JpaTicketRegistryTests.java
Ticket getTicketInTransaction(final String ticketId) { return new TransactionTemplate(txManager).execute(new TransactionCallback<Ticket>() { public Ticket doInTransaction(final TransactionStatus status) { return jpaTicketRegistry.getTicket(ticketId); }/*from w ww. j a v a 2 s. co m*/ }); }
From source file:ch.tatool.app.service.impl.DataServiceImpl.java
/** * Get all distinct trial property names contained in a module * // w ww . java2s . c o m * @return a List of object arrays containing [0] the item name and [1] the property name */ @SuppressWarnings("unchecked") public List<Object[]> findDistinctTrialPropertyNames(final Module module) { final ModuleImpl moduleImpl = (ModuleImpl) module; return (List<Object[]>) moduleImpl.getTransactionTemplate().execute(new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { return moduleImpl.getTrialDAO().findDistinctTrialPropertyNames(moduleImpl); } }); }
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private List<Product> getProducts_Mercifully() { return transactionTemplate.execute(new TransactionCallback<List<Product>>() { @Override/*ww w .j a v a 2s .c o m*/ public List<Product> doInTransaction(TransactionStatus transactionStatus) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Product> query = cb.createQuery(Product.class); Root<Image> imageRoot = query.from(Image.class); Join<Image, Product> productJoin = imageRoot.join(Image_.product); query.select(productJoin); query.distinct(true); List<Predicate> criteria = new ArrayList<Predicate>(); criteria.add(cb.like(cb.lower(productJoin.get(Product_.name)), "%tv%")); criteria.add(cb.gt(imageRoot.get(Image_.index), 0)); query.where(cb.and(criteria.toArray(new Predicate[criteria.size()]))); return entityManager.createQuery(query).getResultList(); } }); }
From source file:jp.go.aist.six.util.core.persist.castor.CastorDatastore.java
public <K, T extends Persistable<K>> Collection<K> findIdentity(final Class<T> type) { Collection<K> p_ids = _executeTx("findIdentity", type, new TransactionCallback<Collection<K>>() { public Collection<K> doInTransaction(final TransactionStatus status) { return getDao(type).findIdentity(); }/*from ww w . ja va 2 s.co m*/ }); return p_ids; }
From source file:org.jasig.cas.ticket.registry.JpaTicketRegistryTests.java
ServiceTicket grantServiceTicketInTransaction(final TicketGrantingTicket parent) { return new TransactionTemplate(txManager).execute(new TransactionCallback<ServiceTicket>() { public ServiceTicket doInTransaction(final TransactionStatus status) { final ServiceTicket st = newST(parent); jpaTicketRegistry.addTicket(st); return st; }/*from w w w . j a v a 2 s .c o m*/ }); }
From source file:com.hs.mail.imap.user.DefaultUserManager.java
public long addAlias(final Alias alias) { return (Long) getTransactionTemplate().execute(new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { try { return DaoFactory.getUserDao().addAlias(alias); } catch (DataAccessException ex) { status.setRollbackOnly(); throw ex; }// w ww .j ava 2s.c om } }); }
From source file:org.openvpms.archetype.rules.patient.PatientMergerTestCase.java
/** * Merges two patients in a transaction, and verifies the 'from' patient * has been deleted.//from www. j a va2 s . co m * * @param from the patient to merge from * @param to the patient to merge to * @return the merged patient */ private Party checkMerge(final Party from, final Party to) { template.execute(new TransactionCallback<Object>() { public Object doInTransaction(TransactionStatus transactionStatus) { rules.mergePatients(from, to); return null; } }); // verify the from patient has been deleted assertNull(get(from)); Party merged = get(to); assertNotNull(merged); return merged; }
From source file:com.thoughtworks.go.server.dao.PipelineSqlMapDao.java
public Pipeline save(final Pipeline pipeline) { return (Pipeline) transactionTemplate.execute(new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { transactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() { @Override/*from ww w . j a va 2 s .c o m*/ public void afterCommit() { goCache.remove(cacheKeyForLatestPipelineIdByPipelineName(pipeline.getName())); invalidateCacheConditionallyForPipelineInstancesTriggeredWithDependencyMaterial(pipeline); } }); pipelineByBuildIdCache.flushOnCommit(); getSqlMapClientTemplate().insert("insertPipeline", pipeline); savePipelineMaterialRevisions(pipeline, pipeline.getId()); environmentVariableDao.save(pipeline.getId(), EnvironmentVariableType.Trigger, pipeline.scheduleTimeVariables()); return pipeline; } }); }
From source file:org.apache.camel.processor.aggregate.jdbc.OptimisticLockingJdbcAggregationRepository.java
@Override public Exchange get(CamelContext camelContext, String key) { return getTransactionTemplate().execute(new TransactionCallback<Exchange>() { @Override/*from w w w.j a v a 2s. co m*/ public Exchange doInTransaction(TransactionStatus status) { try { Map<String, Object> columns = getJdbcTemplate().queryForMap( String.format("SELECT %1$s, %2$s FROM %3$s WHERE %4$s=?", EXCHANGE_KEY, VERSION_KEY, getRepositoryName(), ID_KEY), new Object[] { key }, new int[] { Types.VARCHAR }); byte[] marshalledExchange = (byte[]) columns.get(EXCHANGE_KEY); long version = (long) columns.get(VERSION_KEY); Exchange result = getCodec().unmarshallExchange(camelContext, marshalledExchange); result.setProperty(VERSION_EXCHANGE_PROPERTY, version); return result; } catch (EmptyResultDataAccessException e) { return null; } catch (IOException e) { throw new RuntimeException( String.format("Error getting key [%s] from repository [%s].", key, getRepositoryName()), e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } }); }
From source file:sample.contact.DataSourcePopulator.java
public void afterPropertiesSet() throws Exception { Assert.notNull(mutableAclService, "mutableAclService required"); Assert.notNull(template, "dataSource required"); Assert.notNull(tt, "platformTransactionManager required"); // Set a user account that will initially own all the created data Authentication authRequest = new UsernamePasswordAuthenticationToken("rod", "koala", AuthorityUtils.createAuthorityList("ROLE_IGNORED")); SecurityContextHolder.getContext().setAuthentication(authRequest); try {/*from ww w .j a v a 2 s . c om*/ template.execute("DELETE FROM CONTACTS"); template.execute("DELETE FROM AUTHORITIES"); template.execute("DELETE FROM USERS"); template.execute("DELETE FROM ACL_ENTRY"); template.execute("DELETE FROM ACL_OBJECT_IDENTITY"); template.execute("DELETE FROM ACL_CLASS"); template.execute("DELETE FROM ACL_SID"); } catch (Exception e) { System.out.println("Failed to DELETE FROMs: " + e.getMessage()); } /* * Passwords encoded using MD5, NOT in Base64 format, with null as salt Encoded * password for rod is "koala" Encoded password for dianne is "emu" Encoded * password for scott is "wombat" Encoded password for peter is "opal" (but user * is disabled) Encoded password for bill is "wombat" Encoded password for bob is * "wombat" Encoded password for jane is "wombat" */ template.execute( "INSERT INTO USERS VALUES('rod','$2a$10$75pBjapg4Nl8Pzd.3JRnUe7PDJmk9qBGwNEJDAlA3V.dEJxcDKn5O',TRUE);"); template.execute( "INSERT INTO USERS VALUES('dianne','$2a$04$bCMEyxrdF/7sgfUiUJ6Ose2vh9DAMaVBldS1Bw2fhi1jgutZrr9zm',TRUE);"); template.execute( "INSERT INTO USERS VALUES('scott','$2a$06$eChwvzAu3TSexnC3ynw4LOSw1qiEbtNItNeYv5uI40w1i3paoSfLu',TRUE);"); template.execute( "INSERT INTO USERS VALUES('peter','$2a$04$8.H8bCMROLF4CIgd7IpeQ.tcBXLP5w8iplO0n.kCIkISwrIgX28Ii',FALSE);"); template.execute( "INSERT INTO USERS VALUES('bill','$2a$04$8.H8bCMROLF4CIgd7IpeQ.3khQlPVNWbp8kzSQqidQHGFurim7P8O',TRUE);"); template.execute( "INSERT INTO USERS VALUES('bob','$2a$06$zMgxlMf01SfYNcdx7n4NpeFlAGU8apCETz/i2C7VlYWu6IcNyn4Ay',TRUE);"); template.execute( "INSERT INTO USERS VALUES('jane','$2a$05$ZrdS7yMhCZ1J.AAidXZhCOxdjD8LO/dhlv4FJzkXA6xh9gdEbBT/u',TRUE);"); template.execute("INSERT INTO AUTHORITIES VALUES('rod','ROLE_USER');"); template.execute("INSERT INTO AUTHORITIES VALUES('rod','ROLE_ADMIN');"); template.execute("INSERT INTO AUTHORITIES VALUES('dianne','ROLE_USER');"); template.execute("INSERT INTO AUTHORITIES VALUES('scott','ROLE_USER');"); template.execute("INSERT INTO AUTHORITIES VALUES('peter','ROLE_USER');"); template.execute("INSERT INTO AUTHORITIES VALUES('bill','ROLE_USER');"); template.execute("INSERT INTO AUTHORITIES VALUES('bob','ROLE_USER');"); template.execute("INSERT INTO AUTHORITIES VALUES('jane','ROLE_USER');"); template.execute("INSERT INTO AUTHORITIES VALUES('jane','ROLE_ADMIN');"); template.execute("INSERT INTO contacts VALUES (1, 'John Smith', 'john@somewhere.com');"); template.execute("INSERT INTO contacts VALUES (2, 'Michael Citizen', 'michael@xyz.com');"); template.execute("INSERT INTO contacts VALUES (3, 'Joe Bloggs', 'joe@demo.com');"); template.execute("INSERT INTO contacts VALUES (4, 'Karen Sutherland', 'karen@sutherland.com');"); template.execute("INSERT INTO contacts VALUES (5, 'Mitchell Howard', 'mitchell@abcdef.com');"); template.execute("INSERT INTO contacts VALUES (6, 'Rose Costas', 'rose@xyz.com');"); template.execute("INSERT INTO contacts VALUES (7, 'Amanda Smith', 'amanda@abcdef.com');"); template.execute("INSERT INTO contacts VALUES (8, 'Cindy Smith', 'cindy@smith.com');"); template.execute("INSERT INTO contacts VALUES (9, 'Jonathan Citizen', 'jonathan@xyz.com');"); for (int i = 10; i < createEntities; i++) { String[] person = selectPerson(); template.execute("INSERT INTO contacts VALUES (" + i + ", '" + person[2] + "', '" + person[0].toLowerCase() + "@" + person[1].toLowerCase() + ".com');"); } // Create acl_object_identity rows (and also acl_class rows as needed for (int i = 1; i < createEntities; i++) { final ObjectIdentity objectIdentity = new ObjectIdentityImpl(Contact.class, new Long(i)); tt.execute(new TransactionCallback<Object>() { public Object doInTransaction(TransactionStatus arg0) { mutableAclService.createAcl(objectIdentity); return null; } }); } // Now grant some permissions grantPermissions(1, "rod", BasePermission.ADMINISTRATION); grantPermissions(2, "rod", BasePermission.READ); grantPermissions(3, "rod", BasePermission.READ); grantPermissions(3, "rod", BasePermission.WRITE); grantPermissions(3, "rod", BasePermission.DELETE); grantPermissions(4, "rod", BasePermission.ADMINISTRATION); grantPermissions(4, "dianne", BasePermission.ADMINISTRATION); grantPermissions(4, "scott", BasePermission.READ); grantPermissions(5, "dianne", BasePermission.ADMINISTRATION); grantPermissions(5, "dianne", BasePermission.READ); grantPermissions(6, "dianne", BasePermission.READ); grantPermissions(6, "dianne", BasePermission.WRITE); grantPermissions(6, "dianne", BasePermission.DELETE); grantPermissions(6, "scott", BasePermission.READ); grantPermissions(7, "scott", BasePermission.ADMINISTRATION); grantPermissions(8, "dianne", BasePermission.ADMINISTRATION); grantPermissions(8, "dianne", BasePermission.READ); grantPermissions(8, "scott", BasePermission.READ); grantPermissions(9, "scott", BasePermission.ADMINISTRATION); grantPermissions(9, "scott", BasePermission.READ); grantPermissions(9, "scott", BasePermission.WRITE); grantPermissions(9, "scott", BasePermission.DELETE); // Now expressly change the owner of the first ten contacts // We have to do this last, because "rod" owns all of them (doing it sooner would // prevent ACL updates) // Note that ownership has no impact on permissions - they're separate (ownership // only allows ACl editing) changeOwner(5, "dianne"); changeOwner(6, "dianne"); changeOwner(7, "scott"); changeOwner(8, "dianne"); changeOwner(9, "scott"); String[] users = { "bill", "bob", "jane" }; // don't want to mess around with // consistent sample data Permission[] permissions = { BasePermission.ADMINISTRATION, BasePermission.READ, BasePermission.DELETE }; for (int i = 10; i < createEntities; i++) { String user = users[rnd.nextInt(users.length)]; Permission permission = permissions[rnd.nextInt(permissions.length)]; grantPermissions(i, user, permission); String user2 = users[rnd.nextInt(users.length)]; Permission permission2 = permissions[rnd.nextInt(permissions.length)]; grantPermissions(i, user2, permission2); } SecurityContextHolder.clearContext(); }