I am using two datasources in my Grails application. One is my local db and the other is remote db as shown below.
development {
dataSource {
url = "jdbc:mysql://localhost:3306/testax_dev?autoreconnect=true"
properties {
...
}
}
dataSource_phpscheduler {
driverClassName = 'com.mysql.jdbc.Driver'
username = "xyz"
password = "zxyz"
url = "jdbc:mysql://remote-ip:3306/phpscheduler?autoreconnect=true"
}
}
Now I want to handle the exception caused due to the connection problem of remote database so that the application starts successfully.
A Grails application will not start if the dataSource bean(s) can't be created. There is no way to start the application if the connection to the database fails.
The reason for this is that Grails depends on those beans being instanced, and ready.
Update
As pointed out by Burt Beckwith it is possible to do this, but it does require you to understand the lifecycle of Hibernate and your datasource. It's also possible to register your own dataSource which has error handling. As always, we owe much to Burt.
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'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"
}
}
Grails makes it very easy to configure datasources for different environments (development, test, production) in its DataSources.groovy file, but there seems to be no facility for configuring multiple datasources in one environment. What to I do if I need to access several databases from the same Grails application?
Connecting different databases in different domain classes is very easy in Grails 2.x.x.
for example
development {
dataSource {//DEFAULT data source
.
.
}
dataSource_admin { //Convention is dataSource_name
url = "//db url"
driverClassName = "oracle.jdbc.driver.OracleDriver"
username = "test"
password = 'test123'
}
dataSource_users {
}
}
You can use any datasources in your domain classes by
class Role{
static mapping = {
datasource 'users'
}
}
class Product{
static mapping = {
datasource 'admin'
}
}
For more details look at this
If using Grails 2.0 or higher, there is no need for the plugin, it is supported natively.
http://www.grails.org/doc/latest/guide/single.html#multipleDatasources
There is now Grails plugin that enables the use of multiple datasources directly with Grails' GORM layer:
http://burtbeckwith.com/blog/?p=70
Grails 2.0 can handle multiple data sources without a plugin:
Example with a different datasource for the dev(h2 dataSource) and test(mysql dataSource_mysql) environments:
DataSource.groovy:
dataSource {
pooled = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
dataSource_mysql {
dialect = org.hibernate.dialect.MySQLInnoDBDialect
driverClassName = 'com.mysql.jdbc.Driver'
username = "user"
password = "pass"
url = "jdbc:mysql://mysqldb.com/DBNAME"
}
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 {
development {
dataSource {
configClass = HibernateFilterDomainConfiguration.class
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:file:../devDb;MVCC=TRUE"
sqlLogging = true
}
}
test {
dataSource_mysql {
configClass = HibernateFilterDomainConfiguration.class
dbCreate = "create" // one of 'create', 'create-drop', 'update', 'validate', ''
sqlLogging = true
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
pooled = true
properties {
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
validationQuery="SELECT 1"
}
}
}
}
Do you really want to do this? In my experience, the usual scenario here is:
An application manages its own data in its own database schema
Often, the application will require data from other sources (for example, so reference data doesn't get copied and pasted)
I've normally always had the luxury of all the schemas residing on the one database instance. Therefore, my application:
only has one database connection - which is to the schema it owns and has read/write access
the other applications 'export' their data via views
my application has read access to those views, and has a synonym for that view making it appear local
The reason behind using views is so that the application that is exposing the data
knows explicitly that it is being exported and what is being exported
does not expose the internal structure of the schema (so if the internal structure changes, as long as the view is correct the consuming apps don't know)
I haven't actually had to do this with a Grails application, but the approach should work.
Another approach to sharing data across applications is to create a web service to expose the data. Grails makes this easy.
Hope that helps, but this approach may not be applicable for all situations.
The following post seems to be the best source of information on the subject:
How to get mutli-dataSource in grails
It boils down to:
Define datasource1 in DevelopmentDataSource
Define datasource2 in resources.xml
Write a DAO for CRUD of the domain objects using datasource2
In hibernate.cfg.xml, list all domain objects.
Only the first datasource will have dynamic finder methods.
If its a really simple query you are after and don't mind not having the ORM features you could use Groovy SQL or the native SQL features of Hibernate.