Search in sources :

Example 11 with JndiDataSourceLookup

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);
}
Also used : lombok.val(lombok.val) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSourceLookupFailureException(org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException) JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) SneakyThrows(lombok.SneakyThrows)

Example 12 with JndiDataSourceLookup

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;
}
Also used : JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) DataSource(javax.sql.DataSource) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 13 with JndiDataSourceLookup

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");
}
Also used : JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 14 with JndiDataSourceLookup

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();
}
Also used : JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) Test(org.junit.jupiter.api.Test)

Example 15 with JndiDataSourceLookup

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));
}
Also used : JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

JndiDataSourceLookup (org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup)17 Test (org.junit.jupiter.api.Test)11 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)11 PersistenceUnitInfo (jakarta.persistence.spi.PersistenceUnitInfo)9 DataSource (javax.sql.DataSource)4 Bean (org.springframework.context.annotation.Bean)3 ClassPathResource (org.springframework.core.io.ClassPathResource)3 Disabled (org.junit.jupiter.api.Disabled)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 ProxyDataSource (com.haulmont.cuba.core.sys.jdbc.ProxyDataSource)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 SneakyThrows (lombok.SneakyThrows)1 lombok.val (lombok.val)1 Test (org.junit.Test)1 SimpleNamingContextBuilder (org.springframework.context.testfixture.jndi.SimpleNamingContextBuilder)1 Environment (org.springframework.core.env.Environment)1 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)1 DataSourceLookup (org.springframework.jdbc.datasource.lookup.DataSourceLookup)1 DataSourceLookupFailureException (org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException)1