-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong response when an Exception happens when an object will be saved in a Controller with Transactional annotation #99
Comments
Is the |
@jameskleeh . Yes, it's transactional. |
@AmaliaMV Then the controller should not be transactional |
@jameskleeh, I did what you told me. However, I have an exception. The message of the exception is: "Cannot run more statements in this transaction, it has been committed" Finally, I could reproduce the problem. I updated the sample app to reproduce the same error that I have in the app: https://github.com/AmaliaMV/neo4j-transactional. If you run
The changes I made were:
Log This is the log I have when I run the test:
Summarizing During the binding, a new transaction is opened because I'm doing a call to When person.save() is called, AutoUserstampEventListener.onPersistenceEvent() is invoked. The transaction is closed when Then, when trying to insert the new pet in the database, I have an error because there isn't any active transaction. |
@AmaliaMV I believe this behavior is correct. You should be wrapping new database access inside a listener in a new session. |
@jameskleeh, we are wondering if there is a pattern to write a controller. As you can see in the new sample app (https://github.com/AmaliaMV/grails-transaction2), we are doing the binding in the controller. In some cases, during the binding we have access to the databases to search for an object, add or remove an object from a collection, and bind dynamic properties. If the controller isn't transactional, should the binding process be in a service? Is it right to do it in a service? On the other hand, if we need to access the database within a listener, should we always do in a new session? Is wrong access the database within a listener without starting a new session even though the controller is transactional? Finally, if we need to access the database to read operation inside json views and the controller isn't transactional, how do we have to do? Thanks! |
@AmaliaMV The binding can happen in a service, however the object that is bound isn't persisted in the controller. Unless its causing some sort of problem, I don't consider that an issue. You should always be wrapping any database logic in a new session if it's executed in any GORM listener. You shouldn't be accessing the database in a JSON view at all. All of the necessary data to render the response should be passed to the view. |
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:In addition, I have a controller which has Transactional annotation and this
delete()
action: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 thebeforeDelete
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):
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:
Here is the sample app: https://github.com/AmaliaMV/neo4j-transactional
The versions I am using are:
The text was updated successfully, but these errors were encountered: