2014-09-28 45 views
0

我已經驗證可以使用Java測試程序連接到postgres數據庫。我也證實我可以使用一個小的Grails testDB項目進行連接。但是當我嘗試運行我的大型項目時,它與BuildConfig.groovy相同,它失敗了。從Grails連接到Postgres的問題

我試過不同的jdbc版本(例如,4而不是41)在postgres jar中,但它沒有幫助。

我一直在尋找stackoverflow和其他任何我可以找到無濟於事(例如,「幫助我stackoverflow,你我唯一的希望」)。

我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' 
     compile 'org.postgresql:postgresql:9.3-1101-jdbc41' 
     runtime 'org.postgresql:postgresql:9.3-1101-jdbc41' 
     test "org.grails:grails-datastore-test-support:1.0-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.7' 
     compile ":asset-pipeline:1.9.6" 
     compile ":twitter-bootstrap:3.2.1" 
     // compile ":jquery-dialog:2.0.3" 

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

    } 
} 

我DataSource.groovy文件中包含

dataSource { 
    pooled = true 
    jmxExport = true 
    url = "jdbc:postgresql://150.18.178.9:5432/myDB" 
    driverClassName = "org.postgresql.Driver" 
    dbCreate = "update" 
    username = "user" 
    password = "password" 
    dialect = net.sf.hibernate.dialect.PostgreSQLDialect 
} 
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 { // database dev 
      dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', '' 
      url="jdbc:postgresql://150.18.178.9:5432/dev" 
      username = "user" 
      password = "password" 
     } 
    } 
    test { 
     dataSource { 
      dbCreate = "update" 
      url="jdbc:postgresql://150.18.178.9:5432/test" 
      username = "user" 
      password = "password" 
     } 
    } 
    production { 
     dataSource { 
      dbCreate = "update" 
      url="jdbc:postgresql://150.18.178.9:5432/myDB" 
      username = "user" 
      password = "password" 
      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 
      } 
     } 
    } 
} 

當我嘗試運行我的應用程序得到:

