Search in sources :

Example 1 with TransactionOperations

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;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) TransactionOperations(org.springframework.transaction.support.TransactionOperations) EntityManagerFactory(javax.persistence.EntityManagerFactory) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 2 with TransactionOperations

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
    }
}
Also used : SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) TransactionOperations(org.springframework.transaction.support.TransactionOperations) SQLException(java.sql.SQLException) Connection(java.sql.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) DataSource(javax.sql.DataSource) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataAccessException(org.springframework.dao.DataAccessException) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Aggregations

TransactionStatus (org.springframework.transaction.TransactionStatus)2 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)2 TransactionOperations (org.springframework.transaction.support.TransactionOperations)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 EntityManager (javax.persistence.EntityManager)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 DataSource (javax.sql.DataSource)1 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)1 DataAccessException (org.springframework.dao.DataAccessException)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1 DataSourceTransactionManager (org.springframework.jdbc.datasource.DataSourceTransactionManager)1 SingleConnectionDataSource (org.springframework.jdbc.datasource.SingleConnectionDataSource)1 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)1 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)1