0

Here I have one service Called 'DataSaveService' which I used for Saving Objects like..

class DataSaveService {

    static transactional = true

    def saveObject(object)
    {
        if(object != null)
        {
            try
            {
                if(!object.save())
                {
                    println( ' failed to save ! ')
                    System.err.println(object.errors)
                    return false
                }
                else
                {
                    println('saved...')
                    return true
                }
            }
            catch(Exception e)
            {
                System.err.println("Exception :" + e.getMessage())
                return false
            }
        }
        else
        {
            System.err.println("Object " + object + " is null...")
            return false
        }

    }
}

this service is common, and used by many class`s object for storing. when there is multiple request are there at that time is very slow for saving or you can say its bulky. Because of default scope i.e. singleton.

So, I think for reducing work, I am going to make this service as session scope. like..

static scope = 'session'

then after I am accessing this service and method in controller it generated exception.. what to do for session scope service?, any other idea for implementation this scenario......?

Main thing is I want need best performance in cloud. yeah, I need answer for cloud.

sanghavi7
  • 758
  • 1
  • 15
  • 38

1 Answers1

2

Singleton (if it's not marked as synchronized) can be called from different thread at same time, in parallel, w/o performance loss, it's not a bottleneck.

But if you really need thread safety (mean you have some shared state, that should be used inside one method call, or from different parts of application during one http request or even different requests from same user, and you aren't going to run your app in the cloud), then you can use different scopes, like session or request scope. But i'm not sure that it's a good architecture.

For your current example, there are no benefits of using non singleton scope. And also, you must be know that having few instances of same service requires extra mem and cpu resources.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
Igor Artamonov
  • 35,450
  • 10
  • 82
  • 113