I am creating H2 db in file mode during my Spring-boot application start-up. So is that possible to connect to same db file while running my Serrenity BDD integration API tests?
I took the mixed mode approach specified here Automatic Mixed Mode
application.yaml
spring:
cache:
enabled: false
sql:
init:
mode: always
datasource:
url: jdbc:h2:./../myApp/data/ppyDB;AUTO_SERVER=TRUE;MODE=Oracle
username: sa
password: sa
baseline-on-migrate: true
driverClassName: org.h2.Driver
jpa:
defer-datasource-initialization: false
show-sql: true
hibernate:
ddl-auto: none
flyway:
enabled: true
username: sa
password: sa
url: jdbc:h2:./../myApp/data/ppyDB;AUTO_SERVER=TRUE;MODE=Oracle
locations: classpath:/db/h2/migration
h2:
console:
enabled: true
path: /h2-console
After the test run I can see new records in the DB
This is the code to connect to same DB during test validation
sql = "SELECT * FROM HEADER";
try{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:./../myApp/data/ppyDB;AUTO_SERVER=TRUE;MODE=Oracle");
dataSource.setUsername("sa");
dataSource.setPassword("sa");
conn = dataSource.getConnection();
String metadataValue = "";
String strMetadata = "test_code";
statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
metadataValue = strMetadata.isEmpty() || strMetadata.isBlank() ? rs.getString(0) : rs.getString(strMetadata);
}
statement.close();
conn.close();
Assert.assertEquals(key, "header.approve", metadataValue);
DB file actual path : C:Projects\myApp\data\myDB.mv.db
SerenityBDD integration tests feature files path : C:Projects\myApp\api\src\it\resources\
SerenityBDD integration tests stepDef path : C:Projects\myApp\api\src\it\java\stepdefs
where the above db access code reside
Main App path: C:Projects\myApp\api\src\main
However I can't connect to db using server mode via above test code.
I am connecting to same H2 file in same machine via two connections, if that make sense.
Current Error:
org.h2.jdbc.JdbcSQLNonTransientConnectionException: Connection is broken: "java.net.SocketTimeoutException: Connect timed out: 172.28.308.1:32632" [90067-214] at org.h2.message.DbException.getJdbcSQLException(DbException.java:678) at org.h2.message.DbException.getJdbcSQLException(DbException.java:477)
The myDB.lock.db
#FileLock
#Wed Apr 12 12:39:46 AEST 2023
server=172.28.308.1\:32632
hostName=AULT-D64WVP99.mshome.net
method=file
id=18773536407803de631be7d5c6a327adf2b4a815286
I can connect in two windows to DB in embeded mode with this url jdbc:h2:./../myApp/data/ppyDB;AUTO_SERVER=TRUE;MODE=Oracle
So question here despite be able to connect in two H2 console, why NOT via code? Further to my knowledge and as per docs no need to specify port or IP address (in the lock file) when do a new connection.
Please clarify.