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.
Related
I am using Grails 2.4.3 and the database-migration:1.4.0 plugin.
I have created a simple Domain class called Mod. I am able to create the groovy based changelog using dbm-generate-gorm-changelog changelog.groovy. This correctly generates the file. I then execute dbm-update which reports:
|Starting dbm-update for database sa # jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000
|Finished dbm-update
However, there are no table created in the database and running dvm-status returns:
|Starting dbm-status for database sa # jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
1 change sets have not been applied to SA#jdbc:h2:mem:devDb
changelog.groovy::1413897188349-1::clarkrichey (generated)
|Finished dbm-status
My development environment configuration from DataSource.groovy is as follows:
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"
}
}
The issue centers around the fact you are using an in-memory instance of h2. Thus, nothing is saved between application launches. If you want a persistent database then change your url to use a file based instance.
development {
dataSource {
dbCreate = "none" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:file:/path/to/save/to/devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
}
}
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'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
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.