use of org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup in project cas by apereo.
the class JpaBeans method newDataSource.
/**
* Get new data source, from JNDI lookup or created via direct configuration
* of Hikari pool.
* <p>
* If properties specify a data source name, a lookup will be
* attempted. If the DataSource is not found via JNDI then CAS will attempt to
* configure a Hikari connection pool.
* <p>
* Since the datasource beans are {@link org.springframework.cloud.context.config.annotation.RefreshScope},
* they will be a proxied by Spring
* and on some application servers there have been classloading issues. A workaround
* for this is to use activate data source proxying via settings and then the dataSource will be
* wrapped in an application level class. If that is an issue, don't do it.
* <p>
* If user wants to do lookup as resource, they may include {@code java:/comp/env}
* in {@code dataSourceName} and put resource reference in web.xml
* otherwise {@code dataSourceName} is used as JNDI name.
*
* @param jpaProperties the jpa properties
* @return the data source
*/
@SneakyThrows
public static CloseableDataSource newDataSource(final AbstractJpaProperties jpaProperties) {
val dataSourceName = jpaProperties.getDataSourceName();
if (StringUtils.isNotBlank(dataSourceName)) {
try {
val dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(false);
val containerDataSource = dsLookup.getDataSource(dataSourceName);
return new DefaultCloseableDataSource(containerDataSource);
} catch (final DataSourceLookupFailureException e) {
LOGGER.warn("Lookup of datasource [{}] failed due to [{}]. Back to JPA properties.", dataSourceName, e.getMessage());
}
}
val bean = new HikariDataSource();
if (StringUtils.isNotBlank(jpaProperties.getDriverClass())) {
bean.setDriverClassName(jpaProperties.getDriverClass());
}
val url = SpringExpressionLanguageValueResolver.getInstance().resolve(jpaProperties.getUrl());
bean.setJdbcUrl(url);
bean.setUsername(jpaProperties.getUser());
bean.setPassword(jpaProperties.getPassword());
bean.setLoginTimeout((int) Beans.newDuration(jpaProperties.getPool().getMaxWait()).getSeconds());
bean.setMaximumPoolSize(jpaProperties.getPool().getMaxSize());
bean.setMinimumIdle(jpaProperties.getPool().getMinSize());
bean.setIdleTimeout((int) Beans.newDuration(jpaProperties.getIdleTimeout()).toMillis());
bean.setLeakDetectionThreshold(jpaProperties.getLeakThreshold());
bean.setInitializationFailTimeout(jpaProperties.getFailFastTimeout());
bean.setIsolateInternalQueries(jpaProperties.isIsolateInternalQueries());
bean.setConnectionTestQuery(jpaProperties.getHealthQuery());
bean.setAllowPoolSuspension(jpaProperties.getPool().isSuspension());
bean.setAutoCommit(jpaProperties.isAutocommit());
bean.setValidationTimeout(jpaProperties.getPool().getTimeoutMillis());
bean.setReadOnly(jpaProperties.isReadOnly());
bean.setPoolName(jpaProperties.getPool().getName());
return new DefaultCloseableDataSource(bean);
}
use of org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup in project spring-boot by spring-projects.
the class JndiDataSourceAutoConfiguration method dataSource.
@Bean(destroyMethod = "")
@ConditionalOnMissingBean
public DataSource dataSource(DataSourceProperties properties, ApplicationContext context) {
JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
DataSource dataSource = dataSourceLookup.getDataSource(properties.getJndiName());
excludeMBeanIfNecessary(dataSource, "dataSource", context);
return dataSource;
}
use of org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup in project tutorials by eugenp.
the class DataSourceLookupExceptionManualTest method whenLookupNonExistentDataSource_thenDataSourceLookupFailureException.
@Test(expected = DataSourceLookupFailureException.class)
public void whenLookupNonExistentDataSource_thenDataSourceLookupFailureException() {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
final DataSource dataSource = dsLookup.getDataSource("java:comp/env/jdbc/example_db");
}
use of org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup in project spring-framework by spring-projects.
the class PersistenceXmlParsingTests method testJpa1ExcludeUnlisted.
@Test
public void testJpa1ExcludeUnlisted() throws Exception {
PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
String resource = "/org/springframework/orm/jpa/persistence-exclude-1.0.xml";
PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
assertThat(info).isNotNull();
assertThat(info.length).as("The number of persistence units is incorrect.").isEqualTo(4);
PersistenceUnitInfo noExclude = info[0];
assertThat(noExclude).as("noExclude should not be null.").isNotNull();
assertThat(noExclude.getPersistenceUnitName()).as("noExclude name is not correct.").isEqualTo("NoExcludeElement");
assertThat(noExclude.excludeUnlistedClasses()).as("Exclude unlisted should default false in 1.0.").isFalse();
PersistenceUnitInfo emptyExclude = info[1];
assertThat(emptyExclude).as("emptyExclude should not be null.").isNotNull();
assertThat(emptyExclude.getPersistenceUnitName()).as("emptyExclude name is not correct.").isEqualTo("EmptyExcludeElement");
assertThat(emptyExclude.excludeUnlistedClasses()).as("emptyExclude should be true.").isTrue();
PersistenceUnitInfo trueExclude = info[2];
assertThat(trueExclude).as("trueExclude should not be null.").isNotNull();
assertThat(trueExclude.getPersistenceUnitName()).as("trueExclude name is not correct.").isEqualTo("TrueExcludeElement");
assertThat(trueExclude.excludeUnlistedClasses()).as("trueExclude should be true.").isTrue();
PersistenceUnitInfo falseExclude = info[3];
assertThat(falseExclude).as("falseExclude should not be null.").isNotNull();
assertThat(falseExclude.getPersistenceUnitName()).as("falseExclude name is not correct.").isEqualTo("FalseExcludeElement");
assertThat(falseExclude.excludeUnlistedClasses()).as("falseExclude should be false.").isFalse();
}
use of org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup in project spring-framework by spring-projects.
the class PersistenceXmlParsingTests method testInvalidPersistence.
@Disabled("not doing schema parsing anymore for JPA 2.0 compatibility")
@Test
public void testInvalidPersistence() throws Exception {
PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
String resource = "/org/springframework/orm/jpa/persistence-invalid.xml";
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> reader.readPersistenceUnitInfos(resource));
}
Aggregations