Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Grails from opening a connection to the database in a Controller method

I have a service that is communicating to another machine. Since it's a simple Controller method Grails automatically grabs a DB connection from the pool while my controller is communicating with the other server. I'd like to prevent it from doing that, and manually open up the database connection when I'm ready so that it doesn't suck up a connection during a long period like doing network calls. How do I prevent Grails from automatically grabbing a connection from the pool in a controller method?

like image 677
chubbsondubs Avatar asked Jun 27 '14 14:06

chubbsondubs


1 Answers

When you create a controller it has the Transactional annotation on it, something like:

 @Transactional(readOnly=true)
 class FooController { ..

If you remove that annotation (and any method level annotations) then Grails will no longer connect to the database to start the transaction.

Open Session In View should not come into play since we use a lazy init approach for obtaining the connection with OSIV

Note my answer above assumes you are using a recent version of Grails (2.3.x or above)

Updated

For MongoDB you can disable automatically connection for all controllers by defining the following bean (which overrides the default) in grails-app/conf/spring/resources.groovy:

 mongoPersistenceInterceptor(org.codehaus.groovy.grails.support.NullPersistentCon‌​textInterceptor) 

However there is no way to disable on a per controller basis at the moment

like image 67
Graeme Rocher Avatar answered Oct 12 '22 21:10

Graeme Rocher