1

除了默认数据源之外,我无法将域类对象保存到 dbs 中。应该说我可以从日志数据库中读取,但不能保存(UserLog.list() 有效)。当我运行应用程序时,保存 UserLog 对象会触发以下错误。

数据源.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"
    }
}

日志.用户日志:

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
4

1 回答 1

6

@dmahapatro 建议的示例:

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

这将为该数据源创建一个事务(如果您还不了解)

于 2013-05-20T20:32:21.993 回答