• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Spring - JPA/Hibernate integration @Schedular and @Transactional - Adding duplicate records

 
Ranch Hand
Posts: 138
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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.

This way the code is written.

If anyone knows anything, let me know.

Thanks,
Atul
 
Atul More
Ranch Hand
Posts: 138
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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?

Thanks,
Atul
 
Atul More
Ranch Hand
Posts: 138
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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.

Hope this helps to someone.


Thanks,
Atul
 
reply
    Bookmark Topic Watch Topic
  • New Topic