在Spring Data JPA中的Pageable,PageRequest或Spring中的JdkDynamicAopProxy是否存在错误?

时间:2013-11-02 12:51:47

标签: spring hibernate jpa pagination spring-data

以下集成测试方法testLocalPaging使用HSQL内存数据库,Stock表中有100个条目。存储库类是使用Spring Data JPA实现的,它只是一个扩展PagingAndSortingRepository的接口。但是,当页数设置为大约15或更低时,测试将在第六页“冻结”。我意识到从数据库中读取大约6页后测试将停滞不前。但是,如果我使用更大的页面大小,例如20,则测试运行得很好,在五页之后从数据库读取的所有100个条目都没有问题。我想知道Spring Data JPA是否存在错误?我注意到导致问题的那一行在org.springframework.aop.framework.JdkDynamicAopProxy的第202行。我正在使用spring-data-jpa-1.4.1.RELEASE,spring-aop-3.1.4.RELEASE和hibernate-core-3.6.10.Final等。有人可以指出我的测试中是否有问题或Spring或Hibernate中的错误?我对测试结果感到很困惑。

StockDaoJpa:

import org.springframework.data.repository.PagingAndSortingRepository;

public interface StockDaoJpa extends PagingAndSortingRepository<Stock, Long>{
}

测试方法:

@Test
public void testLocalPaging()
{
    log.info("testLocalPaging()...");
    int size = 10;
    Page<Stock> page = null;
    Pageable request = new PageRequest(0, size, new Sort(new Order("StockId")));
    do
    {
        if(page == null)
            page = stockRepository.findAll(request);
        else
        {
            request = page.nextPageable();
            log.info(request);
            //TODO: It freezes at the sixth request whatever the pagesize is.
            page = stockRepository.findAll(request);
        }
        log.info(page);
        for(Stock s : page)
            log.info(String.format("Id = %s", s.getStockId()));
        log.info(page.hasNextPage());
    } while(page.hasNextPage());
}

JdkDynamicAopProxy类中的代码,测试卡在:

...
            else {
                // We need to create a method invocation...
                invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
                // Proceed to the joinpoint through the interceptor chain.
                retVal = invocation.proceed();
            }
...

从testLocalPaing输出:

