使用 H2 和 HikariCP - Table 未找到
Using H2 and HikariCP - Table Not Found
我正在尝试使用 HikariCP 和 H2 以及 rxjava-jdbc 进行测试。我的代码在连接到真实数据库时工作正常,但是当切换到 H2 时,我似乎无法让 table 保持不变,即使我没有在内存中进行操作。
代码:
@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: Table "Bids" 未找到; SQL声明:(略);
编辑:
通过这个线程,但无法找到解决方案:
H2 in-memory database. Table not found
这段代码的问题不是h2的设置。 H2 正常运行,这是我对 rxjava-jdbc 代码的误解 - create/insert 语句在 运行 它们的末尾需要一个 .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();
我正在尝试使用 HikariCP 和 H2 以及 rxjava-jdbc 进行测试。我的代码在连接到真实数据库时工作正常,但是当切换到 H2 时,我似乎无法让 table 保持不变,即使我没有在内存中进行操作。
代码:
@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: Table "Bids" 未找到; SQL声明:(略);
编辑: 通过这个线程,但无法找到解决方案: H2 in-memory database. Table not found
这段代码的问题不是h2的设置。 H2 正常运行,这是我对 rxjava-jdbc 代码的误解 - create/insert 语句在 运行 它们的末尾需要一个 .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();