Search in sources :

Example 16 with SingleConnectionDataSource

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

Example 17 with SingleConnectionDataSource

use of org.springframework.jdbc.datasource.SingleConnectionDataSource in project spring-boot by spring-projects.

the class DataSourceHealthIndicatorTests method init.

@BeforeEach
void init() {
    EmbeddedDatabaseConnection db = EmbeddedDatabaseConnection.HSQLDB;
    this.dataSource = new SingleConnectionDataSource(db.getUrl("testdb") + ";shutdown=true", "sa", "", false);
    this.dataSource.setDriverClassName(db.getDriverClassName());
}
Also used : SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) EmbeddedDatabaseConnection(org.springframework.boot.jdbc.EmbeddedDatabaseConnection) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 18 with SingleConnectionDataSource

use of org.springframework.jdbc.datasource.SingleConnectionDataSource in project citrus-samples by christophd.

the class EndpointConfig method dataSource.

@Bean
public SingleConnectionDataSource dataSource() {
    SingleConnectionDataSource dataSource = new SingleConnectionDataSource();
    dataSource.setDriverClassName(JdbcDriver.class.getName());
    dataSource.setUrl("jdbc:citrus:http://localhost:3306/testdb");
    dataSource.setUsername("sa");
    dataSource.setPassword("");
    return dataSource;
}
Also used : JdbcDriver(com.consol.citrus.db.driver.JdbcDriver) SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) Bean(org.springframework.context.annotation.Bean)

Example 19 with SingleConnectionDataSource

use of org.springframework.jdbc.datasource.SingleConnectionDataSource in project spring-framework by spring-projects.

the class JdbcTemplateTests method testLeaveConnectionOpenOnRequest.

@Test
public void testLeaveConnectionOpenOnRequest() throws Exception {
    String sql = "SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3";
    given(this.resultSet.next()).willReturn(false);
    given(this.connection.isClosed()).willReturn(false);
    given(this.connection.createStatement()).willReturn(this.preparedStatement);
    // if close is called entire test will fail
    willThrow(new RuntimeException()).given(this.connection).close();
    SingleConnectionDataSource scf = new SingleConnectionDataSource(this.dataSource.getConnection(), false);
    this.template = new JdbcTemplate(scf, false);
    RowCountCallbackHandler rcch = new RowCountCallbackHandler();
    this.template.query(sql, rcch);
    verify(this.resultSet).close();
    verify(this.preparedStatement).close();
}
Also used : SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 20 with SingleConnectionDataSource

use of org.springframework.jdbc.datasource.SingleConnectionDataSource in project spring-framework by spring-projects.

the class RowMapperTests method setUp.

@BeforeEach
public void setUp() throws SQLException {
    given(connection.createStatement()).willReturn(statement);
    given(connection.prepareStatement(anyString())).willReturn(preparedStatement);
    given(statement.executeQuery(anyString())).willReturn(resultSet);
    given(preparedStatement.executeQuery()).willReturn(resultSet);
    given(resultSet.next()).willReturn(true, true, false);
    given(resultSet.getString(1)).willReturn("tb1", "tb2");
    given(resultSet.getInt(2)).willReturn(1, 2);
    template.setDataSource(new SingleConnectionDataSource(connection, false));
    template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
    template.afterPropertiesSet();
}
Also used : SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) SQLStateSQLExceptionTranslator(org.springframework.jdbc.support.SQLStateSQLExceptionTranslator) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

SingleConnectionDataSource (org.springframework.jdbc.datasource.SingleConnectionDataSource)20 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)7 Connection (java.sql.Connection)6 DataSource (javax.sql.DataSource)4 Before (org.junit.Before)4 SQLException (java.sql.SQLException)3 Properties (java.util.Properties)3 Test (org.junit.Test)3 KerberosTicketConfiguration (com.thinkbiganalytics.kerberos.KerberosTicketConfiguration)2 DataSetTemplate (com.thinkbiganalytics.kylo.catalog.rest.model.DataSetTemplate)2 DBSchemaParser (com.thinkbiganalytics.schema.DBSchemaParser)2 Nonnull (javax.annotation.Nonnull)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Extension (alfio.extension.Extension)1 ExtensionService (alfio.extension.ExtensionService)1 ScriptingExecutionService (alfio.extension.ScriptingExecutionService)1 ExternalConfiguration (alfio.manager.system.ExternalConfiguration)1 ExtensionLogRepository (alfio.repository.ExtensionLogRepository)1 ExtensionRepository (alfio.repository.ExtensionRepository)1 JdbcDriver (com.consol.citrus.db.driver.JdbcDriver)1