H2 Database not found with message it's available [duplicate] - spring

This question already has answers here:
Spring Boot default H2 jdbc connection (and H2 console)
(14 answers)
Closed 7 months ago.
I cannot login to my H2 Database event though i see the message that the database is available.
DEBUG [main] [Log Context: ] org.springframework.jdbc.datasource.DriverManagerDataSource:134 Loaded JDBC driver: org.h2.Driver
DEBUG [main] [Log Context: ] org.springframework.jdbc.datasource.DriverManagerDataSource:144 Creating new JDBC DriverManager Connection to [jdbc:h2:mem:testdb]
INFO [main] [Log Context: ] o.s.boot.autoconfigure.h2.H2ConsoleAutoConfiguration:68 H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testdb'
My application.properties file looks like this:
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
and the database not found error messsage looks like this:
I am using h2database version 2.1.214 and spring boot version 2.5.9

at first try to add next properties to the Data Base URL:
DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
Example:
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE

Related

Unable to connect non stop SQL from Spring boot applivcation

My project uses non stop SQL/MX as RDBMS database, a product from HP.I am not able to connect to the data source using Spring Boot's standard practice of defining JDBC URL, user, password inside application.properties file.
spring.datasource.driverClassName = com.tandem.sqlmx.SQLMXDriver
spring.datasource.url = jdbc:sqlmx:
spring.datasource.username=
spring.datasource.password=
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SqlmxDialect
spring.datasource.hikari.connection-test-query=SELECT 1 FROM $USER1.TLFM3SQL.IF09CSTB
This is the error:
com.zaxxer.hikari.pool.PoolBase : HikariPool-1 - Driver does not support get/set network timeout for
connections. (com.tandem.sqlmx.SQLMXConnection.getNetworkTimeout()I)
JdbcEnvironmentInitiator: could not obtain connection to query metadata: Unable to resolve name
[org.hibernate.dialect.SqlmxDialect ] as strategy [org.hibernate.dialect.Dialect]
I had not included SQLMX hibernate jar in project.
It worked after I included that jar in my pom.

Spring Boot Test seems to be creating H2 Test DB different than what I would expect

So, I have a Test annotated with #DataJpaTest and #RunWith(SpringRunner.class), and an application.yml under /src/test/resources with this block (yes, indenting should be fine):
spring:
datasource:
url: jdbc:h2:mem:foobar;MODE=Mysql;MVCC=FALSE;
username: sa
password:
driver-class-name: org.h2.Driver
When I start the Test, I unexpectedly get these lines in the log:
2019-10-23 17:11:08.311 INFO 13468 --- [ main] beddedDataSourceBeanFactoryPostProcessor : Replacing 'dataSource' DataSource bean with embedded version
2019-10-23 17:11:08.801 INFO 13468 --- [ main] o.s.j.d.e.EmbeddedDatabaseFactory : Starting embedded database: url='jdbc:h2:mem:7855270f-61b7-4f37-8796-cbfeb8ad42ea;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
In particular this: Starting embedded database: url='jdbc:h2:mem:7855270f-61b7-4f37-8796-cbfeb8ad42ea;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false
Why is Spring boot starting a DB with a UUID-Generated DB and not taking the settings from spring.datasource.url?
The "productive" app takes the datasource settings fine from the file in /src/main/resources with same syntax without issues...
From the documentation of #DataJpaTest you can see that:
#DataJpaTest uses an embedded in-memory
database (replacing any explicit or usually auto-configured
DataSource). The #AutoConfigureTestDatabase annotation can be used to
override these settings.
So #DataJpaTest annotated with #AutoConfigureTestDatabase that causes TestDatabaseAutoConfiguration to create embedded datasource with hard-coded generateUniqueName(true):
TestDatabaseAutoConfiguration.java :
EmbeddedDatabase getEmbeddedDatabase() {
...
return new EmbeddedDatabaseBuilder()
.generateUniqueName(true)
.setType(connection.getType())
.build();
}
I think they do this to prevent DB name collisions and state mix between test runs.

