Search code examples
mysqlgrails

Configuring the local MySql database connection to grails application


I am a newbie to Grails, I have just started to create my first application just to make CRUD operations by storing the data to my local MySql database and I want to see the entered data in my machine. But I am unable to connect to database itself. Please help me where I am wrong.

My DataSource.groovy code is as shown below:

dataSource {
   pooled = true
   jmxExport = true    
   driverClassName = com.mysql.jdbc.Driver
   dialect = org.hibernate.dialect.MySQL5InnoDBDialect
   username = "test"
   password = "test"
}

environments {
development {
    dataSource {
        dbCreate = "update"
        url = "jdbc:mysql://localhost:3306/librarydb"
    }
}
test {
    dataSource {
        dbCreate = "update"
        url = "jdbc:mysql://localhost:3306/librarydb"
        //url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
    }
}
    production {
    dataSource {
        dbCreate = "update"
        url = "jdbc:mysql://localhost:3306/librarydb"
        //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
         }
       }
     }
    }

And my BuildConfig.groovy file is configured as shown.

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.44'
    // 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.70" // 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.5.7"

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

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

But I am getting the below error in my command prompt.

[localhost-startStop-1] ERROR pool.ConnectionPool  - Unable to create initial connections of pool.
Message: class com.mysql.jdbc.Driver
Line | Method
->>  266 | run       in java.util.concurrent.FutureTask

   1149 | runWorker in java.util.concurrent.ThreadPoolExecutor
   624 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
   748 | run       in java.lang.Thread
   Caused by ClassNotFoundException: class com.mysql.jdbc.Driver
   ->>  381 | findClass in java.net.URLClassLoader

   424 | loadClass in java.lang.ClassLoader
   348 | forName . in java.lang.Class
   266 | run       in java.util.concurrent.FutureTask
   1149 | runWorker in java.util.concurrent.ThreadPoolExecutor
   624 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
   748 | run . . . in java.lang.Thread
   Error |
   2017-11-21 23:22:20,629 [localhost-startStop-1] ERROR 
   pool.ConnectionPool  - Unable to create initial connections of pool.
   Message: class com.mysql.jdbc.Driver
   Line | Method
->>266 | run       in java.util.concurrent.FutureTask

I am using Grails 2.5.6 version, and I have added the mysql-connector-java-5.1.44-bin.jar file in my lib folder of grails application and JAVA_HOME as well.Please help me to solve this and connect to my database to store the data. Thanks in advance.


Solution

  • it worked for me when I removed the jar file from the lib folder of my grails application and used the default mysql connection. i.e username="root" and password="root" in my datasource file. Thanks to @devbd for the answer.