Search in sources :

Example 1 with DbProperties

use of com.haulmont.cuba.core.sys.dbupdate.DbProperties 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)

Aggregations

DbProperties (com.haulmont.cuba.core.sys.dbupdate.DbProperties)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 DataSourceLookupFailureException (org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException)1