mongodb - 如何使用Grails配置MongoDB

标签 mongodb grails

我是这种方式的新手。我仍然尝试在使用mongodb和grails制作登录应用程序时仍然出错。

grails 2.4.0
mongodb 3.0.3

BuildConfig.groovy。

grails.servlet.version = "3.0" // Change depending on target container  
compliance (2.5 or 3.0)
grails.project.class.dir = "target/classes"
grails.project.test.class.dir = "target/test-classes"
grails.project.test.reports.dir = "target/test-reports"
grails.project.work.dir = "target/work"
grails.project.target.level = 1.6
grails.project.source.level = 1.6
//grails.project.war.file = "target/${appName}-${appVersion}.war"

grails.project.fork = [
    // configure settings for compilation JVM, note that if you alter the   
Groovy version forked compilation is required
    //  compile: [maxMemory: 256, minMemory: 64, debug: false, maxPerm: 256, 
daemon:true],

    // configure settings for the test-app JVM, uses the daemon by default
    test: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, 
daemon:true],
    // configure settings for the run-app JVM
    run: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256,  
forkReserve:false],
    // configure settings for the run-war JVM
    war: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, 
forkReserve:false],
    // configure settings for the Console UI JVM
    console: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256]
   ]
grails.project.dependency.resolver = "maven" // or ivy
grails.project.dependency.resolution = {
// inherit Grails' default dependencies
inherits("global") {
    // specify dependency exclusions here; for example, uncomment this to 
disable ehcache:
    // excludes 'ehcache'
}
log "error" // log level of Ivy resolver, either 'error', 'warn', 'info', 
'debug' or 'verbose'
checksums true // Whether to verify checksums on resolve
legacyResolve false // whether to do a secondary resolve on plugin 
installation, not advised and here for backwards compatibility

repositories {
    inherits true // Whether to inherit repository definitions from plugins

    grailsPlugins()
    grailsHome()
    mavenLocal()
    grailsCentral()
    mavenCentral()
    // uncomment these (or add new ones) to enable remote dependency 
    resolution from public Maven repositories
    //mavenRepo "http://repository.codehaus.org"
    //mavenRepo "http://download.java.net/maven/2/"
    //mavenRepo "http://repository.jboss.com/maven2/"
}

dependencies {
    // specify dependencies here under either 'build', 'compile', 'runtime', 
    'test' or 'provided' scopes e.g.
    // runtime 'mysql:mysql-connector-java:5.1.29'
    // runtime 'org.postgresql:postgresql:9.3-1101-jdbc41'

    compile "org.springframework:spring-orm:$springVersion"
    compile 'org.grails:grails-datastore-gorm:3.1.4.RELEASE'
    compile 'org.grails:grails-datastore-core:3.1.4.RELEASE'
    test 'org.grails:grails-datastore-simple:3.1.4.RELEASE'
}

plugins {
    // plugins for the build system only
    build ":tomcat:7.0.53"

    // plugins for the compile step
    compile ":scaffolding:2.1.0"
    compile ':cache:1.1.6'
    compile ":asset-pipeline:1.8.7"

    // plugins needed at runtime but not for compilation
   // runtime ":hibernate4:4.3.5.3" // or ":hibernate:3.6.10.15"
    runtime ":database-migration:1.4.0"
    runtime ":jquery:1.11.1"

    // Uncomment these to enable additional asset-pipeline capabilities
    //compile ":sass-asset-pipeline:1.7.4"
    //compile ":less-asset-pipeline:1.7.0"
    //compile ":coffee-asset-pipeline:1.7.0"
    //compile ":handlebars-asset-pipeline:1.3.0.3"
    compile ":mongodb:3.0.3"

 }
}

DataSource.groovy
dataSource {
pooled = true
jmxExport = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}

grails {
 mongo {
    host = "localhost"
    port = 27017
    username = "admin"
    password = "admin"
    databaseName = "mydb"
 } 
}
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
singleSession = true // configure OSIV singleSession mode
}

