Entradas

Mostrando las entradas de 2014

Grails: Fixing timeout exceptions and hibernate AssertionFailure when execute a SQL query

Some times, for a particular reason you need to perform a SQL query instead of execute a Grails/GORM operation . In these cases you can do the job writing a method in your domain models, or in your service classes like the following example: class MyService { def sessionFactory int myCustomOperation() { def session = sessionFactory.openSession() def sql = session.createSQLQuery( "SELECT field1 FROM ...MY CRAZY QUERY ... LIMIT 1") sql.uniqueResult().intValue() } } As you can seen, a new database session will open each time the function will call, so if the method is massively called by the application, it's possible that after the 30 th or more calls, a timeout exception will rise, because the BBDD engine doesn't have more connections to give to our application. A possible solution can be replace the openSession() call by a getCurrentSession() call, I have tried it, but the result is a new exception given the concurrent operations performed: hibe