gpc / grails-audit-logging-plugin

The Grails Audit Logging Plugin
Apache License 2.0
50 stars 60 forks source link

javax.persistence.TransactionRequiredException: no transaction is in progress #175

Closed petrosbaltzis closed 3 years ago

petrosbaltzis commented 6 years ago

I am getting the exception above when I try to save an auditable domain. Grails version: 3.3.2 Grails-audit-logging-plugin version: 3.0.1

On the service level, I use the @Transactional annotation, therefore, in theory, there is an open transaction. I have tried to use different propagations (REQUIRED, REQUIRED_NEW) but I got the same exception.

I wonder if it is something wrong in my configuration or if it is a potential bug.

robertoschwald commented 6 years ago

Should work. See #161

jkrshw commented 6 years ago

The "javax.persistence.TransactionRequiredException" is caused by a change in hibernate 5.2 that now requires a transaction on all flushing updates

It seems the transactional behaviour of the plugin is not working as expected, I have created a branch with failing test that shows the audit logs are persisted even if the transaction is rolled back.

The existing test "Test failed insert logging" doesn't catch this as grails validation prevents the insert from ever being executed.

Alternatively, updating to hibernate 5.2.17 will cause all integration tests to fail with "javax.persistence.TransactionRequiredException"

You may have more experience with grails application events, I can't tell from the documentation how transaction propagation is meant to work for application events.

longwa commented 5 years ago

The events aren't within the existing transaction as they are triggered as part of the flush itself. The solution is to use the same approach as hibernate-envers, which is to register a callback which is executed on transaction commit to write the entries. This is a requirement for the plugin to work correctly in Grails 4.0 as it requires Hibernate 5.4+.

I'm working on a fix.

luke10 commented 5 years ago

Just to add my 2c, I'm also blocked by this issue as I am using a more recent version of Hibernate for other purposes. For now, I've avoided the issue by setting hibernate.allow_update_outside_transaction = true but this must never make it to production..

wkulik commented 5 years ago

I also have the allow update outside transaction set to true for now. I am currently using Grails 3.3. Are there any updates upcoming or suggested workarounds that can be used in a Production environment, or should I use a different auditing method?

robertoschwald commented 5 years ago

We are working on the tests for longwa's approach. See branch 4.0.0_wip

nitinmahawadiwar commented 4 years ago

I am able to fix the issue as commented by @luke10 , but then what should be ideal solution for Production environment.

robertoschwald commented 4 years ago

As we are not ready with the new approach, yet, we use a workaround which spawns own transaction. Give release 3.0.6 a try.