2011-09-15 30 views
0

請注意,我使用Grails 2.0.0里程碑2在生產或測試中運行時,Grails不使用我的數據源?

,我發現了Hibernate的錯誤createQuery is not valid without active transaction,當我嘗試WAR /部署我的Grails應用程序或運行使用prod run-app/test run-app應用。 如果我只使用普通的run-app,一切都按預期工作。

我在想,prod run-appwar之間可能會有什麼不同,這會導致我的數據源無法正確連接?

這裏是我的DataSource.groovy文件:

dataSource { 
    dbCreate = "none" 
    url = "jdbc:mysql://something/mydb" 
    pooled = true 
    dialect = org.hibernate.dialect.MySQLDialect 
    username = "xxxxxx" 
    password = "xxxxxxxxx" 
    driverClassName = "com.mysql.jdbc.Driver" 
} 

hibernate { 
    config.location = "classpath:some/hibernate/file.cfg.xml" 
} 


而且,我有一個像這樣一個服務:

package org.dostuff 

import org.dostuff.DaoFactory; 
import org.springframework.transaction.annotation.Transactional; 

class StuffService { 

    static transactional = true; 

    @Transactional(readOnly = true) 
    def getSomething() { 
     def daoFactory = new DaoFactory(); 
     def stuff = daoFactory.getSomeDao().getSomething(); 

     return stuff; 
    } 
} 

注意,我注入休眠SessionFactory靜態到我DaoFactoryBootStrap.groovy文件。

我還能做什麼錯?謝謝!

回答

0

我想通了......

正如你可以在我的問題看,我是用我的加載hibernate的配置文件如下:

hibernate { 
    config.location = "classpath:some/hibernate/file.cfg.xml" 
} 

在我file.cfg.xml,我是定義幾個屬性...其中之一是current_session_context_class

<property name="current_session_context_class">thread</property> 

它t當我在做prod run-apptest run-app時,呃,當我在配置文件中使用Grails的時候,當使用run-app時,它並不是出於某種原因。

因此,如果遇到此問題,請確保您的休眠配置文件沒有可能會影響Grails管理Hibernate會話的設置!

1

我看到配置教程的確說 「前面的示例配置假設您需要對所有環境使用相同的配置:生產,測試,開發等。」 但是,爲什麼不嘗試在你的datasource.grrovy中配置如下的環境!

environments { 
    development { 
     dataSource { 
      dbCreate = "create-drop" // one of 'create', 'create-drop','update' 
      url = "jdbc:hsqldb:mem:devDB" 
     } 
    } 
    test { 
     dataSource { 
      dbCreate = "update" 
      url = "jdbc:hsqldb:mem:testDb" 
     } 
    } 
    production { 
     dataSource { 
      dbCreate = "update" 
      url = "jdbc:hsqldb:file:prodDb;shutdown=true" 
     } 
    } 
} 
+0

謝謝,我也試過,還沒有運氣。它看起來像是在生產/測試/戰爭中忽略我的dataSource定義:( – Polaris878

+1

你可以嘗試用腳手架控制器創建一個簡單的Domain類,看看它是否有效?這樣你可以找出問題是否存在datasource.groovy還是它在服務本身 – Sap

相關問題