Maciej
Maciej

Reputation: 45

Return from Service to Controller time in Grails

We are working on performance in our grails app, and it seems like grails needs much time (7-13ms) to get out of Service back to Controller. The data that is returned is reference to domain objects (Map with 2 references), not very complex. Is there any way to shorten this time?

We have log.debug() before return statement in service, and another one after leaving service, in controller.

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

Edit: Grails version 1.3.7

Edit: After turning on hibernate SQL logging:

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

Upvotes: 2

Views: 731

Answers (2)

Lari Hotari
Lari Hotari

Reputation: 5310

The overhead might be caused by Spring building a transaction context around the service call (Grails default, see http://grails.org/doc/2.0.x/guide/services.html#declarativeTransactions). If your service doesn't require (database) transactions, make sure you add

static transactional = false 

in the service.

If you do need transactions and you are doing a lot of service calls from a controller, it's worth moving them to a service so that you have the minimum amount of transactions to deal with. (If you absolutely want to keep them in the controller, withTransaction block can be used to do several service calls in a single transaction.)

Upvotes: 2

Dónal
Dónal

Reputation: 187399

7 to 13 milliseconds is pretty much instantaneous. You're wasting your time trying to reduce this further. Surely you must have other more important things to do?

Even if this is your most pressing concern, there seems little point in spending time on it, because there's really nothing you can do about it, because it's Grails/Spring code (rather than yours) than executes between the service and controller.

Upvotes: 2

Related Questions