Repository navigation
Description
With grails-data-hibernate7, an application without a dataSource block in its configuration gets no dataSource
bean, and no DataSource bean at all. GORM still creates and uses a default datasource (H2,
jdbc:h2:mem:grailsDB;LOCK_TIMEOUT=10000, from DataSourceSettings' defaults), and domain classes work, but the
datasource is only reachable through hibernateDatastore.connectionSources.defaultConnectionSource.dataSource.
Adding any dataSource block makes the bean appear, even one that only repeats a default:
dataSource:
dbCreate: noneSo whether the default datasource is exposed as a bean depends on whether the config has a dataSource block, not on
its contents.
Related: #14356 (registering the dataSource bean early enough for Spring Boot auto-configuration).
Cause
grails.orm.bootstrap.HibernateDatastoreSpringInitializer#configureDataSources (grails-data-hibernate7) collects the
datasource names from the config, and only adds the default when the dataSource map is non-empty:
if (config == null) {
dataSourceNames = [defaultDataSourceBeanName] as Set
}
else {
Map dataSources = config.getProperty(DATA_SOURCES, Map, Collections.emptyMap())
if (dataSources != null && !dataSources.isEmpty()) {
dataSourceNames.addAll(AbstractConnectionSources.toValidConnectionSourceNames(dataSources))
}
Map dataSource = (Map) config.getProperty(DEFAULT_DATA_SOURCE_NAME, Map, Collections.emptyMap())
if (dataSource != null && !dataSource.isEmpty()) {
dataSourceNames.add(ConnectionSource.DEFAULT)
}
}HibernateDatastoreConnectionSourcesRegistrar then registers a dataSource bean only for the names in that set, so
with no dataSource config the set is empty and no bean is registered. The initializer itself skips the default
(if (dataSourceName == ConnectionSource.DEFAULT) continue), and DataSourceGrailsPlugin (grails-datasource)
doesn't register one either when the Hibernate plugin is present (if (pluginManager.hasGrailsPlugin('hibernate'))).
The config == null branch shows the default is meant to be included. A config without a dataSource block falls
through to the else branch and loses it, although HibernateDatastore creates the default connection source
regardless.
Expected Behavior
The default datasource that GORM creates and uses is always available as the dataSource bean (and as a DataSource
for by-type injection), whether or not the application configures a dataSource block. That's consistent with how a
dataSource block that only repeats defaults behaves.
Actual Behaviour
With no dataSource configuration, applicationContext.getBeansOfType(DataSource) is empty and
getBean('dataSource') fails with NoSuchBeanDefinitionException: No bean named 'dataSource' available. Anything
that injects the datasource by name or type cannot be created, although the application has a working default
datasource.
Steps To Reproduce
- Create a Grails 8.0.0-RC2 application with
grails-data-hibernate7, H2 and HikariCP, a domain class, and no
dataSourceblock inapplication.yml. - In
BootStrap, printgrailsApplication.mainContext.getBeansOfType(javax.sql.DataSource).keySet():[]. - Print
hibernateDatastore.connectionSources.defaultConnectionSource.dataSource: a workingHikariDataSourcefor
jdbc:h2:mem:grailsDB;LOCK_TIMEOUT=10000. - Add
dataSource: { dbCreate: none }(equal to the default) toapplication.ymland repeat:[dataSource], backed
by the sameHikariDataSource.
Suggested Fix
Always include the default connection source name in configureDataSources, as the config == null branch already
does, since HibernateDatastore always creates the default connection source. For example, start with
dataSourceNames = [ConnectionSource.DEFAULT] as Set in both branches. If the absence of the bean is intended,
document that applications must configure a dataSource block to get it.
Environment Information
- Grails 8.0.0-RC2 (
grails-data-hibernate7,grails-datasource8.0.0-RC2) - H2 2.4.240, HikariCP 7.0.2
- JDK 21
Example Application
No response
Version
8.0.0-RC2
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
Register the default dataSource bean without dataSource configuration