在grails中使用多个数据库引导数据时没有hibernate会话

在grails中使用多个数据库引导数据时没有hibernate会话,hibernate,grails,gorm,Hibernate,Grails,Gorm,我很难将域类对象保存到dbs中,而不是默认的数据源中。应该说我可以从日志数据库中读取,但无法保存UserLog.list的作品。当我运行应用程序时,保存UserLog对象会触发下面的错误 dataSource.groovy: development { dataSource { dbCreate = "create-drop" url="jdbc:postgresql://localhost:5432/db1" username = "po

我很难将域类对象保存到dbs中,而不是默认的数据源中。应该说我可以从日志数据库中读取,但无法保存UserLog.list的作品。当我运行应用程序时,保存UserLog对象会触发下面的错误

dataSource.groovy:

development {
    dataSource {
        dbCreate = "create-drop"
        url="jdbc:postgresql://localhost:5432/db1"
        username = "postgres"
        password = "postgres"
        driverClassName = "org.postgresql.Driver"
    }
    dataSource_log {
        dbCreate = "update"
        url="jdbc:postgresql://localhost:5432/db2"
        username = "postgres"
        password = "postgres"
        driverClassName = "org.postgresql.Driver"
    }
}
log.UserLog:

class UserLog{
    ...
    static mapping = {
        id generator: "hilo"
        version false
        datasource 'log'
    }
}
conf/bootstrap.groovy:

import groovy.sql.Sql
import happyfloat.Address

import java.text.DateFormat
import java.text.SimpleDateFormat

import log.UserLog

class BootStrap {

    def list = []
    def dataSource_log
    Random rand = new Random()

    def init = { servletContext ->

        Address a1 = new Address() // domain in dataSource [does work!]**
        a1.save()

        UserLog ul = new UserLog() // domain in dataSource_log [ fails! ]**
        ul.save()

    }

    def destroy = {

    }
}
错误:

    | Error 2013-05-20 20:11:48,739 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error initializing the application: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    Message: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    Line | Method
    | Error 2013-05-20 20:11:48,964 [Thread-9] ERROR plugins.DefaultGrailsPlugin  - Error configuration scaffolding: Error creating bean with name 'instanceControllersApi': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
    Message: Error creating bean with name 'instanceControllersApi': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
    Line | Method
    ->> 722 | run in java.lang.Thread

@dmahapatro建议的示例:

UserLog.withTransaction {
    UserLog ul = new UserLog()
    ul.save()
}

如果您尚未理解@dmahapatro建议的示例,这将为该数据源创建一个事务:

UserLog.withTransaction {
    UserLog ul = new UserLog()
    ul.save()
}
如果您还不了解,这将为该数据源创建一个事务

您是否尝试过使用withTransaction-Arounding-UserLog-save?是否尝试过使用withTransaction-Arounding-UserLog-save?