Grails database is not persist after server restart,in my data source file i have following code.
please check where is i am missing.
EDIT:
dataSource {
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
username = "admin"
password = "admin"
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://localhost/mydb?useUnicode=yes&characterEncoding=UTF- 8&zeroDateTimeBehavior=convertToNull"
pooled = true
properties {
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
validationQuery="SELECT 1"
}
}
hibernate {
show_sql = false
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://localhost/mydb?useUnicode=yes&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"
pooled = true
properties {
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
validationQuery="SELECT 1"
}
}
hibernate {
show_sql = false
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://localhost/mydb?useUnicode=yes&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"
pooled = true
properties {
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
validationQuery="SELECT 1"
}
}
}
}
but when i restart server then hole database is drop.any suggestion please.
Thanks in advance.
Related
i am trying to create mysql with some default configurations on azure with terraform, following is my code. although "enforce_gtid_consistency" and "time_zone" is working and being created, but "gtid_mode" "ON" isn't working I am getting following error.
resource "azurerm_mysql_server" "main" {
name = var.mysql_server_name != "" ? var.mysql_server_name : "mysql-01-${var.instancesuffix}"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
administrator_login = var.mysql_administrator_login
administrator_login_password = data.mykv.mysql.value
sku_name = var.mysql_sku_name
storage_mb = var.mysql_storage_mb
version = var.mysql_version
auto_grow_enabled = true
backup_retention_days = 7
geo_redundant_backup_enabled = false
infrastructure_encryption_enabled = true
public_network_access_enabled = true
ssl_enforcement_enabled = true
ssl_minimal_tls_version_enforced = "TLS1_2"
lifecycle {
ignore_changes = [tags]
}
threat_detection_policy {
disabled_alerts = []
email_account_admins = false
email_addresses = []
enabled = true
retention_days = 0
}
}
resource "azurerm_mysql_configuration" "time_zone" {
name = "time_zone"
resource_group_name = azurerm_resource_group.main.name
server_name = azurerm_mysql_server.main.name
value = "+00:00"
}
resource "azurerm_mysql_configuration" "enforce_gtid_consistency" {
name = "enforce_gtid_consistency"
resource_group_name = azurerm_resource_group.main.name
server_name = azurerm_mysql_server.main.name
value = "ON"
}
resource "azurerm_mysql_configuration" "gtid_mode" {
name = "gtid_mode"
resource_group_name = azurerm_resource_group.main.name
server_name = azurerm_mysql_server.main.name
value = "ON"
}
Error: waiting for creation of Configuration: (Name "gtid_mode" / Server Name "mysql-01" / Resource Group "myrg-01"): Code="InternalServerError" Message="An unexpected error occured while processing the request. Tracking ID: 'h59fr7f-18uo-90db-tb20-5y65d34btb04'"
on resources.tf line 164, in resource "azurerm_mysql_configuration" "gtid_mode":
164: resource "azurerm_mysql_configuration" "gtid_mode" {
As I have mentioned in comment , The gtid_mode can only be enabled in sequence , directly turning ON will not work evenif the dependency is set with enforce_gtid_consistency.
So, as a solution you have to set it up in sequence:
OFF_PERMISSIVE
ON_PERMISSIVE
ON
I tested with your code doing some changes as below:
main.tf
provider "azurerm"{
features{}
}
data "azurerm_resource_group" "main"{
name = "resourcegroup"
}
data "azurerm_key_vault" "kv"{
name = "ansumantestkv1234"
resource_group_name = "resourcegroup"
}
data "azurerm_key_vault_secret" "name" {
name = "mysqlpassword"
key_vault_id = data.azurerm_key_vault.kv.id
}
resource "azurerm_mysql_server" "main" {
name = var.mysql_server_name
location = data.azurerm_resource_group.main.location
resource_group_name = data.azurerm_resource_group.main.name
administrator_login = var.mysql_administrator_login
administrator_login_password = data.azurerm_key_vault_secret.name.value
sku_name = var.mysql_sku_name
storage_mb = var.mysql_storage_mb
version = var.mysql_version
auto_grow_enabled = true
backup_retention_days = 7
geo_redundant_backup_enabled = false
infrastructure_encryption_enabled = true
public_network_access_enabled = true
ssl_enforcement_enabled = true
ssl_minimal_tls_version_enforced = "TLS1_2"
threat_detection_policy {
disabled_alerts = []
email_account_admins = false
email_addresses = []
enabled = true
retention_days = 0
}
}
resource "azurerm_mysql_configuration" "time_zone" {
name = "time_zone"
resource_group_name = data.azurerm_resource_group.main.name
server_name = azurerm_mysql_server.main.name
value = "+00:00"
}
resource "azurerm_mysql_configuration" "enforce_gtid_consistency" {
name = "enforce_gtid_consistency"
resource_group_name = data.azurerm_resource_group.main.name
server_name = azurerm_mysql_server.main.name
value = "ON"
depends_on = [
azurerm_mysql_configuration.time_zone
]
}
resource "azurerm_mysql_configuration" "gtid_mode_OFF_permissive" {
name = "gtid_mode"
resource_group_name = data.azurerm_resource_group.main.name
server_name = azurerm_mysql_server.main.name
value = "OFF_PERMISSIVE"
depends_on = [
azurerm_mysql_configuration.enforce_gtid_consistency,
]
}
resource "azurerm_mysql_configuration" "gtid_mode_ON_Permissive" {
name = "gtid_mode"
resource_group_name = data.azurerm_resource_group.main.name
server_name = azurerm_mysql_server.main.name
value = "ON_PERMISSIVE"
depends_on = [
azurerm_mysql_configuration.gtid_mode_OFF_permissive
]
}
resource "azurerm_mysql_configuration" "gtid_mode_ON" {
name = "gtid_mode"
resource_group_name = data.azurerm_resource_group.main.name
server_name = azurerm_mysql_server.main.name
value = "ON"
depends_on = [
azurerm_mysql_configuration.gtid_mode_ON_Permissive
]
}
variable.tf:
variable "mysql_server_name" {
default = "ansumanmysqlserver"
}
variable "mysql_administrator_login" {
default = "ansuman"
}
variable "mysql_sku_name" {
default = "GP_Gen5_2"
}
variable "mysql_storage_mb" {
default = "5120"
}
variable "mysql_version" {
default = "5.7"
}
Outputs:
Reference:
MySQL :: MySQL 8.0 Reference Manual :: 17.1.4.2 Enabling GTID Transactions Online
I'm using eclipse and Grails 2.5.1 with MySQL DB with driver version 5.1.36 and it is working fine in the Run mode , but when i try to use the debug mode i get the below error :
context.GrailsContextLoaderListener , Error initializing the
application: No suitable driver found for
jdbc:mysql://127.0.0.1/MY_Dev
here is the BuildConfig :
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.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'
test "org.grails:grails-datastore-test-support:1.0.2-grails-2.4"
runtime 'mysql:mysql-connector-java:5.1.36'
}
plugins {
// plugins for the build system only
build ":tomcat:7.0.55.3" // or ":tomcat:8.0.22"
// plugins for the compile step
compile ":scaffolding:2.1.2"
compile ':cache:1.1.8'
// asset-pipeline 2.0+ requires Java 7, use version 1.9.x with Java 6
compile ":asset-pipeline:2.2.3"
// plugins needed at runtime but not for compilation
runtime ":hibernate4:4.3.10" // or ":hibernate:3.6.10.18"
runtime ":database-migration:1.4.0"
runtime ":jquery:1.11.1"
//security plugin
compile ':spring-security-core:2.0-RC5'
compile ":spring-security-ui:1.0-RC2"
compile ":mail:1.0.7"
compile ":jquery-ui:1.10.4"
compile ":famfamfam:1.0.1"
//internal mails plugin
compile ":grails-direct-messages:1.0"
// Uncomment these to enable additional asset-pipeline capabilities
//compile ":sass-asset-pipeline:1.9.0"
//compile ":less-asset-pipeline:1.10.0"
//compile ":coffee-asset-pipeline:1.8.0"
//compile ":handlebars-asset-pipeline:1.3.0.3"
}
}
and here is the DataSource:
dataSource {
pooled = true
jmxExport = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
// cache.region.factory_class = 'org.hibernate.cache.SingletonEhCacheRegionFactory' // Hibernate 3
cache.region.factory_class = 'org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory' // Hibernate 4
singleSession = true // configure OSIV singleSession mode
flush.mode = 'manual' // OSIV session flush mode outside of transactional context
}
// 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"
pooled = true
dbCreate = "create-drop"//create-drop
driverClassName = "com.mysql.jdbc.Driver"
username = "***"
password = "***"
url="jdbc:mysql://127.0.0.1/MY_Dev"
}
}
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
}
}
}
}
any recommendations to solve this ?
I am Grails newbie. After doing some basic configuration in Datasource.groovy, my grails app fails to start. I get the below error
Error 2015-07-03 15:27:19,014 [localhost-startStop-1] ERROR pool.ConnectionPool - Unable to create initial connections of pool.
Message: Driver:org.h2.Driver#78f95cf6 returned null for URL:jdbc:mysql://localhost:3306/radb?createDatabaseIfNotExist=true
| Error 2015-07-03 15:27:19,854 [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 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException
The database radb exists. I have verified it
My Datasource.groovy file for reference as below
dataSource {
pooled = true
jmxExport = true
driverClassName = "org.h2.Driver"
dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
//username = "sa"
//password = ""
}
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
flush.mode = 'manual' // OSIV session flush mode outside of transactional context
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:mysql://localhost:3306/radb?createDatabaseIfNotExist=true"
username="root"
password="root"
logSql = true
}
}
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
}
}
}
}
your driverClassName is wrong. use the correct one for mysql (in the environments, e.g. com.mysql.jdbc.Driver), where you need mysql. right now you have:
default: driver=h2, dialect=mysql;
dev: url=mysql
prod: url=h2
you definetly want to clean this up. also make sure, you have the driver in your deps.
i'm trying to connect grails to mysql but i get this error:
| Loading Grails 2.4.4 | Error There was an error loading the
BuildConfig: Bad artifact coordinates mys
ql:mysql-connector-java-5.1.23-bin, expected format is
<groupId>:<artifactId>[:< extension>[:<classifier>]]:<version(Use
--stacktrace to see the full trace)
| Configuring classpath | Error Resolve error obtaining dependencies:
Could not find artifact mysql:mysq l-connector-java:jar:5.1.23-bin in
grailsCentral (htts://repo.grails.org/grails /plugins) (Use
--stacktrace to see the full trace) | Error Resolve error obtaining dependencies: Could not find artifact mysql:mysq
l-connector-java:jar:5.1.23-bin in grailsCentral
(htts://repo.grails.org/grails /plugins) (Use --stacktrace to see the
full trace) | Error Could not find artifact
mysql:mysql-connector-java:jar:5.1.23-bin in gra ilsCentral
(htts://repo.grails.org/grails/plugins) | Run 'grails
dependency-report' for further information.
how can i fix this error ?
here is my buidconfig:
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 "htt://repository.codehaus.org"
//mavenRepo "htt://download.java.net/maven/2/"
//mavenRepo "htt://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.23-bin'
// runtime 'org.postgresql:postgresql:9.3-1101-jdbc41'
test "org.grails:grails-datastore-test-support:1.0.2-grails-2.4"
}
plugins {
// plugins for the build system only
build ":tomcat:7.0.55"
// plugins for the compile step
compile ":scaffolding:2.1.2"
compile ':cache:1.1.8'
compile ":asset-pipeline:1.9.9"
// plugins needed at runtime but not for compilation
runtime ":hibernate4:4.3.6.1" // or ":hibernate:3.6.10.18"
runtime ":database-migration:1.4.0"
runtime ":jquery:1.11.1"
// Uncomment these to enable additional asset-pipeline capabilities
//compile ":sass-asset-pipeline:1.9.0"
//compile ":less-asset-pipeline:1.10.0"
//compile ":coffee-asset-pipeline:1.8.0"
//compile ":handlebars-asset-pipeline:1.3.0.3"
}
}
and datasource:
dataSource {
pooled = true
jmxExport = true
driverClassName = "com.mysql.jdbc.Driver"
dialect = "org.hibernate.dialect.MySQL5Dialect"
username = "root"
password = "thanh" } 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
flush.mode = 'manual' // OSIV session flush mode outside of transactional context }
// environment specific settings
environments {
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:mysql:/localhost:3306/bkavr"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:mysql:/localhost:3306/bkav"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:mysql:/localhost:3306/bkav"
properties {
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
}
}
}
}
Change runtime 'mysql:mysql-connector-java:5.1.23-bin'
to
runtime 'mysql:mysql-connector-java:5.1.34'
That should pull in the dependency correctly.
I am new to grails and intellij ide. I have run a grails project in intellij that's ok. But now I need to use mysql database instead of default. I have no idea how to do it. As much as I know i have changed my datasource class like below >>>
dataSource {
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
properties {
initialSize = 5
maxActive = 10
minIdle = 5
maxIdle = 5
maxWait = 100000
validationQuery = "select 1"
minEvictableIdleTimeMillis = 60000
timeBetweenEvictionRunsMillis = 60000
}
}
hibernate {
cache.use_second_level_cache = false
cache.use_query_cache = false
cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop','update'
loggingSql = true
url = "jdbc:mysql://localhost/sbicloud_jan_13"
username = "root"
password = "admin"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://192.168.18.12/sbicloud"
username = "zia"
password = "zia123321"
}
}
production {
dataSource {
}
}
}
when I run the app some errors occur in console and the first is as below >>>
Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Can anyone please help me on this?! It will be so helpful for me.
There are more or less 2 approaches:
Modify your BuildConfig.groovy so your application will use MySQL. In the BuildConfig there is a block called 'dependencies'
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
// runtime 'mysql:mysql-connector-java:5.1.20'
}
Uncomment the line:
// runtime 'mysql:mysql-connector-java:5.1.20'
And it will download the dependencies.
Put MySQL JDBC driver inside the grails-app/lib directory...
I posted a blog post sometime ago with instruction for this. Look here