Exception in grails dbm-update - NoSuchBeanDefinitionException - grails

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

Related

How to configure quartz plugin in grails 3?

Recently I tried configuring my grails app for use with quartz scheduler. Unfortunately I failed configuring JDBC job store. The quartz plugin seems to ignore quartz.properties file, where table prefix is defined as Z_STAFF_SCHEDULER. Application startup fails with exception:
Caused by: org.springframework.scheduling.SchedulingException: Could
not start Quartz Scheduler; nested exception is
org.quartz.SchedulerConfigException: Failure occured during job
recovery. [See nested exception:
org.quartz.impl.jdbcjobstore.LockException: Failure obtaining db row
lock: Table 'testing.qrtz_locks' doesn't exist [See nested exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table
'testing.qrtz_locks' doesn't exist]]
Here is the relevant code in application.groovy:
quartz {
autoStartup = true
jdbcStore = true
waitForJobsToCompleteOnShutdown = true
exposeSchedulerInRepository = false
props {
scheduler.skipUpdateCheck = true
}
}
environments {
test {
quartz {
jdbcStore = false
autoStartup = false
}
}
}
grails.config.locations = ["classpath:conf/quartz.properties"]
and this is my config in quartz.properties:
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = StaffScheduler
org.quartz.scheduler.instanceId = AUTO
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 25
org.quartz.threadPool.threadPriority = 5
#============================================================================
# Configure JobStore
#============================================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.dataSource = development
org.quartz.jobStore.tablePrefix = Z_STAFF_SCHEDULER_
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.clusterCheckinInterval = 20000
#============================================================================
# Configure Datasources
#============================================================================
org.quartz.dataSource.development.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.development.URL = jdbc:mysql://localhost:3306/testing?useSSL=false
org.quartz.dataSource.development.user = testing
org.quartz.dataSource.development.password = nopass
org.quartz.dataSource.development.maxConnections = 10
org.quartz.dataSource.development.validationQuery = select 1
Anyone out there who can help me please?
I'm using grails 3.2.3 and quartz plugin 2.0.9
I finally found the solution myself. Every option for the quartz plugin can be configured in application.yml itself. See http://www.quartz-scheduler.org/documentation/quartz-2.2.x/configuration/ for a list of supported parameters.
You do not need any further files. Here's an extract from my application.yml as an example:
quartz:
autoStartup: true
jdbcStore: true
scheduler:
instanceName: 'staff_scheduler'
instanceId: 'AUTO'
threadPool:
class: 'org.quartz.simpl.SimpleThreadPool'
threadCount: 25
threadPriority: 5
jobStore:
misfireThreshold: 60000
class: 'org.quartz.impl.jdbcjobstore.JobStoreTX'
driverDelegateClass: 'org.quartz.impl.jdbcjobstore.StdJDBCDelegate'
useProperties: false
dataSource: 'development'
tablePrefix: 'Z_STAFF_SCHEDULER_'
isClustered: true
clusterCheckinInterval: 20000
dataSource:
development:
driver: 'com.mysql.jdbc.Driver'
URL: 'jdbc:mysql://localhost:3306/testing?useSSL=false'
user: 'testing'
password: 'nopass'
maxConnections: 28
validationQuery: 'select 1'

Set datasource 'ALL' for all grails domain mapping GORM

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.

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"
}
}

Configuring DataSource.groovy for Openshift for Grails

