Cache is closed causing an exception while running test suite









up vote
0
down vote

favorite












I'm experiencing a problem similar to the described in this question.



I have a test suite that runs fine in development environment. One of the tests fails when executed in Bitbucket Pipelines with the following exception:



org.springframework.dao.InvalidDataAccessApiUsageException: Cache[model.Role] is closed; nested exception is java.lang.IllegalStateException: Cache[model.Role] is closed
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:364)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
....


I want to try the accepted solution but I don't know how to apply it to my project. The second solution depends on ehcache.xml file. I don't have this file, everything is configured in JavaConfig. How can I adopt the proposed solutions for EhCache + JCache (JSR-107) in JavaConfig?



My cache configuration:



@Configuration
@EnableCaching
public class CacheConfig

private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration =
Eh107Configuration.fromEhcacheCacheConfiguration(CacheConfigurationBuilder
.newCacheConfigurationBuilder(Object.class, Object.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(100, EntryUnit.ENTRIES))
.withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(60)))
.build());

@Bean
public JCacheManagerCustomizer cacheManagerCustomizer()
return cm ->
createIfNotExists(cm, "model.Role");
createIfNotExists(cm, "model.User.roles");
// ...
;


private void createIfNotExists(CacheManager cacheManager, String cacheName)
if (cacheManager.getCache(cacheName) == null)
cacheManager.createCache(cacheName, jcacheConfiguration);





Gradle dependencies:



implementation group: 'org.springframework.boot', name: 'spring-boot-starter-cache'
implementation group: 'javax.cache', name: 'cache-api'
implementation group: 'org.ehcache', name: 'ehcache'
implementation group: 'org.hibernate', name: 'hibernate-jcache'


The failing test:



@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class SecondLevelCacheTest
@Autowired
private RoleRepository roleRepository;
private CacheManager manager;

@Before
public void initCacheManager()
CachingProvider provider = Caching.getCachingProvider();
manager = provider.getCacheManager();

final String cacheRegion = "model.Role";
manager.getCache(cacheRegion).clear();


@Test
public final void givenEntityIsLoaded_thenItIsCached()
final String cacheRegion = "model.Role";

boolean hasNext = manager.getCache(cacheRegion).iterator().hasNext();
final Role role = roleRepository.findByName("USER");
boolean hasNext2 = manager.getCache(cacheRegion).iterator().hasNext();
final Role role2 = roleRepository.findByName("USER");

Assert.assertFalse(hasNext);
Assert.assertTrue(hasNext2);




The most upvoted solution is "to set shared property to false in the testing context." How can I do this with regard to my configuration?










