简体   繁体   中英

Connect to Oracle 12c Database from Grails

How do I configure Grails web app to connect to a simple (one table!) Oracle 12c database? I've been through a bunch of tutorials already, and each one is either incomplete or out of date. I need a simple, hand-holding, step-by-step tutorial. I understand that GORM is based on Hibernate, and somehow it's all taken care of under the hood, but I can't get a simple connection working. I've glanced over the Grails documentation but it seems to favour H2 and MySQL connections, not really Oracle.

So I understand that I have to modify DataSource.groovy, to replace the default H2 settings. Below is my attempt at modifying DataSource.groovy for my Oracle 12c database:

dataSource {
    pooled = true
    driverClassName = "oracle.jdbc.driver.OracleDriver"
    username = "scott"
    password = "Sc0ttSc0tt"
    dialect = "org.hibernate.dialect.OracleDialect"
}

hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
    temp.use_jdbc_metadata_defaults = false
}

// environment specific settings
environments {
    development {
        dataSource {
            pooled - true
            dialect = "org.hibernate.dialect.OracleDialect"
            driverClassName = 'oracle.jdbc.OracleDriver'
            username = 'scott'
            password = 'Sc0ttSc0tt'
            url = "jdbc:oracle:thin:@192.168.0.105:1521:orcl"
            dbCreate = "validate" // one of 'create', 'create-drop', 'update', 'validate', ''           
        }
    }

    test {
        dataSource {
            pooled = true
            dialect = "org.hibernate.dialect.OracleDialect"
            driverClassName = 'oracle.jdbc.OracleDriver'
            username = 'scott'
            password = 'Sc0ttSc0tt'
            url = 'jdbc:oracle:thin:@192.168.0.105:1521:orcl'
            dbCreate = 'validate'
        }
    }

    production {
        dataSource {
            pooled = true
            dialect = "org.hibernate.dialect.OracleDialect"
            driverClassName = 'oracle.jdbc.OracleDriver'
            username = 'scott'
            password = 'Sc0ttSc0tt'
            url = 'jdbc:oracle:thin:@192.168.0.105:1521:orcl'
            dbCreate = 'validate'
        }
    }
}

Then I understand that I can somehow use "Scaffolding" or GORM or whatever else to map domain classes to the database table... which is where I'm stuck, and am either not drinking enough coffee or have missed something.

Can anybody help please?

Thanks in advance.

You are using an older dialect, "org.hibernate.dialect.Oracle10gDialect" is the one you need. (At least for me connecting to a Oracle11gR2 DB) The dialect you are using is for Oracle9g and before for my experience.

After playing around with the Datasource.groovy file, and making the changes recommended above by mwaisgold, I finally got the following format to work:

dataSource {
    pooled = true
    driverClassName = "oracle.jdbc.OracleDriver"
    username = "scott"
    password = "Sc0ttSc0tt"
}

hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
    //    cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
    temp.use_jdbc_metadata_defaults = false
}

// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
            dialect = "org.hibernate.dialect.Oracle10gDialect"
            url = "jdbc:oracle:thin:@192.168.0.103:1521:orcl"           
        }
    }

test {
    dataSource {
        dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
        dialect = "org.hibernate.dialect.Oracle10gDialect"
        url = "jdbc:oracle:thin:@192.168.0.103:1521:orcl"               
    }
}

production {
    dataSource {            
        dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
        dialect = "org.hibernate.dialect.Oracle10gDialect"
        url = "jdbc:oracle:thin:@192.168.0.103:1521:orcl"
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM