In my application, I use @LastModifiedBy
and it's working perfectly until I decided to use co-routines for performances mather.
Now, since the calls to my repositories are inside a Future and executed in a coroutine, fields tagged with @LastModifiedBy
or @LastModifiedDate
are no more filled when they are persisted.
When I'm debugging and stop inside my code executed into a coroutine, SecurityContextHolder
is empty whereas it's filled outside the coroutine.
My code looks like that :
@RestController
class Controller( val service : MyService){
( ... )
@PutMapping(...)
fun saveStuff( val allStuff : List<Stuff>) : String{
return service.saveStuff(allStuff )
}
}
and for the coroutine's part :
@Service
class MyService( val repo: MyRepository){
fun saveStuff( allStuff: List<Stuff>){
val deferred: List<Deferred<Unit>> =
allStuff.forEach{
GlobalScope.async { repo.save(stuff)}
}
runBlocking {
val count = deferred.map { it.await() }.count()
log.info(" all the $count future(s) have finish ")
}
}
}
@Entity
data class Stuff(
@CreatedDate
var creationDate: Timestamp? = null,
@LastModifiedBy
var lastModificationBy: String? = null,
@LastModifiedDate
var lastModificationDate: Timestamp? = null
)