简体   繁体   English

从Grails连接到Oracle 12c数据库

[英]Connect to Oracle 12c Database from Grails

How do I configure Grails web app to connect to a simple (one table!) Oracle 12c database? 如何配置Grails Web应用程序以连接到简单的(一个表!)Oracle 12c数据库? 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. 我知道GORM是基于Hibernate的,某种程度上它已经在后台处理了,但是我无法获得一个简单的连接。 I've glanced over the Grails documentation but it seems to favour H2 and MySQL connections, not really Oracle. 我浏览了Grails文档,但它似乎更支持H2和MySQL连接,而不是Oracle。

So I understand that I have to modify DataSource.groovy, to replace the default H2 settings. 因此,我知道我必须修改DataSource.groovy,以替换默认的H2设置。 Below is my attempt at modifying DataSource.groovy for my Oracle 12c database: 以下是我为Oracle 12c数据库修改DataSource.groovy的尝试:

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. 然后,我了解到我可以以某种方式使用“脚手架”或GORM或其他任何方式将域类映射到数据库表...这就是我遇到的问题,或者我没有喝足够的咖啡或者错过了一些东西。

Can anybody help please? 有人可以帮忙吗?

Thanks in advance. 提前致谢。

You are using an older dialect, "org.hibernate.dialect.Oracle10gDialect" is the one you need. 您使用的是较旧的方言,您需要使用“ org.hibernate.dialect.Oracle10gDialect”。 (At least for me connecting to a Oracle11gR2 DB) The dialect you are using is for Oracle9g and before for my experience. (至少对于我来说,连接到Oracle11gR2 DB)您使用的方言是针对Oracle9g的,而对于我的经验,之前是。

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.groovy文件并进行了mwaisgold上面建议的更改之后,我终于得到了以下格式的代码:

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"
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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