AmaliaMV opened a new issue, #14544:
URL: https://github.com/apache/grails-core/issues/14544

   I have a Person class which does an evaluation inside of `beforeDelete()` 
method an depending on the result could throw an exception. Something like that:
   
   ```
   class Person {
       String name
       static constraints = {
           name unique: true
       }
       void beforeDelete() {
           throw new RuntimeException('just for test')
       }
   }
   ```
   
   In addition, I have a controller which has Transactional annotation and  
this `delete()` action:
   
   ```
   @Transactional(readOnly = true)
   class PersonController {  // this doesn't work as I expected
   
       //....
       @Transactional
       def delete(Long id) {
           if (id == null) {
               render status: NOT_FOUND
               return
           }
   
           personService.delete(id)
   
           respond ([status: OK], [message: 'Object deleted'])
       }
   }
   ```
   
   **Problem**
   When I want to delete a Person object using PersonController, I don't have 
any exception before to send the response. So, the server response is a `200`  
and the exception happens then (when the `beforeDelete` is executed)
   
   **Expected**
   I expect that when I want to delete a Person object using PersonController, 
I will have the exception before to send the response and the response will be 
a `500`.
   
   Note: When I was trying to reproduce this error, first I used the 
generate-all grails command  to create the controller. The command created this 
(I changed the name):
   
   ```
   class PersonWithoutTransactionController {  // this works as I expected
      //...
       def delete(Long id) {
           if (id == null) {
               render status: NOT_FOUND
               return
           }
   
           personService.delete(id)
   
           respond ([status: OK], [message: 'Object deleted'])
       }
   }
   ```
   
   This controller doens't have the Transactional annotation and works I 
expected. What is the correct way to do it? Should works the same in both cases?
   
   
   **Reproduce the error**
   To reproduce the error I have created these tests:
   ```
   class PersonFunctionalSpec extends GebSpec {
   
       RestBuilder getRestBuilder() {
           new RestBuilder()
       }
   
       void setup() {
           if (!Person.findByName('Tomy')) {
               new Person(name: 'Tomy').save(flush:true, failOnError:true)
           }
       }
   
       void "Test the delete action with Transaction Notation"() {
           when:
           def id = Person.findByName('Tomy').id
           def response = restBuilder.delete("${baseUrl}/person/$id")
   
           then:"you should have an error"
           response.status == INTERNAL_SERVER_ERROR.value() // <-- this fail 
       }
   
   
       void "Test the delete action without Transaction Notation"() {
           when:
           def id = Person.findByName('Tomy').id
           def response = 
restBuilder.delete("${baseUrl}/personWithoutTransaction/$id")
   
           then:"you should have an error"
           response.status == INTERNAL_SERVER_ERROR.value()
       }
   }
   ```
   Here is the sample app: https://github.com/AmaliaMV/neo4j-transactional
   
   The versions I am using are:
   ```
   grailsVersion=3.3.8
   gormVersion=6.1.11.BUILD-SNAPSHOT
   grailsNeo4jPluginVersion=6.2.0.BUILD-SNAPSHOT
   ```
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to