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
}
Related
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'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"
}
}
We are using grails with groovy and recently changed database from MySQL to Oracle 11g. We took care of table names like USER, RESOURCE to make it something else, remapped the new names in the domain classes.
I also added some default data in roles from mysql table(for spring security to work) and inserted one user 'admin' manually in GRAUSER table (renamed from USER).
The server does start up in Netbeans
But when I try to login I get the following error
ERROR util.JDBCExceptionReporter ORA-00904: "THIS_"."password": invalid identifier
Not able to debug the cause of this. Let me know if any more details/code is needed to review, but I need to be able to login to the application.
Could you post your DataSource.groovy file? Below is roughly what mine looks like for connecting to Oracle.
dataSource {
logsql = true
pooled = true
driverClassName = "oracle.jdbc.OracleDriver"
username = "user"
password = "secret"
dialect='org.hibernate.dialect.Oracle10gDialect'
}
environments {
development {
dataSource {
//dbCreate = "create-drop" // one of 'create', 'create-drop','update'
url = "jdbc:oracle:thin:#server:1521:sid"
}
}
}
I would like some help about setting Grails to work with SQL Server Express. I know I have to change the datasource, but I'm not sure exactly what to do, and couldn't find updated information on the web.
You need to change the datasource settings in grails-app/conf/DataSource.groovy
dataSource {
// Change the values of these properties to the username, password and hostname
// for your SQL Server database
username = "sa"
password = ""
url = "jdbc:sqlserver://localhost:1433;databaseName=your_database_name_here"
driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
dialect = "org.hibernate.dialect.SQLServerDialect"
}
You'll need to make sure that the SQL Server JDBC driver is on your classpath either by copying it to the lib directory of the grails application, or configuring it as a runtime dependency in BuildConfig.groovy
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