Print Mysql queries produced by Grails - grails

i want to get hold of Mysql queries produced by grails before or after executing.
trainingList = PrivateTraining.findAll(query,parameter)
i want to print the complete mysql query which is executed by the above statement.
is there anyway to print ?

Set this property in your DataSource.groovy:
hibernate.show_sql=true
Example:
environments {
development {
dataSource {
/* ---------------------- */
hibernate.show_sql = true
/* ---------------------- */
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:mysql://localhost:3306/my-db"
// ...

Add the following to your logging configuration:
debug 'org.hibernate'

Related

In grails 4.0.5, moving datasource definition to runtime.groovy causes DB not to be created during tests

I need to move the configuration of the datasource to runtime.groovy, because that configuration code need to access some of my classes.
In previous versions of grails, this was not an issue. However i find that if I move the environments block, and the default datasource block to runtime.groovy, Hibernate will not create the database, and my functional tests fail, obviously.
This is the configuration that I removed from application.yml:
hibernate:
cache:
queries: false
use_second_level_cache: false
use_query_cache: false
dataSource:
pooled: true
jmxExport: true
driverClassName: org.h2.Driver
username: sa
password: ''
environments:
development:
grails.plugin.console.csrfProtection.enabled: false
dataSource:
dbCreate: create-drop
url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
test:
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
production:
hibernate:
jdbc:
use_get_generated_keys: true
Which in my runtime.groovy looks like this:
hibernate {
cache {
queries = false
use_second_level_cache = false
use_query_cache = false
}
}
dataSource {
pooled = true
jmxExport = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
environments {
development {
dataSource {
dbCreate = "create-drop"
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
}
}
test {
dataSource {
dbCreate: "update"
url: "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
}
}
production {
hibernate {
jdbc {
use_get_generated_keys = true
}
}
dataSource {
//production datasource setup irrelevant
}
}
I tried combinations, keeping some of it in application.yml, some of it in application.groovy, all of it in runtime.groovy. Keeping the test and dev sections in application.yml, and the runtime-specific environments in runtime.groovy, does not work either.
Nothing seems to work.
I can't leave it like this because my production mode needs to be able to connect a secrets store to set up the production datasource.
Is there some hidden switch I am missing with the newer versions of grails?
Additional notes, the following commands do work (server starts up):
./grailsw -Dgrails.env=dev run-app
./grailsw -Dgrails.env=test run-app
./gradlew shell -q
The tests fail complaining about missing tables when i run this:
./grailsw -Dgrails.env=test test-app
The tests that are failing are inheriting from GebSpec, as they are full functional tests that need to run against the controllers.
So it seems like Hibernate is not properly initializing when running functional tests, if I move the configuration away from application.yml?
Thanks
FINAL UPDATE
There is a typo in my code, as Jeff pointed it out. Thanks Jeff!
The config you show for your runtime.groovy should be in grails-app/conf/application.groovy (does not need to be runtime.groovy). The problem with your config is you have this:
test {
dataSource {
dbCreate: "update"
url: "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
}
}
You have colons where you should have equals signs, like this:
test {
dataSource {
dbCreate = "update"
url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
}
}

Table "XXX" not found in Grails

Trying to import an old grails project, there seems to be a problem with the mappings. Everything loads perfectly until...
Server running. Browse to http://localhost:8080/prmptvServer
Configuring Spring Security Core ...
... finished configuring Spring Security Core
| Error 2015-11-30 09:06:43,636 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table bank_account drop constraint FK_ss4uej5gx2a07srb540l15s21 if exists
| Error 2015-11-30 09:06:43,638 [localhost-startStop-1] ERROR hbm2ddl.SchemaExport - Table "BANK_ACCOUNT" not found; SQL statement: alter table bank_account drop constraint FK_ss4uej5gx2a07srb540l15s21 if exists [42102-176]
And the rest of domains can't load either. As far as I understand, if my DataSource contains the
dbCreate="create-drop"
, the DB should be rebuilt each time I restart the app, doesn't it? At least that's what I remember. So if it can't find the table, hasn't it been created? If it wasn't created when it should have been, shouldn't I get another error like "couldn't create table" ?
Bootstrap.groovy is all commented to do the debug easier.
DataSource.groovy
dataSource {
pooled = true
jmxExport = 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' // Hibernate 3
cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
singleSession = true // configure OSIV singleSession mode
flush.mode = 'manual' // OSIV session flush mode outside of transactional context
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
}
}...
BankAccount.groovy
class BankAccount {
SecUser user
String alias
String number
static constraints = {
alias nullable: false, blank: false
number nullable: false, blank: false
}
}
I believe this is not a critical problem. I get the same errors in my projects, but the app still working fine.
I believe there is some inconsistency in this configuration. Is not about you, but about grails.
Maybe this post answer your question -> here

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!

Grails 3.0.1 - how and where to configure grails.gorm.default.mapping

I had in the old Config.groovy:
grails.gorm.default.mapping = {
id generator = 'identity'
// send only the dirty fields to the database for updating
dynamicUpdate = true
dynamicInsert = true
}
So I put this in the additionally application.groovy, but it won't be respected any more. All updates are full, sending all fields to the database, even the not changed ones.
I tried to translate this in application.yml:
grails:
gorm:
default:
mapping:
id generator: "identity"
dynamicUpdate: true
dynamicInsert: true
... but still without luck.
With Grails 3.1.10, it works in application.groovy:
dataSource {
//....
}
grails.gorm.default.mapping = {
uuid index:'idx_uuid', type: org.hibernate.type.UUIDCharType, length: 36, defaultValue: null
batchSize 15000
}
but had no sucess either while putting it in application.yml

Render from controller is working fine in development but not in test

I have the following code in my grails app:
def list () {
def roles = principal.authorities*.authority
def page = roles.contains("ROLE_ADMIN")? "allcolors": "usercolors"
if (params.sort == "latest" || params.sort == null) {
logger.debug("came in if");
render view: page, model: [colorlist: colorService.colorList()]
}
else
render view: page, model: [colorlist: colorService.colorListForUser()]
}
When I run my application with grails run-app the above code works fine. However, when I deploy the war file created by grails test war target/myapp.war the above code does not work and errors with "Page not found" even though the debug statement came in if still gets printed.
I've tried to run this app in development with grails test run-app as well but even then the above does not work. Interestingly, when I run the app in prod mode (grails prod run-app) everything works fine as well. So it is certainly something to do with the test environment
Also, to ensure there aren't any data discrepancies I've changed dev test and prod to point at the same development database.
Could it be that my app has some special setting for test environment that I'm failing to see ...which would cause "render" to not work?
My environment looks like this:
environments {
development {
grails.logging.jul.usebridge = true
}
test {
grails.logging.jul.usebridge = true
}
production {
grails.logging.jul.usebridge = false
}
}
And DB config looks like this:
environments {
development {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:mysql://localhost/myapp?useUnicode=yes&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8"
username = "root"
password = ""
}
hibernate {
}
}
test {
dataSource {
dbCreate =
url = "jdbc:mysql://localhost/myapp?useUnicode=yes&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8"
username = "root"
password = ""
properties {
}
hibernate {
}
}
}
production {
dataSource {
dbCreate = "create-drop"
url = "jdbc:mysql://localhost/myapp?useUnicode=yes&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8"
username = "root"
password = ""
}
}
}
How can I resolve this or troubleshoot it further??
run-app runs with the default "development" data source, which is not to be confused with the "test" environment. Check your Config.groovy (edit: and DataSource.groovy) and make sure you have test { } configured within your environments.
More info can be found in the grails documentation here:
http://www.grails.org/Environments

Resources