use of org.springframework.transaction.support.TransactionOperations in project uPortal by Jasig.
the class BaseJpaDao method createCriteriaQuery.
/**
* Factory method for creating a {@link CriteriaQuery} employing standards and best practices in
* general use within the portal. Query objects returned from this method should normally be
* passed to {@link createCachedQuery}; this step is important for the sake of scalability.
*/
protected final <T> CriteriaQuery<T> createCriteriaQuery(Function<CriteriaBuilder, CriteriaQuery<T>> builder) {
final EntityManager entityManager = this.getEntityManager();
final EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
final CriteriaBuilder criteriaBuilder = entityManagerFactory.getCriteriaBuilder();
final CriteriaQuery<T> criteriaQuery = builder.apply(criteriaBuilder);
//Do in TX so the EM gets closed correctly
final TransactionOperations transactionOperations = this.getTransactionOperations();
transactionOperations.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
entityManager.createQuery(//pre-compile critera query to avoid race conditions when setting aliases
criteriaQuery);
}
});
return criteriaQuery;
}
use of org.springframework.transaction.support.TransactionOperations in project uPortal by Jasig.
the class JdbcAuthDao method createToken.
protected void createToken(final String serviceName) {
try {
this.jdbcOperations.execute(new ConnectionCallback<Object>() {
@Override
public Object doInConnection(Connection con) throws SQLException, DataAccessException {
//This is horribly hacky but we can't rely on the main uPortal TM directly or we get
//into a circular dependency loop from JPA to Ehcache to jGroups and back to JPA
final DataSource ds = new SingleConnectionDataSource(con, true);
final PlatformTransactionManager ptm = new DataSourceTransactionManager(ds);
final TransactionOperations to = new TransactionTemplate(ptm);
to.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
logger.info("Creating jGroups auth token");
final String authToken = RandomTokenGenerator.INSTANCE.generateRandomToken(authTokenLength);
final ImmutableMap<String, String> params = ImmutableMap.of(PRM_SERVICE_NAME, serviceName, PRM_RANDOM_TOKEN, authToken);
namedParameterJdbcOperations.update(INSERT_SQL, params);
}
});
return null;
}
});
} catch (ConstraintViolationException e) {
//Ignore, just means a concurrent token creation
} catch (DataIntegrityViolationException e) {
//Ignore, just means a concurrent token creation
}
}
Aggregations