Set datasource 'ALL' for all grails domain mapping GORM - grails

I want to add other datasource to use with GORM.
DataSource.groovy
development {
dataSource {
dbCreate = "update"
driverClassName = "org.postgresql.Driver"
username = "usrcastor"
url = "jdbc:postgresql://localhost:5432/dbcastor_master"
password = "castor"
dialect = net.sf.hibernate.dialect.PostgreSQLDialect
pooled = true
loggingSql = false
}
dataSource_otherDataSource {
dbCreate = "update"
driverClassName = "org.postgresql.Driver"
username = "usrcastor"
url = "jdbc:postgresql://localhost:5432/dbcastor_child_a"
password = "castor"
dialect = net.sf.hibernate.dialect.PostgreSQLDialect
pooled = true
loggingSql = false
}
}
But already have a lot of domain classes and I no have time to define static mapping in each one.
Domain.groovy
static mapping {
datasource 'ALL'
}
Is posible to configurate grails to set datasource 'ALL' for all domains?

You can add something like this to grails-app/conf/application.groovy
grails.gorm.default.mapping = {
datasource 'ALL'
}
See the documentation for Grails mappings.

Related

Grails error: table or view does not exist

When ever I start my Grails application, I get these errors for all my domain classes.
ERROR hbm2ddl.SchemaExport - HHH000389: Unsuccessful: drop table domain_class cascade constraints
ERROR hbm2ddl.SchemaExport -ORA-01031: insufficient privileges
and then
ERROR hbm2ddl.SchemaExport -ORA-00942: table or view does not exist
I am using in-memory database for the application and my DataSource.groovy has this in it:
dataSource {
pooled = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
local {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
development {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
Is there something wrong with the settings in DataSource?
I start my application with grails -Dgrails.env=local run-app -https
I tried to create objects during startup using BootStrap.groovy and even they fail. I use GGTS for development. What privileges is this talking about?
We found the answer. The dataSource in file instanceConfig.local.properties in the resources folder was pointing to an Oracle database and I am not configured to a role to create or drop tables there. Hence the insufficient privileges error.
Even though the DataSource.groovy had the in-memory database setting, I assume that the instanceConfig.local.properties overrides it. Thanks anyways for the help guys!

Exception in grails dbm-update - NoSuchBeanDefinitionException

When I run "grails dbm-update --dataSource=production" I get the following exception:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory_production' is defined
at grails.plugin.databasemigration.MigrationUtils.findSessionFactory(MigrationUtils.groovy:142)
at grails.plugin.databasemigration.MigrationUtils.getDatabase(MigrationUtils.groovy:105)
at _DatabaseMigrationCommon_groovy$_run_closure2_closure11.doCall(_DatabaseMigrationCommon_groovy:52)
at grails.plugin.databasemigration.MigrationUtils.executeInSession(MigrationUtils.groovy:133)
at _DatabaseMigrationCommon_groovy$_run_closure2.doCall(_DatabaseMigrationCommon_groovy:51)
at DbmUpdate$_run_closure1.doCall(DbmUpdate:25)
It works on the default dataSource (if I run "grails dbm-update"), but doesn't work on production or on my custom data source.
I use Grails 2.4.3 and database-migration:1.4.0.
I'm running it on Amazon AWS - RDS MySql DB.
Here's my dataSource:
production {
grails.dbconsole.enabled = true
dataSource {
grails.dbconsole.enabled = true
username = "myusername"
password = "mypassword"
pooled = true
dbCreate = "none"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://mydatabase.us-west-1.rds.amazonaws.com:3306/ebdb?autoReconnect=true" dialect = org.hibernate.dialect.MySQL5InnoDBDialect
properties {
validationQuery = "SELECT 1"
testOnBorrow = true
testOnReturn = true
testWhileIdle = true
timeBetweenEvictionRunsMillis = 1800000
numTestsPerEvictionRun = 3
minEvictableIdleTimeMillis = 1800000
}
}
}
The item you are refering to as production is not a datasource but the config for the regular dataSource in the production environment. So this call should work:
grails prod dbm-update

Defining an alternate connection pool in Grails 2.3.6

I know that, at some point between Grails 1.X and Grails 2.X, the default connection pooling library changed from commons-dbcp to tomcat-dbcp.
Now, I'm trying to configure either BoneCP or HikariCP as the connection pooling library for my Grails application.
However, I see that this answer offers a solution which might only apply to Grails 1.X.
I also found this Gist, but again, I don't know which Grails version it applies to.
So, is it possible to define a custom connection pool inside a Grails 2.3.6 application? Thanks!
UPDATE: OK so you actually need to tell Grails not to pool the datasources, since HikariCP is now taking care of this.
I saw connection weirdness in my apps if I left that switch on. So instead say:
pooled = false
OK yeah, #Joshua Moore is right.
I tried doing it with updated Grails methods and this is the relevant section of my resources.groovy file. As far as I can understand, the configuration values in Datasource.groovy are pulled into resources.groovy at runtime, after the target runtime environment has been identified (development, test or production).
def config = Holders.config
def dataSources = config.findAll {
it.key.toString().contains("dataSource_")
}
dataSources.each { key, value ->
def ds = value
"${key}"(HikariDataSource, { bean ->
def hp = new Properties()
hp.username = ds.username
hp.password = ds.password
hp.connectionTimeout = 6000
hp.maximumPoolSize = 60
hp.jdbcUrl = ds.url
hp.driverClassName = ds.driverClassName
HikariConfig hc = new HikariConfig(hp)
bean.constructorArgs = [hc]
})
}
And this is the relevant section of my DataSource.groovy configuration:
// environment specific settings
environments {
development {
dataSource_myapp1 {
pooled = false
username = "CONFIGURE_ME_EXTERNALLY"
password = "CONFIGURE_ME_EXTERNALLY"
driverClassName = 'oracle.jdbc.OracleDriver'
dialect = 'org.hibernate.dialect.Oracle10gDialect'
url = 'jdbc:oracle:thin:#MYDBHOST1:1521/MYSERVICEID1'
}
dataSource_myApp2 {
pooled = false
username = "CONFIGURE_ME_EXTERNALLY"
password = "CONFIGURE_ME_EXTERNALLY"
driverClassName = 'oracle.jdbc.OracleDriver'
dialect = 'org.hibernate.dialect.Oracle10gDialect'
url = 'jdbc:oracle:thin:#MYDBHOST2:1521/MYSERVICEID2'
}
}
}
In my case, it's pretty much the same for test and production environments. Thanks!

Using H2 database for integration testing in Grails

My understanding is by default Grails using the H2 embedded database for testing.
My Datasource.groovy configures a local Postgres database as a root datasoure
dataSource {
driverClassName = "org.postgresql.Driver"
dialect = org.hibernate.dialect.PostgreSQLDialect
url = ...
pooled = true
...
But I don't want this used in integration testing. I would prefer to use the H2 embedded option. So how do I override this for integration test environment and make it use embedded H2 database?
Thanks
Place the baseline configuration in the dataSource block and override it with an environment block as explained in section 4.2 Environments of the Grails documentation.
dataSource {
pooled = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
environments {
development {
dataSource {
dbCreate = "create-drop"
url = "jdbc:h2:mem:devDb:MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:h2:mem:testDb:MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
production {
dataSource {
driverClassName = "org.postgresql.Driver"
dialect = org.hibernate.dialect.PostgreSQLDialect
dbCreate = ...
url = ...
}
}
}
I have the same question, but until now i didn't find an answer. Perhaps setting the field dialect inside Datasource.groovy like this:
test {
dataSource {
dbCreate = "update"
dialect='org.hibernate.dialect.H2DialectPatch'
url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
}
}

Unable to create an encrypted H2 database for Grails

I have a grails project where I need to create an encrypted H2 database, but I'm not sure how to make it work. Here is what I have in DataSource.groovy:
dataSource {
pooled = true
driverClassName = "org.h2.Driver"
dialect = "org.hibernate.dialect.H2Dialect"
dbCreate = "update" // one of 'create', 'create-drop','update'
url = "jdbc:h2:/opt/viewpoint/data/h2/viewpoint;MODE=MYSQL;CIPHER=AES"
user = "sa"
pwds = "filepwd password"
}
When I run it, I get the following:
Caused by: org.h2.jdbc.JdbcSQLException: Wrong password format, must be: file password <space> user password [90050-117]
at org.h2.message.Message.getSQLException(Message.java:105)
at org.h2.message.Message.getSQLException(Message.java:116)
at org.h2.message.Message.getSQLException(Message.java:75)
at org.h2.message.Message.getSQLException(Message.java:151)
at org.h2.engine.ConnectionInfo.convertPasswords(ConnectionInfo.java:264)
at org.h2.engine.ConnectionInfo.<init>(ConnectionInfo.java:72)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:94)
at org.h2.Driver.connect(Driver.java:58)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at $Proxy46.getMetaData(Unknown Source)
... 23 more
I'm not sure where you got your datasource configuration example from, but you need to use username and password instead of user and pwds:
dataSource {
pooled = true
// ...
username = "sa"
password = "filepwd password"
}

Resources