Search in sources :

Example 1 with DataSourceLookupFailureException

use of org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException in project cuba by cuba-platform.

the class DataStoresCheck method checkDataStore.

protected List<CheckFailedResult> checkDataStore(String storeName, ConnectionSupplier connectionSupplier) {
    List<CheckFailedResult> result = new ArrayList<>();
    Connection connection = null;
    try {
        log.info("Checking connection to data store {}", Stores.storeNameToString(storeName));
        connection = connectionSupplier.get();
        DatabaseMetaData dbMetaData = connection.getMetaData();
        if (Stores.isMain(storeName) && !Boolean.TRUE.equals(Boolean.parseBoolean(AppContext.getProperty("cuba.automaticDatabaseUpdate")))) {
            DbProperties dbProperties = new DbProperties(dbMetaData.getURL());
            boolean isRequiresCatalog = DbmsSpecificFactory.getDbmsFeatures().isRequiresDbCatalogName();
            boolean isSchemaByUser = DbmsSpecificFactory.getDbmsFeatures().isSchemaByUser();
            String catalogName = isRequiresCatalog ? connection.getCatalog() : null;
            String schemaName = isSchemaByUser ? dbMetaData.getUserName() : dbProperties.getCurrentSchemaProperty();
            ResultSet tables = dbMetaData.getTables(catalogName, schemaName, "%", null);
            boolean found = false;
            while (tables.next()) {
                String tableName = tables.getString("TABLE_NAME");
                if ("SEC_USER".equalsIgnoreCase(tableName)) {
                    found = true;
                }
            }
            if (!found) {
                result.add(new CheckFailedResult("SEC_USER table not found in main data store - it doesn't look like a CUBA database", null));
            }
        }
    } catch (DataSourceLookupFailureException e) {
        if (Stores.isMain(storeName)) {
            result.add(new CheckFailedResult("Cannot find datasource for main data store", e));
        } else {
            String beanName = AppContext.getProperty("cuba.storeImpl_" + storeName);
            if (beanName == null) {
                result.add(new CheckFailedResult(String.format("Cannot find datasource for '%s' data store", storeName), null));
            }
        }
    } catch (Throwable e) {
        result.add(new CheckFailedResult(String.format("Exception occurred while connecting to data store %s", storeName), e));
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
            // Do nothing
            }
        }
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) DataSourceLookupFailureException(org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DbProperties(com.haulmont.cuba.core.sys.dbupdate.DbProperties) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 2 with DataSourceLookupFailureException

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

Aggregations

DataSourceLookupFailureException (org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException)2 DbProperties (com.haulmont.cuba.core.sys.dbupdate.DbProperties)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 SneakyThrows (lombok.SneakyThrows)1 lombok.val (lombok.val)1 JndiDataSourceLookup (org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup)1