Uploaded image for project: 'Spring Data JPA'
  1. Spring Data JPA
  2. DATAJPA-1023

Reject stream executions if not executed within transaction

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 1.10.5 (Hopper SR5)
    • Component/s: None
    • Labels:
    • Environment:
      Mac OS X Sierra 10.12.1

      Description

      I'm using Stream's to loop through each element in a Repository in an @Scheduled method. After running 200 times I get the following error:

      2016-12-13 09:40:39.159  WARN 51004 --- [pool-2-thread-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: null
      2016-12-13 09:40:39.159 ERROR 51004 --- [pool-2-thread-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : [pool-2-thread-1] Timeout: Pool empty. Unable to fetch a connection in 2 seconds, none available[size:200; busy:200; idle:0; lastwait:2000].
      

      The Repository is defined as such:

      public interface PushNotificationRepository extends JpaRepository<PushNotification, Long> {
          @Query(value = "select p from PushNotification p")
          Stream<PushNotification> findAllAndStream();
      }
      

      The Service is defined like this:

      @Service
      @Transactional()
      public class PushNotificationFlusherService {
          private static final Logger LOGGER = LoggerFactory.getLogger(PushNotificationFlusherService.class);
      
          private final PushNotificationRepository pushNotificationRepository;
      
          @Autowired
          public PushNotificationFlusherService(PushNotificationRepository pushNotificationRepository) {
              this.pushNotificationRepository = pushNotificationRepository;
          }
      
          @Scheduled(fixedDelay = 5000)
          protected void flushPushNotificationQueue() {
              LOGGER.info("Flushing push notifications");
      
              try (Stream<PushNotification> pushNotifications = pushNotificationRepository.findAllAndStream()) {
                  pushNotifications.forEach((PushNotification pushNotification) -> {
                     // Send notification 
                  });
              }
      
              LOGGER.info("Flushed push notifications");
          }
      

      I've tried using @Transactional( readOnly=true) but it does not make a difference.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                olivergierke Oliver Drotbohm
                Reporter:
                MrHus Maarten Hus
                Last updater:
                Alexander Simeonov
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: