Spring synchronized NOT SYNCHRONIZED method

Environment:

  • apache tomcat 7
  • java 7
  • oracle 11g
  • eclipse
  • apache jmeter 2.1
  • spring
  • sleep mode

    I am working on a web application that receives requests from clients and generates a sequence number for them according to the type of request that will be used for further processing. To generate a unique serial number, I have a way to get the current serial number from the database and increase it by 1, and then update this record with a new serial number.

Function:

    @Transactional
public synchronized Long generateSequenseNumber(String requestType) {
     //get current sequence number for this requestType
            //increment it by one
            //update it in DB

}

The function works fine, but the problem is that I call the application from the stress testing tool (JMeter) to send 50 requests per second. I get below exceptions:

org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [

although the function is synchronized.

Any suggestion would be helpful. Thank.

+4
2

, @Transactional , .

, Spring @Transactional 10.5.1.

:

synchronized(this){
   generateSequenseNumber();
}
+5

...

@Transactional Spring - AOP. . , :

-> FooProxy#generateSequenseNumber
   -> TransactionInterceptor#invoke
      -> BEGIN TRANSACTION
         -> Foo#generateSequenceNumber (synchronized)
      -> COMMIT|ROLLBACK TRANSACTION

() , , .

generateSequenseNumber, TransactionTemplate REQUIRES_NEW. , @Transactional .

+1

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


All Articles