Spring boot 2.1.0 Hikari CP bad password

So I updated my Spring Boot to 2.1.0, and now hikari is the default CP. According to some SO questions related to this I no longer needed to use the .hikari in my application.properties file. My properties file now looks like this:
# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2
# Datasource
spring.datasource.url=jdbc:h2:file:~/deployHistory/deployHistory
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
Now if I use the driver manager directly, like this:
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection(databaseUrl);
Statement stat = conn.createStatement();
stat.execute("create table ...)");
stat.close();
conn.close();
It all works fine, However, when I use the Spring Boot JDBC template, and do a simple:
jdbcTemplate.update(...);
I get an error:
2018-11-26 14:27:54.772 INFO 7349 --- [nio-8080-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2018-11-26 14:27:56.059 ERROR 7349 --- [nio-8080-exec-1] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
org.h2.jdbc.JdbcSQLException: Wrong user name or password [28000-197]
What is the way to fix this? All the pre-2.0.4 answers don't seem to work.

Import schema.sql into a specificic H2 database with Spring Boot

I'm struggling to understand if I can import the data defined inside my schema.sql and data.sql into a specific H2 database.
I use Spring Boot 2.0.6 and this is the configuration inside my application.properties:
# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2
# Datasource
spring.datasource.url=jdbc:h2:mem:assignment
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
And I correctly placed both schema.sql and data.sql inside the resources folder.
The application starts up correctly and imports the files:
2018-10-26 20:39:24.834 INFO 9448 --- [main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from URL [file:/C:/Users/Gabriele/code/java/assignment/target/classes/schema.sql]
2018-10-26 20:39:24.848 INFO 9448 --- [main] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from URL [file:/C:/Users/Gabriele/code/java/assignment/target/classes/schema.sql] in 13 ms.
2018-10-26 20:39:24.850 INFO 9448 --- [main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from URL [file:/C:/Users/Gabriele/code/java/assignment/target/classes/data.sql]
2018-10-26 20:39:24.868 INFO 9448 --- [main] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from URL [file:/C:/Users/Gabriele/code/java/assignment/target/classes/data.sql] in 18 ms.
But when I open the browser console, the assignment database is empty
while the test one is not.
What am I missing?

Using HikariCP as connection pool with GemfireXD in a Spring boot application throws ClassCastException

I am trying to use HikariCP with Gemfire XD in a Spring boot application.I am getting below ClassCastException:`
10:29:04.757 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Started.
10:29:04.791 [main] WARN c.z.hikari.util.DriverDataSource - Registered driver with driverClassName=com.pivotal.gemfirexd.internal.jdbc.ClientConnectionPoolDataSource was not found, trying direct instantiation.
10:29:04.794 [main] WARN c.z.hikari.util.DriverDataSource - Failed to create instance of driver class com.pivotal.gemfirexd.internal.jdbc.ClientConnectionPoolDataSource, trying jdbcUrl resolution
java.lang.ClassCastException: com.pivotal.gemfirexd.internal.jdbc.ClientConnectionPoolDataSource cannot be cast to java.sql.Driver
at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:71)
at com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:298)
at com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:91)
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:101)
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:94)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:615)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:658)
I have specified below configurations in application.properties file:
spring.datasource.url=jdbc:gemfirexd://192.168.162.141:1527/
spring.datasource.username=APP
spring.datasource.password=APP
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driver-class-name=com.pivotal.gemfirexd.internal.jdbc.ClientConnectionPoolDataSource
spring.datasource.schema=APP
spring.datasource.transaction-isolation=0
spring.datasource.auto-commit=true`**
And as per the BlogPost I have tried changing to
spring.datasource.driver-class-name=com.pivotal.gemfirexd.internal.jdbc.ClientDataSource
But still it does not work and throws same exception.
Gemfire XD Version:1.4.1
HikariCP Version:2.4.6
Spring boot Version:1.3.5
Spring JDBC version:4.2.6
Java Version :1.7

Resources