Search code examples
grailsdatasourcegrails-orm

Concurrency Issue Grails


I am using this code for updating a row.

SequenceNumber.withNewSession  {
   def hibSession = sessionFactory.getCurrentSession()
   Sql sql = new Sql(hibSession.connection())
   def rows = sql.rows("select for update query");
}

in this query I am updating the number initially sequenceNumber is 1200. and every time this code run then it will b increamented by 1. and I am running this code 5 times in loop. but this is not flushing the hibernate session so that every time I am getting the same number 1201.

I have also used

hibSession.clear()
hibSession.flush()

but no success.

If I use following code then its works fine.

SequenceNumber.withNewSession  {
   Sql sql = new Sql(dataSource)
   def rows = sql.rows("select for update query")
}

every time I am getting a new number.

Can anybody tell me what's wrong with above code


Solution

  • Try with Transaction, + flush it on the end, like:

    SequenceNumber.withTransaction { TransactionStatus status ->
        ...
        status.flush()
    }