我们正在研究 grails 应用程序的性能,看来 grails 需要很多时间(7-13 毫秒)才能退出服务返回到 Controller 。返回的数据是对域对象的引用(带有 2 个引用的 Map),不是很复杂。有什么办法可以缩短这个时间吗?
我们在服务中的 return 语句之前有 log.debug() ,在离开服务后,在 Controller 中还有另一个 log.debug() 。
2012-02-01 15:16:07,048 [http-8080-1] DEBUG api.TestService test before service return
2012-02-01 15:16:07,063 [http-8080-1] DEBUG api.TestController test after service return
编辑:Grails 版本 1.3.7
编辑:打开休眠 SQL 日志记录后:
2012-02-02 09:20:04,504 [http-localhost%2F127.0.0.1-8080-1] DEBUG api.TestService before return
2012-02-02 09:20:04,505 [http-localhost%2F127.0.0.1-8080-1] DEBUG hibernate.SQL select nextval ('hibernate_sequence')
2012-02-02 09:20:04,516 [http-localhost%2F127.0.0.1-8080-1] DEBUG hibernate.SQL insert into test ...
2012-02-02 09:20:04,520 [http-localhost%2F127.0.0.1-8080-1] DEBUG hibernate.SQL update test1 ...
2012-02-02 09:20:04,522 [http-localhost%2F127.0.0.1-8080-1] DEBUG hibernate.SQL insert into test_test1 ...
2012-02-02 09:20:04,524 [http-localhost%2F127.0.0.1-8080-1] DEBUG api.TestController after service
最佳答案
开销可能是由 Spring 围绕服务调用构建事务上下文引起的(Grails 默认值,请参阅 http://grails.org/doc/2.0.x/guide/services.html#declarativeTransactions )。 如果您的服务不需要(数据库)事务,请确保添加
static transactional = false
在服务中。
如果您确实需要事务并且正在从 Controller 进行大量服务调用,则值得将它们转移到服务,以便您可以处理最少的事务量。 (如果您绝对想将它们保留在 Controller 中,则可以使用 withTransaction block 在单个事务中执行多个服务调用。)
关于performance - Grails 中从服务返回到 Controller 的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9097542/