I have created a schedular job, which insert data into tables. I am using Spring4, JPA2.1.
The scenario is:
1. Schedular job runs.
2. Read the data from table X
3. Process data and insert records in table A.
4. Next step, is to read the newly inserted records
5. Process them and insert processed data into table B.
The problem occurs at step 3, where it inserts duplicate records in table A. I have a loop but it only reads the records, process it and then insert it into DB.
The configuration is like:
1. The schedular job is marked with @EnableSchedular and @Service annotations.
2. From the schedular class I am calling a method of another class which is also marked as @Service annotaions.
3. The method is marked with @Transactional annotation with propagation required new.
4. It calls the DAO class method. The DAO class is marked with @Repository annotations.
I figure out the issue, the schedular method called twice and because of that it insert duplicate records.
But still dont understand, why schedular method called twice?
Once it trigger the method, only one thread can work on it, why it spans two thread?
Is Spring Schedular has any issue?
The issue has been resolved.
The problem was, the "component-scan" was defined in two xml files. One is spring-servlet.xml(Used for MVC, servlet) and another one is applicationContext.xml file.
I removed the "component-scan" from spring-servlet.xml file and its start working fine. Only single call me made the schedular method.
Because of two "component-scan", the application was scanned twice and it find the annatation and call them again.