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
}
}
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());
}
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;
}
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();
}
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();
}
Aggregations