Running a job in @Scheduled does not call spring data jpa save

I planned to work with annotation @Scheduled

which was to process the data and store it in the database using spring data jpa. The method save

was called without any exception, but there was no insert into the database. In the same annotated method, I called a method findAll

that worked with exact and retrieved data. What could be the reason?

@Repository
public interface PossibleOfferLinkRepository extends PagingAndSortingRepository<PossibleOfferLink,   Long> {
}


@Configuration
@ComponentScan
@EnableAutoConfiguration
@Import({Scheduler.class})
@EntityScan(basePackages="model_package")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }


}

@EnableScheduling
@ConditionalOnProperty(value= "property_name")
public class Scheduler {

...
    @Scheduled(fixedRate=100000)
    public void scheduleCrawlerJob() throws MalformedURLException {
            Iterable<PossibleOfferLink> links = repo.findAll();
            PossibleOfferLink link = repo.save(new PossibleOfferLink(new URL("...")));
    }

}

      

Maven

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.1.8.RELEASE</version>
        </parent>
    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-batch</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <version>1.4.182</version>
            </dependency>
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>${guava.version}</version>
            </dependency>
            <!-- Test -->
            <dependency>
                <groupId>org.easytesting</groupId>
                <artifactId>fest-assert</artifactId>
                <version>${easytesting.version}</version>
            </dependency>
        </dependencies>

      

+3


source to share


1 answer


Your problem is due to the transaction not being executed.

Cause:

javax.persistence.TransactionRequiredException: no transaction is in progress

      

Here is an example of how to set up an annotation transaction manager. Here's the link



@Configuration
@EnableTransactionManagement
public class PersistenceJPAConfig{

   @Bean
   public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){
      ...
   }

   @Bean
   public PlatformTransactionManager transactionManager(){
      JpaTransactionManager transactionManager = new JpaTransactionManager();
      transactionManager.setEntityManagerFactory(
       entityManagerFactoryBean().getObject() );
      return transactionManager;
   }

}

      

Add to scheduleCrawlerJob

@Transactional

@Scheduled(fixedRate=100000)
@Transactional
public void scheduleCrawlerJob() throws MalformedURLException {
        Iterable<PossibleOfferLink> links = repo.findAll();
        PossibleOfferLink link = repo.save(new PossibleOfferLink(new URL("...")));
}

      

+1


source







All Articles