【问题标题】:Grails MySQL database reset on each restart of the applicationGrails MySQL 数据库在应用程序每次重新启动时重置
【发布时间】:2014-03-18 16:45:34
【问题描述】:

我是 Grails 的新手。我正面临一个非常恼人的问题。 为什么我的 grails 应用程序会在每次重新启动应用程序时重置所有表数据。例如我有 Post 域类。当我创建一些数据时,它会保存在我的数据库中。但是当我再次重新启动我的应用程序时。所有行都被重置。我不知道为什么会这样。

检查我的 DataSource.groovy ...

dataSource {
  pooled = true
  driverClassName = "com.mysql.jdbc.Driver"
  dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
}
hibernate {
  cache.use_second_level_cache = true
  cache.use_query_cache = true
  cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
   development {
      dataSource {
          dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
          url = "jdbc:mysql://localhost:3306/blog?useUnicode=yes&characterEncoding=UTF-8"
          username = "root"
          password = ""
        }
    }
    test {
        dataSource {
        dbCreate = "update"
        url = "jdbc:mysql://localhost:3306/blog?useUnicode=yes&characterEncoding=UTF-8"
        username = "root"
        password = ""
       }
    }
    production {
        dataSource {
        dbCreate = "update"
        url = "jdbc:mysql://localhost:3306/blog?useUnicode=yes&characterEncoding=UTF-8"
        username = "root"
        password = ""
    }
   }
  }

我正在 localhost:8080(开发环境)上测试我的应用程序。请澄清这个问题。

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    知道了……

    我从这里更改了我的 DataSource.groovy

    development {
      dataSource {
          dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
          url = "jdbc:mysql://localhost:3306/blog?useUnicode=yes&characterEncoding=UTF-8"
          username = "root"
          password = ""
        }
    }
    

     development {
        dataSource {
            dbCreate = "validate" // one of 'create', 'create-drop', 'update', 'validate', ''
            url = "jdbc:mysql://localhost:3306/blog?useUnicode=yes&characterEncoding=UTF-8"
            username = "root"
            password = ""
        }
    }
    

    它对我有用... :)

    【讨论】:

    • 即使在 dbCreate = "update" 的情况下,您的数据库也不应该重置
    • 只是一个建议:请记住,如果您更改任何域类,Grails 可能会显示错误,因为您必须删除并重新创建该表。在这种情况下,暂时将其更改为dbCreate = "create"...
    【解决方案2】:

    我在 DataSource.groovy 中使用以下代码

    dataSource {
      pooled = true
      driverClassName = "com.mysql.jdbc.Driver"
      dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
    }
    hibernate {
      cache.use_second_level_cache = true
      cache.use_query_cache = true
      cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
    }
    // environment specific settings
    environments {
        development {
          dataSource {
            dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
            url = "jdbc:mysql://localhost/blog?autoReconnect=truejdbcCompliantTruncation=false"
            username = "root"
            password = ""
          }
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-03-22
      • 1970-01-01
      • 2020-05-19
      • 2022-11-28
      • 2011-07-19
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 2018-07-15
      相关资源
      最近更新 更多