Grails services are not transactional?

grails, service, transactions

Solution

OK, found the cause, or Gotcha:

"Annotating a service method with Transactional disables the default Grails transactional behavior for that service"

So I happened to annotate one of the many methods in the service as `@Transactional(propagation=Propagation.REQUIRES_NEW)`, thinking that the others will retain their default of transactional, but no, if you make any declarations, it removes the transactoinal behavior of all other methods silently, even if you say "`static transactional = true`"

This appears to be rather dangerous, and from now on, I will annotate every service class with `@Transactional` to avoid being caught out.

Problem

According to the official documentation, and the books I have read, services are transnational be default. however, we were getting records committed, even if we immediately throw a RuntimeException. e.g: ``` class MyService { def someMethod() { new someDomainObject().save(failOnError:true) throw new RuntimeException("rollback!") } } ``` and calling it thusly: ``` class myController{ MyService myService def someMethod() { myService.someMethod() } } ``` In the above case, after calling the controller which calls the service, then checking if the row was created by attaching to the DB using mysql workbench, the row was indeed committed and not rolled back. So we next tried this: ``` class MyService { static transactional = true def someMethod() { new someDomainObject().save(failOnError:true) throw new RuntimeException("rollback!") } } ``` Same problem. Next we tried this: ``` @Transactional class MyService { static transactional = true def someMethod() { new SomeDomainObject().save(failOnError:true) throw new RuntimeException("rollback!") } } ``` Finally, this works. However, we dont understand why. Note: Grails 2.4.4 using MYSQL: ``` development { dataSource { dbCreate = "create-drop" url = "jdbc:mysql://127.0.0.1:3306/db" username = "user" password = "***" } } ``` Is this normal behavior? Is @Transactional different to static tranasctional=true? The Service classes were generated by intellij 14 using the "new groovy class" option from the Services folder in the Grails view. The "new Grails Service" option does not work for us, it just does nothing, so we have to create all groovy classes "by hand" in the right place.

Original source