// environment specific settings
environments {
 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"
    }
  }
 test {
    dataSource {
        dbCreate = "update"
        url =   
    "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
    }
  }
  production {
    dataSource {
        dbCreate = "update"
        url = 
        "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
        properties {
           // See http://grails.org/doc/latest/guide/conf.html#dataSource   
             //for documentation
           jmxEnabled = true
           initialSize = 5
           maxActive = 50
           minIdle = 5
           maxIdle = 25
           maxWait = 10000
           maxAge = 10 * 60000
           timeBetweenEvictionRunsMillis = 5000
           minEvictableIdleTimeMillis = 60000
           validationQuery = "SELECT 1"
           validationQueryTimeout = 3
           validationInterval = 15000
           testOnBorrow = true
           testWhileIdle = true
           testOnReturn = false
           jdbcInterceptors = "ConnectionState"
           defaultTransactionIsolation = 
                         java.sql.Connection.TRANSACTION_READ_COMMITTED
        }
      }
   }
}

运行此应用程序时出现错误,我花了更多时间解决这两个问题,但我认为这将是版本兼容性问题。
  Error | 2015-05-09 11:50:58,502 [localhost-startStop-1] ERROR    
  context.GrailsContextLoaderListener  - Error initializing the application:    
  Error creating bean with name 'transactionManagerPostProcessor':   
  Initialization of bean failed; nested exception is 
  org.springframework.beans.factory.BeanCreationException: Error creating 
  bean with name 'transactionManager': Cannot resolve reference to bean 
  'mongoTransactionManager' while setting constructor argument with key [1]; 
  nested exception is 
  org.springframework.beans.factory.BeanCreationException: Error creating 
  bean with name 'mongoTransactionManager': Cannot resolve reference to bean 
  'mongoDatastore' while setting bean property 'datastore'; nested exception 
  is org.springframework.beans.factory.BeanCreationException: Error creating 
  bean with name 'mongoDatastore': Cannot resolve reference to bean   
  'mongoBean' while setting bean property 'mongo'; nested exception is 
  org.springframework.beans.factory.BeanCreationException: Error creating 
  bean with name 'mongo': Invocation of init method failed; nested exception 
  is com.mongodb.MongoTimeoutException: Timed out while waiting for a server 
  that matches AnyServerSelector{} after 10000 ms

 Message: Error creating bean with name 'transactionManagerPostProcessor': 
 Initialization of bean failed; nested exception is 
 org.springframework.beans.factory.BeanCreationException: Error creating 
 bean with name 'transactionManager': Cannot resolve reference to bean 
 'mongoTransactionManager' while setting constructor argument with key [1]; 
 nested exception is 
 org.springframework.beans.factory.BeanCreationException: Error creating 
 bean with name 'mongoTransactionManager': Cannot resolve reference to bean 
 'mongoDatastore' while setting bean property 'datastore'; nested exception 
 is org.springframework.beans.factory.BeanCreationException: Error creating 
 bean with name 'mongoDatastore': Cannot resolve reference to bean 
 'mongoBean' while setting bean property 'mongo'; nested exception is 
 org.springframework.beans.factory.BeanCreationException: Error creating  
 bean with name 'mongo': Invocation of init method failed; nested exception 
 is com.mongodb.MongoTimeoutException: Timed out while waiting for a server 
 that matches AnyServerSelector{} after 10000 ms

最佳答案

好像您的MongoDB服务器未在本地计算机上运行。请确认并启动服务。

对于Ubuntu:

 sudo service mongod start

或对于其他操作系统,请遵循此http://docs.mongodb.org/manual/installation/

关于mongodb - 如何使用Grails配置MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29999633/

相关文章:

用于 Tomcat、Weblogic、Glassfish 的 Grails 3 JNDI 数据源 - Vs Grails 2

grails - 具有显式jruby依赖关系的AsciidoctorJ导致IncompatibleClassChangeError

grails - 为什么在构建 Grails 项目时 @Override 注释不起作用?

grails - 使 Grails 形成开发 DRYer

java - 有关在 GORM 中保存的更多信息

node.js - 在 mongoose (mongodb) 中设置集合属性的到期时间

javascript - 在 typescript 中使用 Mongoose 填充查询

php - 如何将 maxPoolSize 与 mongodb-php 驱动程序版本 1.2.0 一起使用

mysql - 在运行时将 SQL Server 查询转换为 MongoDB 查询

MongoDB 对结构未知的子文档进行文本搜索