An error occurred when multiple threads in Spring called a method in Service.

the method of entering the library in service needs to be tested in the case of multithreading, and then two threads are opened to execute the service method in a loop, but there are a lot of problems, including the following situations.

  1. both threads are halfway through execution and then terminate with an error. The database has several pieces of data
  2. both threads are executed only once, and the database has one piece of data each
  3. Database has no data

related codes

 @Rollback(value = false)
    @Test
    public void test() throws Exception {
//        ,
//        for (int i = 0; i < 10; iPP) {
//            normal(Thread.currentThread().getId(), i);
//        }

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; iPP) {
                    normal(Thread.currentThread().getId(), i);
                }
            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; iPP) {
                    normal(Thread.currentThread().getId(), i);
                }
            }
        });

        thread1.start();
        thread2.start();

    }

    public void normal(long id, int n) {
        auditLoggingService.insertAuditLogging("" + id + "/" + n, 56, 6, 0,
                BaseConstants.AuditLoggingOperatorType.MODIFY, "", "", 0, "", "", "", "", BaseConstants.UserInfo.USER_CAT_INNER, "yangyan", "");

    }

error message

  1. can sometimes be executed successfully with no error message, but there is no information in the database
  2. Returning cached instance of singleton bean "org.springframework.transaction.interceptor.TransactionInter
Apr.15,2021

use multithreading in the test class. When the test program is finished, the thread shuts down. So there will be such a problem.
the solution is to keep the test program running continuously, such as sleep for a period of time, let the thread finish first, or add System.in.read () to the end of the program to read and wait.

Menu