使用H2和HikariCP - 找不到表

时间:2017-04-13 23:31:59

标签: spring-boot rx-java h2 hikaricp

我正在尝试使用HikariCP和H2以及rxjava-jdbc进行测试。我的代码在连接到真实数据库时工作正常,但是当切换到H2时,我似乎无法让表格坚持下去,即使我没有在内存中进行操作。

代码:

@RunWith(SpringRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class)
public class BidsRepositoryTest {

    private static final Logger log = LoggerFactory.getLogger(BidsRepositoryTest.class);

    @Configuration
    static class ContextConfiguration {

        // this bean will be injected into the OrderServiceTest class
        @Bean
        public BidsRepository bidsRepository() {
            BidsRepository bidsRepository = new BidsRepositoryImpl();
            // set properties, etc.
            return bidsRepository;
        }

        @Bean(destroyMethod = "close")
        public DataSource dataSource() throws SQLException {
            HikariConfig config = new HikariConfig();
            config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");
            config.setConnectionTestQuery("VALUES 1");
            config.addDataSourceProperty("URL", "jdbc:h2:~/testdb;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MSSQLServer");
            config.addDataSourceProperty("user", "sa");
            config.addDataSourceProperty("password", "sa");
            HikariDataSource ds = new HikariDataSource(config);

            HikariDataSource dataSource = new HikariDataSource(config);

            return dataSource;
        }

        @Bean
        public ConnectionProvider connectionProvider(){
            return new CustomConnectionProvider();
        }
    }

    @Autowired
    private BidsRepository bidsRepository;

    @Autowired
    ConnectionProvider connectionProvider;


    @Test
    public void shouldSelectEntityForMatchingIds(){

        int userId = 1012;
        int listingId = 2;

        Database db = Database.builder().connectionProvider(connectionProvider).build();
        db.update("create table Bids\n" +
                "(\n" +
                "\tID int not null,\n" +
                "\tUserID int not null\n" +
                "\tAmount money not null,\n" +
                "\tParticipation money default 0 not null,\n" +
                "\tMinRate decimal(6,3) not null,\n" +
                "\tListingID int not null\n" +
                "\tStandingBidID int default (-1) not null,\n" +
                "\tFundingAccountID int not null\n" +
                "\tCreationDate datetime default getdate() not null,\n" +
                "\tModifiedDate datetime default getdate() not null,\n" +
                "\tCollectionAgencyID int\n" +
                "\tWithdrawn bit default 0 not null,\n" +
                "\tAnonymous bit default 0 not null,\n" +
                "\tAltKey varchar(30) default substring(convert(varchar50,newid()),1,4) + convert(varchar50,(convert(bigint,getdate()) * 86400000 + datepart(hour,getdate()) * 3600000 + datepart(minute,getdate()) * 60000 + datepart(second,getdate()) * 1000 + datepart(millisecond,getdate()) + abs(checksum(newid())))) + substring(convert(varchar50,newid()),30,6) not null,\n" +
                "\tAltKeySearchID as checksum([AltKey]),\n" +
                "\tBidSourceID tinyint not null\n" +
                "\tMinYield decimal(6,3) not null,\n" +
                "\tInvestmentOrderID int\n" +
                "\tconstraint PK_Bids\n" +
                "\t\tprimary key (ID, ListingID)\n" +
                ") go");


        db.update("INSERT INTO Bids (ID, UserId, Amount, Participation, MinRate, ListingId, BidSourceId, MinYield) "
                + " VALUES(1, 1012, 10000, 3000, 0.06, 2, 2, 12000) go");


        bidsRepository.getBid(userId, listingId).forEach( a-> {
                    assertThat(a.getUserId()).isEqualTo(userId);
                    assertThat(a.getListingId()).isEqualTo(listingId);
                }
        );
    }
}

例外: rx.exceptions.OnErrorNotImplementedException:未找到表“出价”; SQL语句:(省略);

编辑: 经过这个线程,但无法找到解决方案: H2 in-memory database. Table not found

1 个答案:

答案 0 :(得分:0)

此代码的问题不在于h2的设置。 H2正在按预期执行,这是我对rxjava-jdbc代码的误解 - 创建/插入语句需要一个.execute()来运行它们。

Database db = Database.builder().connectionProvider(connectionProvider).build();
        db.update("create table Bids\n" +
                "(\n" +
                "\tID int not null,\n" +
                "\tUserID int not null,\n" +
                "\tAmount decimal not null,\n" +
                "\tParticipation decimal default 0 not null,\n" +
                "\tMinRate decimal(6,3) not null,\n" +
                "\tListingID int not null,\n" +
                "\tStandingBidID int default (-1) not null,\n" +
                "\tFundingAccountID int not null,\n" +
                "\tCreationDate datetime default getdate() not null,\n" +
                "\tModifiedDate datetime default getdate() not null,\n" +
                "\tCollectionAgencyID int,\n" +
                "\tWithdrawn bit default 0 not null,\n" +
                "\tAnonymous bit default 0 not null,\n" +
                "\tAltKey varchar(30) default 'ABC123' not null,\n" +
                "\tAltKeySearchID  VARCHAR(30),\n" +
                "\tBidSourceID tinyint not null,\n" +
                "\tMinYield decimal(6,3) not null,\n" +
                "\tInvestmentOrderID int\n" +
                ")").execute();