Failed to load data using Grails Audit-Trail plugin

I am working on a Grails 2.3.4 application. I am adding the Audit-Trail v2.0.3 plugin to our project because we have a requirement to keep track of all the creation and change times and users to help debug problems that may arise. I have successfully installed the plugin, and everything works EXCEPT that we can no longer upload data to the development environment.

This is our Config.groovy:

plugin {

    audittrail {
        createdBy.field = "createUser"
        createdBy.type = "java.lang.String"
        createdDate.field = "createDate"

        editedBy.field = "updateUser"
        editedBy.type = "java.lang.String"
        editedDate.field = "updateDate"
        /*
            This closure returns the value of the user for the plugin.
         */
        currentUserClosure = { ctx ->
            ctx.userServiceProxy.getUserToken()

        }
    }

Here is the UserService package com.perceptivecloud.frg.datadefinition

/**
* Service class to help the Audit-Trail plugin get userId information.
*
* TODO:This will need to be revisited when  real Authorization is implemented.
*/
class UserService {
/*
    force a new instance of the service per session.
    see http://grails.org/doc/latest/guide/services.html#scopedServices for details
 */
static scope = "session"

   String userToken

    void setUserToken(String userId) {
        userToken = userId
    }

    String getUserToken() {
        userToken
    }

}

The proxy service is defined in .groovy resources:

userServiceProxy(ScopedProxyFactoryBean) {
    targetBeanName = 'userService'
    proxyTargetClass = true
}

Our Domain class is as follows:

@AuditStamp
class MyDomainClass {
    String name

    static constraints = {
        name nullable: false
    }

}

Our BootStrap.groovy looks like this:

def userServiceProxy
...
development {
    userServiceProxy.setUserToken("bootstrap")

    def claims = new MyDomainClass(name: 'Submit claim')
    claims.save(flush: true)
}

When I launch the application, I get a stack trace that looks like this:

| Error 2014-03-13 13:10:48,963 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error initializing the application: Error creating bean with name 'userService': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
Message: Error creating bean with name 'userService': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
    Line | Method
->>   41 | doCall                       in BootStrap$_closure1_closure2_closure4
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    321 | execute                      in grails.util.Environment$EnvironmentBlockEvaluator
|    302 | executeForEnvironment . . .  in grails.util.Environment
|    277 | executeForCurrentEnvironment in     ''
|    262 | run . . . . . . . . . . . .  in java.util.concurrent.FutureTask
|   1145 | runWorker                    in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . . . . . . . . . .  in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run                          in java.lang.Thread

When I tried to create a session in bootstrap, for example:

development {
    WebUtils.storeGrailsWebRequest(new GrailsWebRequest(new GrailsMockHttpServletRequest(),
        new GrailsMockHttpServletResponse(), (ServletContext)servletContext))
WebUtils.retrieveGrailsWebRequest().applicationContext.getBean('userServiceProxy')
    .setUserToken("userToken")

    def claims = new MyDomainClass(name: 'Submit claim')
    claims.save(flush: true)
}

.

| Error 2014-03-13 12:50:34,179 [localhost-startStop-1] ERROR hibernate.AssertionFailure  - an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
Message: null id in MyDomainClass entry (don't flush the Session after an exception occurs)
    Line | Method
->>  112 | doCall                       in Config$_run_closure2_closure7_closure11_closure12
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     67 | currentUserId                in grails.plugin.audittrail.AuditTrailHelper
|     45 | doCall . . . . . . . . . . . in grails.plugin.audittrail.AuditTrailInterceptor$_onSave_closure1
|     37 | onSave                       in grails.plugin.audittrail.AuditTrailInterceptor
|     46 | doCall . . . . . . . . . . . in BootStrap$_closure1_closure2_closure4
|    321 | execute                      in grails.util.Environment$EnvironmentBlockEvaluator
|    302 | executeForEnvironment . . .  in grails.util.Environment
|    277 | executeForCurrentEnvironment in     ''
|    262 | run . . . . . . . . . . . .  in java.util.concurrent.FutureTask
|   1145 | runWorker                    in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . . . . . . . . . .  in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run                          in java.lang.Thread

, , - , , .

, , , , Audit-Trail.

+4

Source: https://habr.com/ru/post/1531624/


All Articles