Oracle – How-to: Grails 3.0.2 + Oracle Database 12c

grailsjdbcoracle

I am making my first steps with Grails and try to create a hello world application with an Oracle Database 12c.

Unfortunately the tutorial documentation on the database specific part at www.grails.org seems not that comprehensive (in contrast for example to that of Ruby on Rails, which I used now several years) – especially when it comes to non-H2 databases.

Is there some recent tutorial on how to get a Grails 3.0.2 app up and running with an Oracle Database 12c?

Mainly I am interested on a) where to put the corresponding JDBC driver and b) how the database configuration should look like.

For a)

grails.org talks about a "lib" directory but does not explain, where it exists or should be created. Other sources say, that the "lib" directory is outdated since several Grails versions and that JDBC drivers should be loaded via dependencies from some repositories – which is obviously not possible with the closed-source Oracle JDBC driver.

For b)

Currently, I have created basic database configuration at <my-app>/grails-app/conf/DataSource.groovy for Oracle 12c:

dataSource {
        url = "jdbc:oracle:thin:@my-server:my-port:my-sid"
        driverClassName = "oracle.jdbc.OracleDriver"
        dialect = "Oracle10gDialect"
        username = "my-user"
        password = "my-pass"
}

Should that be enough for a simple hello world app?

Everything I have found so far at the web is outdated either on the Grails side (covering versions 2.x oder 1.x) and/or the Oracle side (covering versions 10* oder 11*).

Thanks for any hints!


Update (just for the record and to bring together the comments below):

1)

Edit <myapp>/grails-app/conf/application.yml.

hibernate:
    jdbc:
        use_get_generated_keys: true
(...)
dataSource:
    pooled: true
    jmxExport: true
    driverClassName: oracle.jdbc.OracleDriver
    username: <myuser>
    password: <mypassword>
(...)
environments:
    development:
        dataSource:
            dbCreate: create
            url: jdbc:oracle:thin:@<myserver>:<myport>:<mysid>
(...)

2)

Get the corresponding Oracle JDBC driver that fits your JDK and Oracle DB release. In my case (openJDK 7 + Oracle 12c) this was ojdbc7.jar release 12.1.0.2.0 from the Oracle Website: http://www.oracle.com/technetwork/database/features/jdbc/index.html

3)

Place this JDBC driver in <myapp>/lib (create this directory, if it does not yet exists).

4)

Add details of this JDBC driver to the dependencies section of <myapp>/build.gradle (modify if necessary to fit your environment/releases):

(...)
dependencies {
    (...)
    runtime "com.oracle.ojdbc7:12.1.0.2.0" 
    (...)
}
(...)

5)

Create a Maven artifact (modify if necessary to fit your environment/releases):

mvn install:install-file -Dfile=lib/ojdbc7.jar -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=12.1.0.2.0 -Dpackaging=jar -DgeneratePom=true

6)

Add the path to the JDBC driver to your CLASSPATH: export CLASSPATH=/<somewhere>/<myapp>/lib/ojdbc7.jar

7)

Enjoy …

Best Answer

yes this will be OK for meanwhile. But there are details missing. I'll post detailed instructions as soon as i get to the office. Actually there is a sample for mySql somewhere on the grails site, and from it it's pretty clear how to make same for an oracle DB.

I just have created a new app and compared it with myne for oracle 11g. Here it is:

  1. ./build.gradle, add following line to "dependencies" section:

    runtime "com.oracle:ojdbc14:10.2.0.3.0"

  2. ./grails-app/conf/application.yml, add following to "hibernate" section:

jdbc:
    use_get_generated_keys: true
  1. ./grails-app/conf/application.yml, dataSource: section should be like:
dataSource:
    pooled: true
    jmxExport: true
    driverClassName: oracle.jdbc.OracleDriver
    username: YOURUSERNAME
    password: yoursecret
  1. ./grails-app/conf/application.yml, environments: configuration, all three environments should be like:
environments:
development:
    dataSource:
        dbCreate: create
        url: jdbc:oracle:thin:@somehost.net:1521:YOURORAINSTANCE
test:
    dataSource:
        dbCreate: update
        url: jdbc:oracle:thin:@somehost.net:1521:YOURORAINSTANCE
production:
    dataSource:
        dbCreate: update
        url: jdbc:oracle:thin:@somehost.net:1521:YOURORAINSTANCE

And the Hibernate dialect for Oracle you have is pretty old one, I think you'd better remove it, without specifying it explicitly all works fine.