Grails 2.5: complex "findOrCreate" and synchronization between threads (HTTP sessions)

In the Grails application, we have an interface that takes a three-level structure similar to a path that is created from parameters with a single request of the type

level1/level2/document

level2Link object level1and documentlinks level2.

Several objects using the same substrate level level1 / level2 can be loaded in parallel, and therefore, synchronization between sessions must be implemented. Each of the accounts can have its own structure.

In essence, the logic is similar to the advanced findOrCreate, which should be synchronized between multiple concurrent queries.

My idea was to implement something like this inside a service method:

Doc d
synchronised(currentUser) {
    Dir.withNewTransaction {
       Dir l1 = findFirstLevel(level1) // (A)
       if (!l1) {
           l1 = new Dir(name: level1)
           l1.save()
       }
       Dir l2 = findSecondLevel(l1, level2)
       if (!l2) {
           l2 = new Dir(name: level2, parent: l1)
           l2.save()
       }
       d = new Doc(name: docName, parent: l2)
       d.save()
    } // (B)
} // (C)
saveDocumentContent(d, content)

withNewTransaction, , , , .

, T1. , , (A), null T2, , , T1 . :

D1/D2/D3
D1/D2/D4

D1/D2/D3
     /D4

, (B) withNewTransaction , , T2, .

.

?

: .

EDIT 2: Hibernate 3.6 Grails. ?

+4
1

, , , . .

0

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


All Articles