I'm trying to have a look at the tables generated in h2 db used in Grails project, but something's amiss.
I connect to the browser console at http://127.0.1.1:8082/ but all that's there to browse is INFORMATION_SCHEMA and Users. How do I get tho the tables used/generated by the app?
Just started building out the app and only few domain classes are in place and I'm trying to get a feel for working h2. Prior to that I've been using PostgreSql in all projects so this is very unnerving for the moment.
Thanks in advance
Are you using the right JDBC URL when logging in?
The default in grails is jdbc:h2:mem:devDB.
When an non-existing URL is given, like jdbc:h2:blabla, an empty database is created, with the default INFORMATION_SCHEMA and Users as you described.
Make sure you connect to the URL where your grails application stores its tables. You can find the URL in $GRAILS_PROJECT/config/DataSource.groovy, after the url definition.
environments {
development {
dataSource {
pooled = false
logSql = false
username = "sa"
password = ""
dialect = "com.hp.opr.hibernate.dialect.H2Dialect"
driverClassName = "org.h2.Driver"
dbCreate = "create-drop"
url = "jdbc:h2:mem:devDB;DB_CLOSE_DELAY=-1;MVCC=TRUE"
}
}
}
If you're using 2.0 the web console is enabled by default in dev mode and can be enabled in other environments: http://grails.org/doc/2.0.0.M2/guide/conf.html#databaseConsole
If you're not using 2.0 yet you can install the http://grails.org/plugin/dbconsole plugin or follow the link to my blog post and set it up yourself if you want to customize the url (or if you're using Grails pre-1.3.6 since the plugin has an artificial version restriction to 1.3.6+)
... so in datasource I changed the url to: url = "jdbc:h2:rswDb" (removing the 'mem' part and changing the name of the db). Then 3 db files showed up in the root dir of the project.
Next, in db console set the jdbc url to: jdbc url: jdbc:h2:~/work/web/rsw/rswDb
... and when I hit 'connect' all the tables were there!
Thanks again!
Related
In ASP.NET MVC 4, to switch out the default session provider for a custom one, you had to let the app know via web.config, which now is gone in ASP.NET 5.
I've tried to use Microsoft.Web.RedisSessionStateProvider (which is based on StackExchange.Redis), but not sure how to proceed beyond getting it via NuGet. It simply doesn't work.
What am I missing?
Following is a Session sample where you can use Redis cache as a store for it:
https://github.com/aspnet/Session/blob/dev/samples/SessionSample/Startup.cs#L32-L39
You can configure Redis cache options like below:
services.Configure<RedisCacheOptions>(redisOptions =>
{
redisOptions.Configuration = "localhost";
redisOptions.InstanceName = "SampleInstance";
}
At one point a couple months ago I had an app (similar to https://github.com/aspnet/Session/blob/dev/samples/SessionSample/Startup.cs) connecting to a remote Redis server like this:
app.UseDistributedSession(new RedisCache(new RedisCacheOptions()
{
Configuration = "ip:port,password=xxx"
}));
It's probably different now in beta7, but hopefully that helps.
My Grails app uses a h2 database in dev mode (the default behaviour for Grails apps). The DB connection settings in DataSource.groovy are
dataSource {
pooled = true
jmxExport = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
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"
}
I'm trying to setup a connection for this database using IntelliJ IDEA's database client tools. I start off creating the connection like so
Then in the following dialog, I enter the JDBC URL
And choose all available databases on the "Schemas & Tables" tab.
The "Test Connection" button indicates success, but as you can see from the red circle, no tables are found. It seems like I've correctly setup a connection to the h2 server, but not the schema itself.
BTW, I try to setup this connection once the app is running, so I'm sure that the schema/tables do actually exist.
Your configuration is for an h2:mem database. Memory Databases have no tables upon connecting to them, and any & all tables are lost when all the connections are closed. Furthermore, a (named) in memory database is unique to the JVM process that opens it. From the H2 documentation:
Sometimes multiple connections to the same in-memory database are required. In this case, the database URL must include a name. Example: jdbc:h2:mem:db1. Accessing the same database using this URL only works within the same virtual machine and class loader environment. (Emphasis added)
This means IDEA will create a unique devDb in its JVM (and classloader) space and your application will create a unique devDb in its JVM (and classloader) space. You can not connect to an in memory database from an external JVM process.
If you want to connect both your application and IntelliJ IDEA (or any other DB tool) to an H2 database at the same time, you will need to either
use an embedded database (that writes to a file) in your application and use Mixed Mode to allow IntelliJ IDEA (and/or other database tools) to connect to it
use a server mode database
See http://www.h2database.com/html/features.html#connection_modes for more information.
This article has a great write up on how to set up the IntelliJ database client to connect to an H2 in-memory database if you happen to be using Spring Boot: http://web.archive.org/web/20160513065923/http://blog.techdev.de/querying-the-embedded-h2-database-of-a-spring-boot-application/
Basically, you wrap the in-memory database with a tcp server, then you have an access point to connect with a sql client via remote access.
Try to open http://localhost:8080/dbconsole and fill your jdbc url
During development you can use grails h2 dbconsole
Let's imagine you've already created entities (Users, Addresses)
Step 1. In application.yml file add H2 properties.
server:
port: 8080
spring:
datasource:
url: jdbc:h2:~/data/parserpalce (for Mac OS)
username: sa
password: password
driver-class-name: org.h2.Driver
jpa:
database-platform: org.hibernate.dialect.H2Dialect
hibernate:
ddl-auto: update
h2:
console:
enabled: true
Step 2. Add H2 Database client
Step 3. Configure H2 database client properties based on your application.yml properties.
Step 4. Run the application.
Step 5. Check if tables(Users, Addresses) are created.
Or you can use H2 console for it in browser:
http://localhost:8080/h2-console
P.S. Do not forget to paste appropriate values in fields!
Does anyone know if it's possible to autogenerate a database on the as400 / iSeries via the domain classes in a grails application as you can do for other DB's ? I can use existing tables via mappings but i'm trying to create a new app and it won't create new tables for me ..
My datasource ..
dbcreate = "create-drop"
username = "user"
password = "password"
driverClassName = "com.ibm.as400.access.AS400JDBCDriver"
url = "jdbc:as400://xxx.xxx.xxx.xxx;naming=system;libraries=,SAMPLEDB;date format=iso;prompt=false;translate binary=true"
dialect = "org.hibernate.dialect.DB2400Dialect"
But this just throws an error basically saying it can't find the files that i'm writing to via bootstrap .. Anyone know if it's possible ? .. Fwiw i'm on v5r4 of the os/400 OS - and yes I know it's old and not supported anymore ..
Thanks
I want to use external configuration in my application. Along with it I use mail plugin and want to store its configuration in external .property file. I've chaged my Config.groovy so now it looks for external configuration under USER_HOME/.grails/APP_NAME-configuration.properties:
environments {
development {
grails.logging.jul.usebridge = true
if (new File("${userHome}/.grails/${appName}-config.properties").exists()) {
println "*** User defined config ***"
grails.config.locations = ["file:${userHome}/.grails/${appName}-config.properties"]
}
}
.....
}
As you can see there is if statement which tests is the configuration file exists. It exists so every time I see "* User defined config *" message when application starts.
I'm running application in development mode. After that configuration of my mail plugin goes:
grails {
mail {
host = grailsApplication.config['mail']['host']
port = grailsApplication.config['mail']['host']
username = grailsApplication.config['mail']['username']
password = grailsApplication.config['mail']['password']
props = ["mail.smtp.auth": "true",
"mail.smtp.socketFactory.port": "465",
"mail.smtp.socketFactory.class": "javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback": "false"]
}
}
When I tried to send a message with email plugin it appeared that configuration of email plugin is not initialized - username, password = null and host, port have default values (localhost and -1). So it seemed to me that config.properties file is simply not loaded. I decided to check if config.properties had been loaded and found out that grailsApplication.config object contained all properties that I'd defined in config.properties: mail.host, mail.port, mail.username, mail.password. I checked if it's possible to get corresponding properties using grailsApplication.config['property']['name'] expression (in debug mode). This expression gave proper values for all mail.host, mail.port, mail.username and mail.password. I also tried to use just grailsApplication.config['property.name'] (with one pair of brackets and '.') but this variant didn't work.
Can someone help me handle this situation? I've spent almost 3 trying different variants and still don't have a solution. Thank you.
You cannot load maps from .properties file. You should use .groovy file instead of .properties.
You can load configuration from external *.groovy file where you can have maps etc. create a file with contents below...
grails {
mail {
host = smtp.gmail.com
port = 465
username = username
password = password
props = ["mail.smtp.auth" : "true",
"mail.smtp.socketFactory.port" : "465",
"mail.smtp.socketFactory.class" : "javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback": "false"]
}
}
And use this in your config.groovy,
grails.config.locations = ["classpath:mail-config.groovy"]
I am using wamp server, it comes with mysql 5.5.16 and I am trying use the mysql from grails 1.3.7. In my Buildconfig.groovy file i have added "runtime 'mysql:mysql-connector-java:5.1.6'" under dependencies and my datasource.groovy looks like this:
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop','update'
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
username = "root"
password = ""
url = "jdbc:mysql://localhost:3306/moviestore_dev"
}
but when i refresh dependencies or run the app, i get error=>
:: mysql#mysql-connector-java;5.1.6: not found
I can connect to the database but the data in the database remains as long as the app is running, the next time I run the app, all the data are gone !
I have no idea what is happening, please help !
Thanks
problem is in
dbCreate = "create-drop"
use
dbCreate = "update"
• create-drop: Drops and re-creates the database schema on each application load
• create: Creates the database on application load
• update: Creates and/or attempts an update to existing tables on application load
• [blank]: Does nothing