I'm trying to deploy a Grails application on Openshift.
I'm deploying the app using a locally built war file.
Right now, I am using the url given by the rhc-app-show command in the DataSource.groovy file for my Database configurations.
Whenever I try with the environment variables of Openshift (eg. $OPENSHIFT_MYSQL_DB_HOST), it crashes. Any idea or pointers on how to use those in the config file?
Thanks.
I haven't used OpenShift but Google led me to the FAQ which shows these environment variables:
OPENSHIFT_MYSQL_DB_HOST
OPENSHIFT_MYSQL_DB_PASSWORD
OPENSHIFT_MYSQL_DB_USERNAME
OPENSHIFT_MYSQL_DB_URL
OPENSHIFT_MYSQL_DB_PORT
so it looks like this would work:
production {
dataSource {
driverClassName = 'com.mysql.jdbc.Driver'
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
String host = System.getenv('OPENSHIFT_MYSQL_DB_HOST')
String port = System.getenv('OPENSHIFT_MYSQL_DB_PORT')
String dbName = System.getenv('OPENSHIFT_APP_NAME')
url = "jdbc:mysql://$host:$port/$dbName"
username = System.getenv('OPENSHIFT_MYSQL_DB_USERNAME')
password = System.getenv('OPENSHIFT_MYSQL_DB_PASSWORD')
properties {
...
}
}
}
The missing bit is the database name - is that something that you would have available? I'm not sure of the format of OPENSHIFT_MYSQL_DB_URL but it looks like you might just be able to use url = "jdbc:${System.getenv('OPENSHIFT_MYSQL_DB_URL')}"
POSTGRES SQL ALSO ...there is a nice , tutorial for that on this link ...but the datasource configuration must be configured like this ....
production {
dataSource {
dbCreate = "update"
driverClassName = "org.postgresql.Driver"
dialect = org.hibernate.dialect.PostgreSQLDialect
uri = new URI(System.getenv('OPENSHIFT_POSTGRESQL_DB_URL'))
url = "jdbc:postgresql://"+uri.host+uri.path+"/"+System.getenv('OPENSHIFT_APP_NAME')
username = System.getenv('OPENSHIFT_POSTGRESQL_DB_USERNAME')
password = System.getenv('OPENSHIFT_POSTGRESQL_DB_PASSWORD')
}
second Alternative . . .
production {
dataSource {
dbCreate = "update"
driverClassName = "org.postgresql.Driver"
dialect = org.hibernate.dialect.PostgreSQLDialect
uri = new URI(System.getenv('OPENSHIFT_POSTGRESQL_DB_URL').toString())
appname = System.getenv('OPENSHIFT_APP_NAME').toString()
url = "jdbc:postgresql://"+uri.host.toString()+uri.path.toString()+"/"+appname
username = System.getenv('OPENSHIFT_POSTGRESQL_DB_USERNAME').toString()
password = System.getenv('OPENSHIFT_POSTGRESQL_DB_PASSWORD').toString()
}
In Grails 3.0.11, File application.yml
System.getenv doesn't work, so i had to configure manually the url connection for database in mysql.... Use the command of documentation https://developers.openshift.com/en/managing-port-forwarding.html
dataSource:
dbCreate: update
url : jdbc:mysql://thisIsTheUrlGetWithPort-ForwardCommandAndThePort:48381/server?verifyServerCertificate=false&autoReconnect=true&useSSL=false&requireSSL=false
driverClassName: com.mysql.jdbc.Driver
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
username : admin9CzsS #this is a username example
password : RR7y9uKw3t #this is a password example
properties:
jmxEnabled: true
initialSize: 5
maxActive: 50
minIdle: 5
maxIdle: 25
maxWait: 10000
maxAge: 600000
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 60000
validationQuery: SELECT 1
validationQueryTimeout: 3
validationInterval: 15000
testOnBorrow: true
testWhileIdle: true
testOnReturn: false
jdbcInterceptors: ConnectionState
defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED

how can i dynamic update Datasource.groovy file

in a grails project, there will a file named DataSource.groovy. Such as follows:
dataSource {
pooled = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
mongodb {
host = 127.0.0.1// adjust this according to your settings
port = 27017
databaseName = 'test'
username = 'user' // database user and password, if server requires authentication
password = 's3cret'
}
My question is that how can I set for example mongodb.host dynamically at run time.
If you have different MongoDB Hosts, you can set up different environments for development, test, and production using the environments closure in your DataSource.groovy.
In your example above, let's say that you are using localhost 127.0.0.1 for development and mongo-prodserver for production
environments {
development {
grails {
mongo {
host = "127.0.0.1"
port = 27017
username = "user"
password= "s3cret"
databaseName = "test"
}
}
}
production {
grails {
mongo {
host = "mongo-prodserver"
port = 27017
username = "user"
password= "s3cret"
databaseName = "prod"
}
}
}
...
}
Here is the link to Grails Doc on DataSources and Environments.

Resources