[INFO ] | 20:38:22 | [mainhread] |           ConfigTest    63 | testLocalPaging()...
[INFO ] | 20:38:22 | [mainhread] |           ConfigTest    78 | Page 0 of 10 containing jstat.sample.Stock instances
[INFO ] | 20:38:22 | [mainhread] |           ConfigTest    80 | Id = 1
[INFO ] | 20:38:22 | [mainhread] |           ConfigTest    80 | Id = 2
[INFO ] | 20:38:22 | [mainhread] |           ConfigTest    80 | Id = 3
[INFO ] | 20:38:22 | [mainhread] |           ConfigTest    80 | Id = 4
[INFO ] | 20:38:22 | [mainhread] |           ConfigTest    80 | Id = 5
[INFO ] | 20:38:22 | [mainhread] |           ConfigTest    80 | Id = 6
[INFO ] | 20:38:22 | [mainhread] |           ConfigTest    80 | Id = 7
[INFO ] | 20:38:22 | [mainhread] |           ConfigTest    80 | Id = 8
[INFO ] | 20:38:22 | [mainhread] |           ConfigTest    80 | Id = 9
[INFO ] | 20:38:22 | [mainhread] |           ConfigTest    80 | Id = 10
[INFO ] | 20:38:22 | [mainhread] |           ConfigTest    81 | true
[INFO ] | 20:38:22 | [mainhread] |           ConfigTest    74 | Page request [number: 1, size 10, sort: StockId: ASC]
[INFO ] | 20:38:26 | [mainhread] |           ConfigTest    78 | Page 1 of 10 containing jstat.sample.Stock instances
[INFO ] | 20:38:26 | [mainhread] |           ConfigTest    80 | Id = 11
[INFO ] | 20:38:26 | [mainhread] |           ConfigTest    80 | Id = 12
[INFO ] | 20:38:26 | [mainhread] |           ConfigTest    80 | Id = 13
[INFO ] | 20:38:26 | [mainhread] |           ConfigTest    80 | Id = 14
[INFO ] | 20:38:26 | [mainhread] |           ConfigTest    80 | Id = 15
[INFO ] | 20:38:26 | [mainhread] |           ConfigTest    80 | Id = 16
[INFO ] | 20:38:26 | [mainhread] |           ConfigTest    80 | Id = 17
[INFO ] | 20:38:26 | [mainhread] |           ConfigTest    80 | Id = 18
[INFO ] | 20:38:26 | [mainhread] |           ConfigTest    80 | Id = 19
[INFO ] | 20:38:26 | [mainhread] |           ConfigTest    80 | Id = 20
[INFO ] | 20:38:26 | [mainhread] |           ConfigTest    81 | true
[INFO ] | 20:38:26 | [mainhread] |           ConfigTest    74 | Page request [number: 2, size 10, sort: StockId: ASC]
[INFO ] | 20:38:28 | [mainhread] |           ConfigTest    78 | Page 2 of 10 containing jstat.sample.Stock instances
[INFO ] | 20:38:28 | [mainhread] |           ConfigTest    80 | Id = 21
[INFO ] | 20:38:28 | [mainhread] |           ConfigTest    80 | Id = 22
[INFO ] | 20:38:28 | [mainhread] |           ConfigTest    80 | Id = 23
[INFO ] | 20:38:28 | [mainhread] |           ConfigTest    80 | Id = 24
[INFO ] | 20:38:28 | [mainhread] |           ConfigTest    80 | Id = 25
[INFO ] | 20:38:28 | [mainhread] |           ConfigTest    80 | Id = 26
[INFO ] | 20:38:28 | [mainhread] |           ConfigTest    80 | Id = 27
[INFO ] | 20:38:28 | [mainhread] |           ConfigTest    80 | Id = 28
[INFO ] | 20:38:28 | [mainhread] |           ConfigTest    80 | Id = 29
[INFO ] | 20:38:28 | [mainhread] |           ConfigTest    80 | Id = 30
[INFO ] | 20:38:28 | [mainhread] |           ConfigTest    81 | true
[INFO ] | 20:38:28 | [mainhread] |           ConfigTest    74 | Page request [number: 3, size 10, sort: StockId: ASC]
[INFO ] | 20:38:29 | [mainhread] |           ConfigTest    78 | Page 3 of 10 containing jstat.sample.Stock instances
[INFO ] | 20:38:29 | [mainhread] |           ConfigTest    80 | Id = 31
[INFO ] | 20:38:29 | [mainhread] |           ConfigTest    80 | Id = 32
[INFO ] | 20:38:29 | [mainhread] |           ConfigTest    80 | Id = 33
[INFO ] | 20:38:29 | [mainhread] |           ConfigTest    80 | Id = 34
[INFO ] | 20:38:29 | [mainhread] |           ConfigTest    80 | Id = 35
[INFO ] | 20:38:29 | [mainhread] |           ConfigTest    80 | Id = 36
[INFO ] | 20:38:29 | [mainhread] |           ConfigTest    80 | Id = 37
[INFO ] | 20:38:29 | [mainhread] |           ConfigTest    80 | Id = 38
[INFO ] | 20:38:29 | [mainhread] |           ConfigTest    80 | Id = 39
[INFO ] | 20:38:29 | [mainhread] |           ConfigTest    80 | Id = 40
[INFO ] | 20:38:29 | [mainhread] |           ConfigTest    81 | true
[INFO ] | 20:38:29 | [mainhread] |           ConfigTest    74 | Page request [number: 4, size 10, sort: StockId: ASC]
[INFO ] | 20:39:09 | [mainhread] |           ConfigTest    78 | Page 4 of 10 containing jstat.sample.Stock instances
[INFO ] | 20:39:10 | [mainhread] |           ConfigTest    80 | Id = 41
[INFO ] | 20:39:12 | [mainhread] |           ConfigTest    80 | Id = 42
[INFO ] | 20:39:12 | [mainhread] |           ConfigTest    80 | Id = 43
[INFO ] | 20:39:12 | [mainhread] |           ConfigTest    80 | Id = 44
[INFO ] | 20:39:12 | [mainhread] |           ConfigTest    80 | Id = 45
[INFO ] | 20:39:12 | [mainhread] |           ConfigTest    80 | Id = 46
[INFO ] | 20:39:12 | [mainhread] |           ConfigTest    80 | Id = 47
[INFO ] | 20:39:12 | [mainhread] |           ConfigTest    80 | Id = 48
[INFO ] | 20:39:12 | [mainhread] |           ConfigTest    80 | Id = 49
[INFO ] | 20:39:12 | [mainhread] |           ConfigTest    80 | Id = 50
[INFO ] | 20:39:12 | [mainhread] |           ConfigTest    81 | true
[INFO ] | 20:39:12 | [mainhread] |           ConfigTest    74 | Page request [number: 5, size 10, sort: StockId: ASC]
[INFO ] | 20:39:31 | [mainhread] |           ConfigTest    78 | Page 5 of 10 containing jstat.sample.Stock instances
[INFO ] | 20:39:35 | [mainhread] |           ConfigTest    80 | Id = 51
[INFO ] | 20:39:38 | [mainhread] |           ConfigTest    80 | Id = 52
[INFO ] | 20:39:38 | [mainhread] |           ConfigTest    80 | Id = 53
[INFO ] | 20:39:38 | [mainhread] |           ConfigTest    80 | Id = 54
[INFO ] | 20:39:38 | [mainhread] |           ConfigTest    80 | Id = 55
[INFO ] | 20:39:38 | [mainhread] |           ConfigTest    80 | Id = 56
[INFO ] | 20:39:38 | [mainhread] |           ConfigTest    80 | Id = 57
[INFO ] | 20:39:38 | [mainhread] |           ConfigTest    80 | Id = 58
[INFO ] | 20:39:38 | [mainhread] |           ConfigTest    80 | Id = 59
[INFO ] | 20:39:38 | [mainhread] |           ConfigTest    80 | Id = 60
[INFO ] | 20:39:38 | [mainhread] |           ConfigTest    81 | true
[INFO ] | 20:39:38 | [mainhread] |           ConfigTest    74 | Page request [number: 6, size 10, sort: StockId: ASC]

1 个答案:

答案 0 :(得分:1)

事实证明,带有spring-data-jpa-1.4.1.RELEASE的hibernate-core-3.6.10.Final导致了这个问题。我改为hibernate-core-4.2.7.Final,问题自动消失。