I'm using Grails 1.3.7 and the db-migration plug-in.
I have generated a chagelog.groovy-file containing my delta, I set theese properties:
grails.plugin.databasemigration.updateOnStart = true
grails.plugin.databasemigration.updateOnStartFileNames = ['changelog.groovy‘]
Now in my Datasource.groovy I have the the dbCreate to update.
I start my application and it tells me that the table I have in my delta is already created.
Any ideas on this?
You don't need to set any dbCreate option in your DataSource.groovy.
The migration plugin manages all necessary operations if you specified your delta correctly.
Example part of your DataSource.groovy:
production {
dataSource {
dbCreate = ""
url = "yourDBUrl"
username = "yourUser"
password = "yourPassword"
}
}
Related
I'm new to Grails and I want to integrate it with my existing google cloud sql (Grails 2.x). What are the files i need to alter and are there any tutorials already ?
I was not able to find any. Appreciate your help in advance.
you just need to add your jdbc jar in your lib folder, and then configure the data source in your DataSource.groovy file:
for example:
development {
dataSource {
dbCreate = "create"
pooled = false // true doesn't work with Google SQL
driverClassName = "com.google.cloud.sql.Driver" //<------THIS
dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"//<------THIS
username = 'root'
password = ''
url = 'jdbc:google:rdbms://yourinstancename/yourdatabasename'//<------THIS
}
It seems that Grails is trying to access my database when I first deploy it to my production tomcat server. I know this because I get the following error message in the stacktrace.log
invalid username/password; login denied
Now, I disabled database creation
dataSource {
pooled = false
driverClassName = "oracle.jdbc.OracleDriver"
dialect = org.hibernate.dialect.Oracle10gDialect
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
//Set jdbc metadata to false to not open a session
temp.use_jdbc_metadata_defaults = false
}
production {
dataSource {
dbCreate = "none"
url = "jdbc:oracle:thin:#1.1.1.1:1521:xe"
}
}
I don't supply the database password because we use database users for authentication and authorization (please don't criticize this decision, I know it's awful, but we have a legacy database). So the username/password is supplied when the user makes a request through the client. We used http://sergiosmind.wordpress.com/2013/03/14/grails-using-a-database-user-for-security-login/ to set this up.
It seems that the Grails app cannot start because of this. Why is Grails accessing the database? What is it trying to do?
Grails uses connections at startup to initialize GORM - there's one to detect the dialect, one to configure the LOB handler, and Hibernate connects to initialize its configuration also.
I discuss this in these two blog posts: http://burtbeckwith.com/blog/?p=312 and http://burtbeckwith.com/blog/?p=1565
I am trying to create a new domain object in the grails console with the help of this guide.
According to the console output the new object is created:
grails> shell
groovy:000> new foo.Book(title: 'bar').save(failOnError: true, flush: true)
groovy:000> foo.Book : 1
groovy:000> foo.Book.list()
groovy:000> [foo.Book : 1]
But this new book entity is not visible in the dbconsole
The table BOOK is present when I connect with the JDBC url for the dev environment as found in DataSource.groovy:
jdbc:h2:mem:devDb;MVCC=TRUE
username: sa
password: <blank>
but a select returns 0 rows
The relevant piece of DataSource.groovy config (the default)
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' // Hibernate 3
// cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
}
// 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"
}
}
When the entity is created using the console, rather than the groovy shell, the issue remains.
I am using the newest grails build at this moment, which is 2.3.1
The embedded H2 database vrsion = H2 1.3.173 (2013-07-28)
I think the problem is that the database is getting locked. Let's try this one then (works on my experiment):
edit your grails-app/conf/spring/resources.groovy and make it looking like this:
// Place your Spring DSL code here
beans = {
h2Server(org.h2.tools.Server, "-tcp,-tcpPort,8043") { bean ->
bean.factoryMethod = "createTcpServer"
bean.initMethod = "start"
bean.destroyMethod = "stop"
}
}
Then, modify your grails-app/conf/DataSource.groovy to look like this:
test {
dataSource {
dbCreate = "update"
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
Now, you are ready to add some new objects as per the tutorial:
$ grails
grails> run-app
grails> shell
groovy:000> new test.Book(title: 'Book 1').save(failOnError: true)
===> test.Book : 1
groovy:000> new test.Book(title: 'Book 2').save(failOnError: true)
===> test.Book : 2
groovy:000> test.Book.list()
===> [test.Book : 1, test.Book : 2]
To view the H2 console, go to
http://localhost:8080/{project}/dbconsole
but select [Generic H2 Server] from the list and on the JDBC URL enter:
jdbc:h2:tcp://localhost:8043/mem:devDb
and connect. I hope that helps
======================
After a bit further experimentation, it appears that locking was your problem and you need to use a mixed mode approach when connecting to your H2. You can read more information here:
http://www.h2database.com/html/features.html#auto_mixed_mode
So, the simplest thing to do is use this jdbc connection URL:
url = "jdbc:h2:/tmp/myDB;MVCC=TRUE;LOCK_TIMEOUT=10000;AUTO_SERVER=TRUE"
for both your application and the H2 dbconsole (notice the AUTO_SERVER=TRUE) (no need to modify the spring bean)
I suggest to change the
dbCreate = "create-drop"
to
dbCreate = "update"
on your DataSource.groovy and try again
When I modified the spring bean as Nick suggested, I could not start run-app and at the same time start the grails console or shell. Here is the error I got:
Message: Error creating bean with name 'h2Server': Invocation of init method failed; nested exception is org.h2.jdbc.JdbcSQLException: Exception opening port "8043" (port may be in use), cause: "java.net.BindException: Address already in use" [90061-173]
The simple change to url worked, thanks Nick -:)
I have the following settings on a new grails project:
dataSource {
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
username = "sa"
password = ""
}
environments {
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:mysql://localhost/myapp?useUnicode=yes&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8"
username = "root"
password = ""
}
}
}
when I run my app it fails with an error: Error creating bean with name 'transactionManagerPostProcessor':
This error goes away when I manually go to my database and create a database called myapp
I thought the create-drop setting in dbCreate is suppose to create the db if it does not exist.
Question
How can I configure the settings so that the database gets created when it does not exist in the MySQL
Creating the database itself is impractical because it is very vendor-specific, even more so than the DDL to create tables, sequences, etc. You often need to specify access rules, storage options, etc.
Hibernate will generate and run the schama DDL but you have to start the process by creating the database itself except for simple databases like H2.
I'm following the racetrack example from Jason Rudolph's book at InfoQ, using grails-1.2.1. I got up to the part where I was to switch from hsqldb to mysql. I think I've deleted every reference to hsqldb in the DataSource.groovy file, but I get an exception and the stack trace shows it's still using hsqldb.
DataSource.groovy
dataSource {
boolean pooled = true
String driverClassName = "com.mysql.jdbc.Driver"
String url = "jdbc:mysql://localhost/dfpc2"
String dbCreate = "create"
String username = "dfpc2"
String password = "dfpc2"
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
}
hibernate {
cache.use_second_level_cache=true
cache.use_query_cache=true
cache.provider_class='net.sf.ehcache.hibernate.EhCacheProvider'
}
// environment specific settings
environments {
development {
}
test {
}
production {
}
}
When I grails run-app it all starts up with no errors. I can navigate to the home page. But when I click on one of the links, I get a stack trace:
java.sql.SQLException: Table not found in statement [select this_.id as id0_0_, this_.version as version0_0_, this_.name as name0_0_, this_.variant as variant0_0_ from domainObject this_ limit ?]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
at dfpc2.domainObjectController$_closure2.doCall(script1269434425504953491149.groovy:13)
at dfpc2.domainObjectController$_closure2.doCall(script1269434425504953491149.groovy)
at java.lang.Thread.run(Thread.java:619)
My mysql database shows no tables created. (I don't think groovy's connected to mysql yet.)
Things I've checked:
mysql-connector-java-5.1.6.jar is in lib directory.
I've tried grails clean
I tried putting the dataSource info in the development environment (I haven't graduated to test or prod yet), but it seemed to make no difference. The stdout shows I'm using development env.
I've googled for solutions, but the only solution I've found is when people don't change the test or production environments.
Problem was the type declarations. Instead of
dataSource {
boolean pooled = true
String driverClassName = "com.mysql.jdbc.Driver"
String url = "jdbc:mysql://localhost/dfpc2"
String dbCreate = "create"
String username = "dfpc2"
String password = "dfpc2"
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
}
should have had:
dataSource {
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost/dfpc2"
dbCreate = "create"
username = "dfpc2"
password = "dfpc2"
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
}
Found the answer in the grails doco:
When configuring the DataSource do not include the type or the def keyword before any of the configuration settings as Groovy will treat these as local variable definitions and they will not be processed. For example the following is invalid:
boolean pooled = true
That book is way out of date and InfoQ should pull it or add a link to the 2nd edition which came out recently and is based on Grails 1.2: http://www.infoq.com/minibooks/grails-getting-started