Grails 3:SessionFactory Bean 未注入服务

Grails 3 : SessionFactory Bean is not injecting in Service

我正在尝试从文件中批量添加数据,我从该门户中读取了一种使用批处理批量添加数据的方法

http://krixisolutions.com/bulk-insert-grails-gorm/

当我使用这种技术并根据它更改我的应用程序时,我的代码无法正常工作,我已经工作了好几天来寻找批量保存数据的方法,以通过简单的手动刷新来加快处理速度4分钟存数据库1000行数据,我想尽量缩短这个时间

在下面给出的代码中,每当我调试它在 SessionFactory 之后确实停止的代码时,我不明白问题是什么,因为我对 grails 很陌生,对 sessionFactory 或事务没有任何经验。

这是我的代码:

   runAsync {
            res = benchmark { result ->
                    Session session = SessionFactory.openSession()
Transaction tx = (Transaction)session.beginTransaction()




                    groovyFile.eachLine {


                        String[] tagData = it.split(',')

                        def isTimeToLive = true


                        if (isTimeToLive) {
                            try {
                                caller = new Caller(callingNumber: 
                       tagData.getAt(0), callerName: tagData.getAt(1))
                                session.save(caller)
                            } catch (Exception ex) {
                                log.error(ex.getMessage())
                            }

                            caller.validate()
                            if (caller.hasErrors()) {
                                println("message", "Invalid calling number. Digits can only be from 10 to 15.")
                            }

                            callCallerList = new CallCallerList(caller: caller, callerList: callerList)
                            callCallerList.validate()
                            if (callCallerList.hasErrors()) {
                                println("message", "Invalid calling number. Digits can only be from 10 to 15.")
                            } else {
                                session.save(callCallerList)
                            }

                        }
                        count++;
                        if (count % 100 == 0) {
                            session?.flush()
                            session?.clear()
                        }
                    }
                    tx.commit();
                    session.close();

                }


            }

        }

在 Grails 中,您可以简单地使用 withSession{}withTransaction{}:

我会这样写:

Caller.withTransaction{
  int count = 0
  groovyFile.splitEachLine( ',' ){ String[] line ->
    Caller caller = new Caller( callingNumber:line[ 0 ], callerName:line[ 1 ] )
    if( !caller.save( flush:0 == count % 100 ) ) println caller.errors
    count++
  }
}

您不应该真正手动处理会话。

更新

Why transaction do a rapid insertion while sessions fail?

没有。通常,事务会自动包装会话,因此 withSession{}withTransaction{} 一样快。

Is it because session was not cleaning the cache properly or because each sql query was doing its own flush?

是的,除非您 flushclose 否则会话的缓存不会被清理。这是我的代码或任何关于 GORM 或 Hibernate 批量处理的建议中所做的。

openSession 不是静态方法,您应该注入 sessionFactory 并打开一个会话(如果您还没有的话)。

class SomeService {
  def sessionFactory
  void someMethod() {
    def session = sessionFactory.openSession() //or sessionFactory.currentSession, not sure how this works with async operations
    //all your stuff
    if(<threshold>) { 
       session.flush()
       session.clear() //helps performance on exceptionally large imports, probably not so much on ~1000 records
    }
  }
}