Grails pogo method calls within a transaction - grails

This may be a dumb question.
I'm on a time constraint so I don't have much time to play with it, but if I call a pogo object's method residing in src/groovy from a grails service and it performs JDBC database work, will all of it be included within the current transaction? Or should I make these pogos into services?
Can't find the answer directly anywhere and don't have much time to experiment.
Thanks!

Once you're within a transaction, any GORM methods you call (directly or indirectly) from there will execute within that transaction unless the code makes a special effort to do something different (e.g. using its own withTransaction {} block).

Related

How to reduce CreateTable transaction when use Azure function binding?

Currently, my project is using Azure function 2.x.
I'm binding TableStorage to CloudTable. Function is triggered by Eventhub message
However, there are many CreateTable transactions. See picture below:
This transaction seem happens for every function invoke.
Check the source of in Github, that call the method: CreateIfNotExists() when binding, so I guess it can be the root cause of this behavior above. It is strange, too much CreateTable transactions does not happen in Azure function 1.x
Is there anyway to reduce CreateTable transaction?
May I need to use custom binding?
Thanks in advance.
There are many CreateTable transactions.
The reason why have so many CreateTable transactions is because the underlying code is trying to create.
Is there anyway to reduce CreateTable transaction? May I need to use
custom binding?
I am not sure what language you are using now, if you try to use C# IBinder, maybe you will still run the create step. You can create your own logic in the body of function, I think this can perfectly avoid this problem. Just try to get it instead of create it.

Groovy/Grails, thread safety and closures

I'm investigating what looks like a race condition in my Grails application. I occasionally see what I think can only be the result of 2 different threads interfering with one another when the application is under a lot of load.
The offending code builds a query using a closure (which is defined at class-level, like a method) which dynamically adds query parameters based on the properties of the domain class. On the surface the code looks fine (to me, as a Java developer), however I stumbled across this comment regarding controller scope in the Grails docs (emphasis mine):
prototype (default) - A new controller will be created for each request (recommended for actions as Closure properties)
session - One controller is created for the scope of a user session
singleton - Only one instance of the controller ever exists (recommended for actions as methods)
Reference
So my question is: what are the implications of using a closure (instead of a method) in a service which is a singleton?
EDIT:
The code is part of the grails quick search plugin:
https://github.com/tadodotcom/grails-quick-search/blob/master/grails-app/services/org/grails/plugins/quickSearch/QuickSearchService.groovy
There are 2 closures, aliasBuilder and propertyQueryBuilder which I think may not be thread-safe.
Should you still need a solution, I just happened to fork that plugin and I stumbled upon the same problem (in the same concurrent scenario).
The long story short, helped by this post, I applied the #Synchronized annotation to the search method of QuickSearchService service and the exception (NPE btw) went away.
HTH

Adding AJAX mechanism on the Groovy controller?

I have a scenario where I use JS(KNOCKOUT) to call Groovy controller and that indeed invokes a service call to the backend. The groovy controller calls about 4 different services when this process is invoked from frontend. Groovy controller currently works in the order how its been coded and waits for each services to complete its task before the other, which is correct. I need to re-factor code so that the service calls should run parallel(not wait for one another, kind of AJAX nature). I understand I can use Threads to achieve it. Is there any other library or Groovy functionality that I can make use of?
I cannot use groovy.Async because we use grails 2.2.2 in our project. Any help is greatly appreciated.
Take a look at GPARS. It's got a very good reputation as a parallelism API for Groovy.

Shouldn't Grails GORM calls be in the service and not controller layer?

I'm trying to decide, according to Grails best practices and MVC patterns, when is the correct time to introduce a Service and not keep fattening controllers. I find it somewhat conflicting, what I've read about best practices and what appears to be common practice, so would love to hear what others think about this.
With GORM calls in mind, I would have expected that anything to do with GORM should really go into a service. Although I don't practice this myself, especially when writing very basic controller methods like show that simply perform a get() on a domain class and then render a view to show the details of the retrieved object.
However, after following books like 'Clean Code' and similar books, well maintained code should be cohesive and methods should ideally perform a single task only. So in the perfect world, would the show method in a controller be responsible only for determining the object to display before rendering a view? The retrieval from the database could go into a method in the database that's sole task is to retrieve from the DB and throw an exception if not found etc.
But yes, this does somewhat seem like overkill.
So taking the step a bit further, the create() or update() methods. Again currently Grails' generated code puts everything into the controller, no use of a service at all.
So when would the recommended point be that we need to use a Service? Is it only when something transactional has to take place, for instance, on a create() call, we might also want to write a record to a Log file to keep an audit log of sorts. Surely this warrants a service?
I'd love to hear what others feel is the correct time to introduce services, I imagine it differs greatly person to person.
I recommend you this post: http://www.infoq.com/articles/grails-best-practices
We are creating static methods in Domain class to encapsulate queries. Service are used only for transactional operations or very complex queries with multiple domains interaction. Controllers simply call domains or services methods.

Mixing Transaction Script pattern with DDD/CQRS

Here is the situation, in order to support our legacy system, we need to insert to a table whenever a user logs in. This is basically an CRUD operation, so it doesn't really make sense to create repository/entity/command/event for this since this doesn't tie to any business rules at all. The only benefit to create a CQRS command is that this database write can happen asynchronously under that model. Which is a better route to take?
Use CQRS, and then call a stored proc.
when handling that command?
Just call database directly in the controller (I am using asp.net mvc)
If you are using (and persisting) events for possible playback, then it makes sense to do the writing to a legacy DB as part of an event handler (think "gateway"). If you need to replay this event in the future you can swap in a fake handler that doesn't reinsert the record.
Your controller should really only be a translation layer between and HTTP request and a command for your domain. Writing to a DB (even legacy, non-domain access) doesn't really make sense there, IMHO. Putting the logic in an event handler makes the interaction very explicit.

Resources