Search in sources :

Example 11 with DataSourceTransactionManager

use of org.springframework.jdbc.datasource.DataSourceTransactionManager 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

DataSourceTransactionManager (org.springframework.jdbc.datasource.DataSourceTransactionManager)11 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)4 ProcessEngineFactoryBean (org.activiti.spring.ProcessEngineFactoryBean)3 Bean (org.springframework.context.annotation.Bean)3 SQLException (java.sql.SQLException)2 Test (org.junit.Test)2 TransactionStatus (org.springframework.transaction.TransactionStatus)2 GargoyleException (com.kyj.fx.voeditor.visual.exceptions.GargoyleException)1 NotSupportException (com.kyj.fx.voeditor.visual.exceptions.NotSupportException)1 File (java.io.File)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 DataSource (javax.sql.DataSource)1 JndiRegistry (org.apache.camel.impl.JndiRegistry)1 SpringTransactionPolicy (org.apache.camel.spring.spi.SpringTransactionPolicy)1 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)1 Before (org.junit.Before)1 DefaultEventConfDao (org.opennms.netmgt.config.DefaultEventConfDao)1 SnmpPeerFactory (org.opennms.netmgt.config.SnmpPeerFactory)1