简体   繁体   中英

multiple times triggering of Datasource.groovy in grails application ( throwing exception when called second time)

Work Around for creating the application :

  1. creating a Grails Application using Postgres database.
  2. Needs to create Database when the application is executed (ie database is to be created from the project itself instead of creating the database manually).

For creating the database I am calling a groovy service CreateDatabaseService in datasource.groovy as follows :

import demo.grails.CreateDatabaseService
CreateDatabaseService.serviceMethod()
dataSource {
   pooled = true
   jmxExport = true
   driverClassName = "org.postgresql.Driver"
   dialect = "org.hibernate.dialect.PostgreSQLDialect"
   username = "postgres"
   password = "password"
}
hibernate {
   cache.use_second_level_cache = true
   cache.use_query_cache = false
   singleSession = true // configure OSIV singleSession mode
   flush.mode = 'manual' 
}

// environment specific settings
environments {
development {
    dataSource {
        dbCreate = "create-drop" 
        url = "jdbc:postgresql://localhost:5432/SampleAppDb"
    }
}
test {
    dataSource {
        dbCreate = "update"
        url = "jdbc:postgresql://localhost:5432/SampleAppDb"
    }
} .....

Full code for CreateDatabaseService :

package demo.grails
import groovy.sql.Sql
import java.sql.DriverManager
import java.sql.Connection
import java.sql.SQLException
import java.sql.*

class CreateDatabaseService {

public static flag =0

def static serviceMethod() {

    Connection conn = null;
    Statement stmt = null;
        try{
         if(flag == 0)  {
               //STEP 2: Register JDBC driver
               Class.forName("org.postgresql.Driver");
               //STEP 3: Open a connection
               System.out.println("Connecting to database...");
               conn =     DriverManager.getConnection("jdbc:postgresql://localhost:5432", "postgres", "password");
               //STEP 4: Execute a query
               System.out.println("Creating database...");
               stmt = conn.createStatement();
               String isDatabase = "select datname from pg_catalog.pg_database where lower(datname) = lower('SampleAppDb')"
               ResultSet rs = stmt.executeQuery(isDatabase);
               String idr
               while(rs.next()){
                   //Retrieve by column name
                   idr  = rs.getString("datname");
               }
                 if(!idr.equals("SampleAppDb")) {
                   String sql = "create database SampleAppDb";
                   stmt.executeUpdate(sql);
                   System.out.println("Database created successfully...");
                   flag =1
                 }
                 else {
                    flag =1
                 }
          }
         }catch(Exception e){
            e.printStackTrace();

        }
        finally{
             stmt.close();
            conn.close();
         }
    }

}

On running the grails app following is the full stack trace that shows datasource.groovy is called multiple times, though the database is created from the application but complains about no suitable driver found when datasource.groovy is called second time.

|Loading Grails 2.4.5
|Configuring classpath
.
|Environment set to development
.................................
|Packaging Grails application
................Connecting to database...
Creating database...
Database created successfully...
....................
|Running Grails application
Connecting to database...
Error |
java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432
Error |
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
Error |
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
Error |
    at java_sql_DriverManager$getConnection.call(Unknown Source)
Error |
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
Error |
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
Error |
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
Error |
    at demo.grails.CreateDatabaseService.serviceMethod(CreateDatabaseService.groovy:23)
Error |
    at demo.grails.CreateDatabaseService$serviceMethod.call(Unknown Source)
Error |.......

Please suggest how to overcome this error.

BuildConfig.groovy

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'
    runtime 'postgresql:postgresql:9.0-801.jdbc4'
    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.2" // or ":tomcat:8.0.20"

    // plugins for the compile step
    compile ":scaffolding:2.1.2"
    compile ':cache:1.1.8'
    compile ":asset-pipeline:2.1.5"

    // plugins needed at runtime but not for compilation
    runtime ":hibernate4:4.3.8.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"
}
}

Validate that you have the following configuration in your BuildConfig

dependencies {
    ...
    compile 'org.grails.plugins:hibernate:4.3.10.4'
    provided 'org.postgresql:postgresql:9.4-1203-jdbc4'
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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