share|improve this question

























    up vote
    0
    down vote

    favorite












    I'm experiencing a problem similar to the described in this question.



    I have a test suite that runs fine in development environment. One of the tests fails when executed in Bitbucket Pipelines with the following exception:



    org.springframework.dao.InvalidDataAccessApiUsageException: Cache[model.Role] is closed; nested exception is java.lang.IllegalStateException: Cache[model.Role] is closed
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:364)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
    ....


    I want to try the accepted solution but I don't know how to apply it to my project. The second solution depends on ehcache.xml file. I don't have this file, everything is configured in JavaConfig. How can I adopt the proposed solutions for EhCache + JCache (JSR-107) in JavaConfig?



    My cache configuration:



    @Configuration
    @EnableCaching
    public class CacheConfig

    private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration =
    Eh107Configuration.fromEhcacheCacheConfiguration(CacheConfigurationBuilder
    .newCacheConfigurationBuilder(Object.class, Object.class,
    ResourcePoolsBuilder.newResourcePoolsBuilder()
    .heap(100, EntryUnit.ENTRIES))
    .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(60)))
    .build());

    @Bean
    public JCacheManagerCustomizer cacheManagerCustomizer()
    return cm ->
    createIfNotExists(cm, "model.Role");
    createIfNotExists(cm, "model.User.roles");
    // ...
    ;


    private void createIfNotExists(CacheManager cacheManager, String cacheName)
    if (cacheManager.getCache(cacheName) == null)
    cacheManager.createCache(cacheName, jcacheConfiguration);





    Gradle dependencies:



    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-cache'
    implementation group: 'javax.cache', name: 'cache-api'
    implementation group: 'org.ehcache', name: 'ehcache'
    implementation group: 'org.hibernate', name: 'hibernate-jcache'


    The failing test:



    @SpringBootTest
    @RunWith(SpringJUnit4ClassRunner.class)
    public class SecondLevelCacheTest
    @Autowired
    private RoleRepository roleRepository;
    private CacheManager manager;

    @Before
    public void initCacheManager()
    CachingProvider provider = Caching.getCachingProvider();
    manager = provider.getCacheManager();

    final String cacheRegion = "model.Role";
    manager.getCache(cacheRegion).clear();


    @Test
    public final void givenEntityIsLoaded_thenItIsCached()
    final String cacheRegion = "model.Role";

    boolean hasNext = manager.getCache(cacheRegion).iterator().hasNext();
    final Role role = roleRepository.findByName("USER");
    boolean hasNext2 = manager.getCache(cacheRegion).iterator().hasNext();
    final Role role2 = roleRepository.findByName("USER");

    Assert.assertFalse(hasNext);
    Assert.assertTrue(hasNext2);




    The most upvoted solution is "to set shared property to false in the testing context." How can I do this with regard to my configuration?










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm experiencing a problem similar to the described in this question.



      I have a test suite that runs fine in development environment. One of the tests fails when executed in Bitbucket Pipelines with the following exception:



      org.springframework.dao.InvalidDataAccessApiUsageException: Cache[model.Role] is closed; nested exception is java.lang.IllegalStateException: Cache[model.Role] is closed
      at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:364)
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
      at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
      at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
      ....


      I want to try the accepted solution but I don't know how to apply it to my project. The second solution depends on ehcache.xml file. I don't have this file, everything is configured in JavaConfig. How can I adopt the proposed solutions for EhCache + JCache (JSR-107) in JavaConfig?



      My cache configuration:



      @Configuration
      @EnableCaching
      public class CacheConfig

      private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration =
      Eh107Configuration.fromEhcacheCacheConfiguration(CacheConfigurationBuilder
      .newCacheConfigurationBuilder(Object.class, Object.class,
      ResourcePoolsBuilder.newResourcePoolsBuilder()
      .heap(100, EntryUnit.ENTRIES))
      .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(60)))
      .build());

      @Bean
      public JCacheManagerCustomizer cacheManagerCustomizer()
      return cm ->
      createIfNotExists(cm, "model.Role");
      createIfNotExists(cm, "model.User.roles");
      // ...
      ;


      private void createIfNotExists(CacheManager cacheManager, String cacheName)
      if (cacheManager.getCache(cacheName) == null)
      cacheManager.createCache(cacheName, jcacheConfiguration);





      Gradle dependencies:



      implementation group: 'org.springframework.boot', name: 'spring-boot-starter-cache'
      implementation group: 'javax.cache', name: 'cache-api'
      implementation group: 'org.ehcache', name: 'ehcache'
      implementation group: 'org.hibernate', name: 'hibernate-jcache'


      The failing test:



      @SpringBootTest
      @RunWith(SpringJUnit4ClassRunner.class)
      public class SecondLevelCacheTest
      @Autowired
      private RoleRepository roleRepository;
      private CacheManager manager;

      @Before
      public void initCacheManager()
      CachingProvider provider = Caching.getCachingProvider();
      manager = provider.getCacheManager();

      final String cacheRegion = "model.Role";
      manager.getCache(cacheRegion).clear();


      @Test
      public final void givenEntityIsLoaded_thenItIsCached()
      final String cacheRegion = "model.Role";

      boolean hasNext = manager.getCache(cacheRegion).iterator().hasNext();
      final Role role = roleRepository.findByName("USER");
      boolean hasNext2 = manager.getCache(cacheRegion).iterator().hasNext();
      final Role role2 = roleRepository.findByName("USER");

      Assert.assertFalse(hasNext);
      Assert.assertTrue(hasNext2);




      The most upvoted solution is "to set shared property to false in the testing context." How can I do this with regard to my configuration?










      share|improve this question













      I'm experiencing a problem similar to the described in this question.



      I have a test suite that runs fine in development environment. One of the tests fails when executed in Bitbucket Pipelines with the following exception:



      org.springframework.dao.InvalidDataAccessApiUsageException: Cache[model.Role] is closed; nested exception is java.lang.IllegalStateException: Cache[model.Role] is closed
      at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:364)
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
      at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
      at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
      ....


      I want to try the accepted solution but I don't know how to apply it to my project. The second solution depends on ehcache.xml file. I don't have this file, everything is configured in JavaConfig. How can I adopt the proposed solutions for EhCache + JCache (JSR-107) in JavaConfig?



      My cache configuration:



      @Configuration
      @EnableCaching
      public class CacheConfig

      private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration =
      Eh107Configuration.fromEhcacheCacheConfiguration(CacheConfigurationBuilder
      .newCacheConfigurationBuilder(Object.class, Object.class,
      ResourcePoolsBuilder.newResourcePoolsBuilder()
      .heap(100, EntryUnit.ENTRIES))
      .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(60)))
      .build());

      @Bean
      public JCacheManagerCustomizer cacheManagerCustomizer()
      return cm ->
      createIfNotExists(cm, "model.Role");
      createIfNotExists(cm, "model.User.roles");
      // ...
      ;


      private void createIfNotExists(CacheManager cacheManager, String cacheName)
      if (cacheManager.getCache(cacheName) == null)
      cacheManager.createCache(cacheName, jcacheConfiguration);





      Gradle dependencies:



      implementation group: 'org.springframework.boot', name: 'spring-boot-starter-cache'
      implementation group: 'javax.cache', name: 'cache-api'
      implementation group: 'org.ehcache', name: 'ehcache'
      implementation group: 'org.hibernate', name: 'hibernate-jcache'


      The failing test:



      @SpringBootTest
      @RunWith(SpringJUnit4ClassRunner.class)
      public class SecondLevelCacheTest
      @Autowired
      private RoleRepository roleRepository;
      private CacheManager manager;

      @Before
      public void initCacheManager()
      CachingProvider provider = Caching.getCachingProvider();
      manager = provider.getCacheManager();

      final String cacheRegion = "model.Role";
      manager.getCache(cacheRegion).clear();


      @Test
      public final void givenEntityIsLoaded_thenItIsCached()
      final String cacheRegion = "model.Role";

      boolean hasNext = manager.getCache(cacheRegion).iterator().hasNext();
      final Role role = roleRepository.findByName("USER");
      boolean hasNext2 = manager.getCache(cacheRegion).iterator().hasNext();
      final Role role2 = roleRepository.findByName("USER");

      Assert.assertFalse(hasNext);
      Assert.assertTrue(hasNext2);




      The most upvoted solution is "to set shared property to false in the testing context." How can I do this with regard to my configuration?







      ehcache spring-test jcache shared-cache






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 0:39









      naXa

      12.8k884131




      12.8k884131






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          The proposed solution you are talking about is based on Ehcache 2. You are using Ehcache 3 (good for you), so it's not valid.



          Spring will take care of closing the CacheManager so normally, you don't need to take care of anything. Also, you do not need to access it through the CachingProvider. You can @Autowired the javax.cache.CacheManager and that way you are sure to get the right one.



          However, you are using Hibernate. You should make sure that Spring and Hibernate are using the same CacheManager. The way to configure it depends on the Spring and Hibernate version.



          Can we have the full stack trace? Right now it feels like something is closing the CacheManager without deregister it from the CachingProvider. This is impossible unless you are closing the org.ehcache.CacheManagerwithout closing the javax.cache.CacheManager wrapping it. Closing the later will cause the deregistration.






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234996%2fcache-is-closed-causing-an-exception-while-running-test-suite%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            The proposed solution you are talking about is based on Ehcache 2. You are using Ehcache 3 (good for you), so it's not valid.



            Spring will take care of closing the CacheManager so normally, you don't need to take care of anything. Also, you do not need to access it through the CachingProvider. You can @Autowired the javax.cache.CacheManager and that way you are sure to get the right one.



            However, you are using Hibernate. You should make sure that Spring and Hibernate are using the same CacheManager. The way to configure it depends on the Spring and Hibernate version.



            Can we have the full stack trace? Right now it feels like something is closing the CacheManager without deregister it from the CachingProvider. This is impossible unless you are closing the org.ehcache.CacheManagerwithout closing the javax.cache.CacheManager wrapping it. Closing the later will cause the deregistration.






            share|improve this answer
























              up vote
              0
              down vote













              The proposed solution you are talking about is based on Ehcache 2. You are using Ehcache 3 (good for you), so it's not valid.



              Spring will take care of closing the CacheManager so normally, you don't need to take care of anything. Also, you do not need to access it through the CachingProvider. You can @Autowired the javax.cache.CacheManager and that way you are sure to get the right one.



              However, you are using Hibernate. You should make sure that Spring and Hibernate are using the same CacheManager. The way to configure it depends on the Spring and Hibernate version.



              Can we have the full stack trace? Right now it feels like something is closing the CacheManager without deregister it from the CachingProvider. This is impossible unless you are closing the org.ehcache.CacheManagerwithout closing the javax.cache.CacheManager wrapping it. Closing the later will cause the deregistration.






              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                The proposed solution you are talking about is based on Ehcache 2. You are using Ehcache 3 (good for you), so it's not valid.



                Spring will take care of closing the CacheManager so normally, you don't need to take care of anything. Also, you do not need to access it through the CachingProvider. You can @Autowired the javax.cache.CacheManager and that way you are sure to get the right one.



                However, you are using Hibernate. You should make sure that Spring and Hibernate are using the same CacheManager. The way to configure it depends on the Spring and Hibernate version.



                Can we have the full stack trace? Right now it feels like something is closing the CacheManager without deregister it from the CachingProvider. This is impossible unless you are closing the org.ehcache.CacheManagerwithout closing the javax.cache.CacheManager wrapping it. Closing the later will cause the deregistration.






                share|improve this answer












                The proposed solution you are talking about is based on Ehcache 2. You are using Ehcache 3 (good for you), so it's not valid.



                Spring will take care of closing the CacheManager so normally, you don't need to take care of anything. Also, you do not need to access it through the CachingProvider. You can @Autowired the javax.cache.CacheManager and that way you are sure to get the right one.



                However, you are using Hibernate. You should make sure that Spring and Hibernate are using the same CacheManager. The way to configure it depends on the Spring and Hibernate version.



                Can we have the full stack trace? Right now it feels like something is closing the CacheManager without deregister it from the CachingProvider. This is impossible unless you are closing the org.ehcache.CacheManagerwithout closing the javax.cache.CacheManager wrapping it. Closing the later will cause the deregistration.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 at 3:07









                Henri

                3,44411322




                3,44411322



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234996%2fcache-is-closed-causing-an-exception-while-running-test-suite%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                    Syphilis

                    Darth Vader #20