/usr/java/latest/bin/java -Dgrails.home=/home/iank/software/grails/latest -Dbase.dir=/home/iank/IdeaProjects/nderground -Dtools.jar=/usr/java/latest/lib/tools.jar -Dgroovy.starter.conf=/home/iank/software/grails/latest/conf/groovy-starter.conf -Xmx768M -Xms768M -XX:MaxPermSize=256m -XX:PermSize=256m -javaagent:/home/iank/software/grails/latest/lib/org.springframework/springloaded/jars/springloaded-1.2.0.RELEASE.jar -noverify -Dspringloaded=profile=grails -Didea.launcher.port=7535 -Didea.launcher.bin.path=/home/iank/software/idea-IU-135.909/bin -Dfile.encoding=UTF-8 -classpath /home/iank/software/grails/latest/lib/org.codehaus.groovy/groovy-all/jars/groovy-all-2.3.6.jar:/home/iank/software/grails/latest/dist/grails-bootstrap-2.4.3.jar:/home/iank/software/idea-IU-135.909/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain org.codehaus.groovy.grails.cli.support.GrailsStarter --main org.codehaus.groovy.grails.cli.GrailsScriptRunner --conf /home/iank/software/grails/latest/conf/groovy-starter.conf "run-app -plain-output" 
|Loading Grails 2.4.3 
|Configuring classpath 
. 
|Environment set to development 
................................. 
|Packaging Grails application 
........... 
|Compiling 1 source files 
............................ 
|Running Grails application 
| Error 2014-09-27 17:16:38,021 [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.IllegalArgumentException: object is not an instance of declaring class 
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 '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.IllegalArgumentException: object is not an instance of declaring class 
    Line | Method 
->> 262 | run  in java.util.concurrent.FutureTask 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor 
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker 
^ 745 | run  in java.lang.Thread 
Caused by 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.IllegalArgumentException: object is not an instance of declaring class 
->> 262 | run  in java.util.concurrent.FutureTask 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor 
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker 
^ 745 | run  in java.lang.Thread 
Caused by BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class 
->> 262 | run  in java.util.concurrent.FutureTask 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor 
| 615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker 
^ 745 | run  in java.lang.Thread 
Caused by IllegalArgumentException: object is not an instance of declaring class 
->> 22 | doCall in nderground.User$__clinit__closure1 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
| 262 | run  in java.util.concurrent.FutureTask 
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor 
| 615 | run  in java.util.concurrent.ThreadPoolExecutor$Worker 
^ 745 | run . . . in java.lang.Thread 
Error | 
Forked Grails VM exited with error 
|Server running. Browse to http://localhost:8080/nderground 

Process finished with exit code 1 

任何幫助將深表感謝。我的進度已經完全停滯,除了嘗試逐個重建項目外,我不知道該做什麼。

非常感謝......

+0

您是否先清理該項目?我的意思是:使用grails clean命令,然後使用grails run-app。 – luisZavaleta 2014-09-28 05:38:22

+0

您可以嘗試從buildconfig中刪除postgres依賴項,並在grails-app/lib文件夾中添加postgres jdbc jar。 – 2014-09-28 07:13:50

+0

爲什麼你認爲連接到postgresql時有問題?日誌中沒有指向postgres的連接問題。然而它確實會說:'無法解析對bean'sessionFactory'的引用 – Eelke 2014-09-28 08:02:35

回答

0

由於@EElke在評論中指出,沒有跡象顯示,在所有的Postgres的問題;你應該看的錯誤說Error creating bean with name 'sessionFactory' ... object is not an instance of declaring class

我的猜測是net.sf.hibernate.dialect.PostgreSQLDialect是問題 - 用org.hibernate.dialect.PostgreSQLDialect替換。

而且,刪除:

runtime 'org.postgresql:postgresql:9.3-1101-jdbc41' 

既然你已經有了

compile 'org.postgresql:postgresql:9.3-1101-jdbc41' 

和運行範圍包括從編譯範圍依賴。

0

謝謝你對我的問題的回覆,Burt。我非常感謝你的時間。我做了這個變化DataSource.groovy的:

dataSource { 
 
    pooled = true 
 
    jmxExport = true 
 
\t url = "jdbc:postgresql://150.18.178.9:5432/myDB" 
 
\t driverClassName = "org.postgresql.Driver" 
 
    dbCreate = "update" 
 
    username = "user" // the real user and password would normally be here 
 
    password = "password" 
 
    // dialect = net.sf.hibernate.dialect.PostgreSQLDialect 
 
\t dialect = org.hibernate.dialect.PostgreSQLDialect 
 
}

不幸的是,問題仍然存在。

只要你不必在黑盒子裏面看,框架就很棒。但在這種情況下,錯誤發生在框架的某個地方,並且正在被遮蔽。

我通過JDBC有很多使用SQL的經驗,在這一點上,我認爲我可以試着通過從我的Grails應用程序中刪除Hibernate來繼續前進。我只有五個左右的表,SQL查詢非常簡單。考慮到我在這方面花費的時間,我可以在相當的時間內重寫Java JDBC中的所有內容。 Hibernate不提供我的優勢。

我喜歡關於自己處理數據庫連接的一件事情,那就是我可以在連接數據庫和SQL時記錄更明顯的錯誤。

[稍後]

我看着擺脫休眠的。我當然可以在沒有Hibernate的情況下構建Grails應用程序。但是,Hibernate基礎結構不僅提供數據訪問和更新。 Grails Hibernate基礎設施還隱藏了關於連接池的所有細節(據我所知,在Tomcat上它使用Tomcat的連接池)。爲了讓它在Grails Hibernate之外工作,你必須實現JNDI連接數據源。這意味着更改Tomcat文件(web.xml和configure.xml)。因此,您不能再創建Grails WAR文件並將其放在Tomcat Web服務器上。所以看起來我會繼續尋找我的問題的根源。當我找到它時我會再次發佈。

1

通過創建一個工作並慢慢添加代碼的Grails項目,我能夠找到問題。我有一個不適當的指定約束。

class User { 
 
\t String handle 
 
    String emailAddr 
 
\t String salt 
 
\t String password 
 
    boolean isAdmin 
 
    
 
    String toString() 
 
    { 
 
     String rslt = "$handle" 
 
     return rslt 
 
    } 
 

 
    static constraints = { 
 
     handle blank : false, nullable : false, unique : true 
 
     emailAddr blank : false, nullable : false, unique : true, email : true 
 
\t \t salt blank: false, nullable: false 
 
\t \t password blank: false, nullable: false 
 
\t \t isAdmin false <<======= This is the problem! 
 
    } 
 
    
 
    static mapping = { 
 
     table 'users' 
 
     handle index : 'handle_Ix' 
 
     emailAddr index: 'email_ix' 
 
\t \t isAdmin defaultValue: false 
 
    } 
 
}

這應該是在靜態映射部分:

static mapping = { 
 
     table 'users' 
 
     handle index : 'handle_Ix' 
 
     emailAddr index: 'email_ix' 
 
\t \t isAdmin defaultValue: false <<== This is the right way to set the default 
 
    }

說得客氣一點,其煩人的錯誤顯示爲一個線程創建錯誤,沒有任何其他錯誤的指示。