在NPE域的beforeInsert方法中的第二个DataStore中创建表

时间:2015-05-15 19:08:47

标签: grails transactions gorm

我使用运行时数据源插件来获得第二个数据源,并通过一个名为TableDefinition的域将表信息保存在主数据源中,在运行时创建动态表,但在保存此域实例之前,我想在第二个创建实际表数据源,但它失败了NullPointerException,而实际的表是在第二个数据库中创建的,但域实例没有保留。

第二次数据源服务执行

class DataConnectionService {

  def runtimeDataSource 
  static transactional = true

  def execute( String query, String dsName )
  {
    try
    {
        Sql sql =  runtimeDataSource.getSql( dsName )
        return sql.execute( query )
    }
    catch ( Exception e )
    {
        log.error( "Error in executing query $query [$e.message]", e )
        return null
    }
    finally
    {
        sql.close()
    }
  }
}

域的beforeInsert方法

def beforeInsert()
{
    try {
        String q = "Create Table test ...."
        dataConnectionService.execute( q, dsName )
    }
    catch( e )
    {
        log.error "Creating table in 2nd db failed [$e.message]"
        return false
    }
}

异常堆栈如下:

->>  163 | removeBatchLoadableEntityKey    in org.hibernate.engine.spi.BatchFetchQueue
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    388 | addEntity                       in org.hibernate.engine.internal.StatefulPersistenceContext
|    461 | addEntity . . . . . . . . . . . in     ''
|    143 | makeEntityManaged               in org.hibernate.action.internal.AbstractEntityInsertAction
|    203 | addResolvedEntityInsertAction . in org.hibernate.engine.spi.ActionQueue
|    181 | addInsertAction                 in     ''
|    216 | addAction . . . . . . . . . . . in     ''
|    324 | addInsertAction                 in org.hibernate.event.internal.AbstractSaveEventListener
|    288 | performSaveOrReplicate . . . .  in     ''
|    194 | performSave                     in     ''
|    125 | saveWithGeneratedId . . . . . . in     ''
|    209 | saveWithGeneratedOrRequestedId  in org.hibernate.event.internal.DefaultSaveOrUpdateEventListener
|    194 | entityIsTransient . . . . . . . in     ''
|    114 | performSaveOrUpdate             in     ''
|     90 | onSaveOrUpdate . . . . . . . .  in     ''
|    684 | fireSaveOrUpdate                in org.hibernate.internal.SessionImpl
|    676 | saveOrUpdate . . . . . . . . .  in     ''
|    671 | saveOrUpdate                    in     ''
|     58 | doInHibernate . . . . . . . . . in org.codehaus.groovy.grails.orm.hibernate.metaclass.SavePersistentMethod$1
|    188 | doExecute                       in org.codehaus.groovy.grails.orm.hibernate.GrailsHibernateTemplate
|    132 | execute . . . . . . . . . . . . in     ''
|     56 | performSave                     in org.codehaus.groovy.grails.orm.hibernate.metaclass.SavePersistentMethod
|    215 | doInvokeInternal . . . . . . .  in org.codehaus.groovy.grails.orm.hibernate.metaclass.AbstractSavePersistentMethod
|     69 | invoke                          in org.codehaus.groovy.grails.orm.hibernate.metaclass.AbstractDynamicPersistentMethod

1 个答案:

答案 0 :(得分:0)

在强制执行有关事务和查询的不同选项之后,以下组合允许成功执行查询并保存域实例:

static transactional = false

sql.executeUpdate( query )

如果任何人能够提供有关此行为的理由或任何更好的优化替代方案,那将会很棒。