Spring 5 constantly losing connection to the DB - java

I know this question was asked a lot and there is ton of articles about it, but none of the solutions seem to work for me, I'm new to Spring so maybe I'm missing something.
I'm using spring 5.0.6 , spring boot 2.0.0, with hibernate, mysql and Hikari pool.
My app connects successfully and performing succesfull query and then losing connection. Here's related application config:
spring.datasource.url=jdbc:mysql://server/db?useSSL=false
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.testOnBorrow=true
spring.datasource.testWhileIdle = true
spring.datasource.timeBetweenEvictionRunsMillis = 20000
spring.datasource.validationQuery = SELECT 1
spring.jpa.show-sql = true
I've also tried setting config specific to hikari, still no luck.
spring.datasource.hikari.connection-timeout=3000
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=1
spring.datasource.hikari.connection-test-query=SELECT 1
Can anyone give a hint?

Related

Spring Boot: how to connect to external h2 database

I have created one Spring boot application and I am not using in-memory H2 database, instead I have installed exe for H2 database and using it externally. Now I want to connect my Spring boot app with this external H2 database. I have added the dependencies, I have added all the required properties in application.properties file (you can see below). Also I have created one Entity class having #Entity annotation. But when I am trying to connect with the database it is connecting even with different URL and username and I cannot see my Entity class table over there. So where am I doing a mistake and what are the things which I have missed, please address me on this.
For your information, my Spring boot app is running on port - 8080 and H2 database is running on 8082
spring.h2.driverClassName = org.h2.Driver
spring.h2.url = jdbc:h2:file:~/test2;DB_CLOSE_ON_EXIT=FALSE; AUTO_RECONNECT=TRUE
spring.h2.username = QW
spring.h2.password = root
spring.h2.console.enabled = true
spring.datasource.platform = h2
You can try this
spring.datasource.url=jdbc:h2:tcp://localhost:8082/~/<DB-NAME>
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driverClassName=org.h2.Driver
Give in properties file
# H2
spring.datasource.url=<your value
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Check this tutorial below link
https://howtodoinjava.com/spring-boot2/h2-database-example/
https://www.baeldung.com/spring-boot-h2-database

Spring Boot not loading username from application.properties

I'm developing a simple web application with spring boot. In the first step, I implemented everything about the page itself and a simple login. Additionally, I implemented a database access using mysql and hibernate. My database runs on a local easyphp devserver.
In the first step, everything worked fine. However, after a reset of the database and recreating the user with the same credentials, Hibernate is not able to get a connection:
2018-02-09 21:17:11.487 ERROR 9592 --- [ restartedMain] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user ''#'localhost' to database 'db'
As I can understand, no username was configured. But in my application.properties, a username is set:
spring.datasource.url=jdbc:mysql://localhost:3306/db?userSSL=false
spring.datasource.username=user
spring.datasource.password=
The exception occurres on startup, so it has to be the configuration. At the database, the user 'user' has all priviliges at the database 'db', but no one on others.
The data source URL might be causing the issue. Is that a typo? Can you check by resetting it to the following? It has to be "useSSL" instead of "userSSL"
spring.datasource.url=jdbc:mysql://localhost:3306/db?useSSL=false
spring.datasource.username=user
spring.datasource.password=
If the above does not work, your problem might be related to permissions for the user. A question posted here relates to yours (although it is in PHP).
Hopefully this resolves it, let us know.
Ok, found the solution by myself. The real username contained a '_' like 'user_name'. Somehow this character was the problem why the username was not read.
Edit: Worked a few times, but the problem still exists...

Spring-boot application, H2 embedded database - but tables disappear when the application closes

My database.properties file is:
datasource.driver=org.h2.Driver
datasource.url=jdbc:h2:file:./test_database/comixed_db;create=true
datasource.username=sa
datasource.password=
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.batch.size=20
hibernate.current.session.context.class=org.springframework.orm.hibernate5.SpringSessionContext
hibernate.generate_statistics=false
hibernate.enable_lazy_load_no_trans=false
When my application starts up I see I am able to see the tables by using the h2.bat tool and peeking at the database from a web browser. However, when I shut down the application and then go back to the database with the h2.bat tool the tables are all gone!
AM I doing something incorrectly with my hibernate configuration? I am not using create-drop but update since this code is currently in flux and I'd like the tables to be adjusted as changes occur. But that doesn't seem to be the issue since it's at app shutdown that the tables keep going away.
Any help would be appreciated.
If you want spring boot to catch your hibernate properties you should prefix them with spring.jpa, so:
spring.jpa.hibernate.ddl-auto=update
Otherwise, and that is the case in my opinion, spring will use the default create-drop options as it is dealing with an H2 in-memory database.
By adding the following line to applications.properties:
spring.jpa.hibernate.ddl-auto=update
Spring-boot stopped dropping tables when the application exits.

Spring data and oracle slow data load

I'm developing an Spring-boot app with spring-data and oracle database. I have a couple of pretty simple tables but load of the data is so slow.
For example, loading 2000 rows from table that only have 3 columns (texts) lasts almost 25 seconds. I was using the spring data before but not with spring boot and it was not that slow. I have only one resource file application.properties and here it is.
spring.datasource.url = jdbc:oracle:thin:#1.2.3.4:1521:abcd
spring.datasource.username = user
spring.datasource.password = ____
spring.datasource.driverClassName = oracle.jdbc.driver.OracleDriver
spring.datasource.maxActive=15
spring.datasource.initialSize=5
spring.datasource.validationQuery=select 1
spring.datasource.removeAbandoned=false
spring.datasource.testOnBorrow=true
spring.datasource.testOnReturn=true
spring.jpa.database-platform = org.hibernate.dialect.Oracle10gDialect
I've been trying with default repository implementation like findAll() as well as with custom #Query and it is the same.
Do I need some additional configuration? Any ideas?
In the configuration properties, you should use spring.jpa.properties.hibernate.jdbc.fetch_size

Spring Boot default H2 jdbc connection (and H2 console)

I am simply trying to see the H2 database content for an embedded H2 database which spring-boot creates when I don't specify anything in my application.properties and start with mvn spring:run. I can see hibernate JPA creating the tables but if I try to access the h2 console at the URL below the database has no tables.
http://localhost:8080/console/
I see suggestions like this one:
View content of embedded H2 database started by Spring
But I don't know where to put the suggested XML in spring-boot and even if I did, I don't want the h2console to be available anymore when an external database is configured so it is more likely that I need to handle this with some kind of conditional code (or maybe just allow spring to automatically handle it in the most ideal case where I only include H2 when a maven profile is activated).
Does anyone have some sample code showing how to get the H2 console working in boot (and also the way to find out what the jdbc connection string that spring is using is)?
This is how I got the H2 console working in spring-boot with H2. I am not sure if this is right but since no one else has offered a solution then I am going to suggest this is the best way to do it.
In my case, I chose a specific name for the database so that I would have something to enter when starting the H2 console (in this case, "AZ"). I think all of these are required though it seems like leaving out the spring.jpa.database-platform does not hurt anything.
In application.properties:
spring.datasource.url=jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
In Application.java (or some configuration):
#Bean
public ServletRegistrationBean h2servletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(new WebServlet());
registration.addUrlMappings("/console/*");
return registration;
}
Then you can access the H2 console at {server}/console/. Enter this as the JDBC URL: jdbc:h2:mem:AZ
As of Spring Boot 1.3.0.M3, the H2 console can be auto-configured.
The prerequisites are:
You are developing a web app
Spring Boot Dev Tools are enabled
H2 is on the classpath
Even if you don't use Spring Boot Dev Tools, you can still auto-configure the console by setting spring.h2.console.enabled to true
Check out this part of the documentation for all the details.
Note that when configuring in this way the console is accessible at: http://localhost:8080/h2-console/
I have found a nice tutorial about this topic:
https://springframework.guru/using-the-h2-database-console-in-spring-boot-with-spring-security/
Basically the correct JDBC URL for me was: jdbc:h2:mem:testdb
From http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
H2 Web Console (H2ConsoleProperties):
spring.h2.console.enabled=true //Enable the console.
spring.h2.console.path=/h2-console //Path at which the console will be available.
Adding the above two lines to my application.properties file was enough to access the H2 database web console, using the default username (sa) and password (empty, as in don't enter a password when the ui prompts you).
A similar answer with Step by Step guide.
Add Developer tools dependency to your pom.xml or build.gradle
Maven
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
Gradle
dependencies {
compile("org.springframework.boot:spring-boot-devtools")
}
Access the db from http://localhost:8080/h2-console/
Specify jdbc:h2:mem:testdb as JDBC URL
You should see the entity you specified in your project as a table.
For Spring Boot 2.1.1 straight from Spring Initialzr:
Default with devtools is http://127.0.0.1:8080/h2-console/
POM: spring-boot-starter, h2, spring-boot-starter-web, spring-boot-devtools
Without devtools - you need to set it in properties: spring.h2.console.enabled=true spring.h2.console.path=/h2-console
POM: spring-boot-starter, h2, spring-boot-starter-web
Once you get there - set JDBC URL: jdbc:h2:mem:testdb (The default one will not work)
I had only below properties in /resources/application.properties. After running spring boot, using this URL(http://localhost:8080/h2-console/), the table in H2 console was visible and read to view the table data, also you can run simple SQL commands. One thing, in your java code, while fetching data, the column names are upper-case, even though schema.sql is using lower-case names :)
spring.datasource.initialize=true
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=- 1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true
Check spring application.properties
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
here testdb is database defined
Make sure h2 console have same value while connecting other wise it will connect to default db
For Spring Boot 2.3.3.RELEASE straight from Spring Initialzr:
POM: data jpa, h2, web
application properties: spring.h2.console.enabled=true
When you run the application look for line like below in the run console:
2020-08-18 21:12:32.664 INFO 63256 --- [ main] o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:eaa9d6da-aa2e-4ad3-9e5b-2b60eb2fcbc5'
Now use the above JDBC URL for h2-console and click on Connect.
If you use Spring Boot's developer tools, it comes with H2 Console enabled by default. It can be accessed from /h2-console/. On the login interface, for input JDBC URL use value jdbc:h2:mem:testdb. Pay attention to mem string.
If you don't use Spring Boot's developer tools, you can enable the console in application.properties using spring.h2.console.enabled=true. This will enable console under /h2-console. If you want to change the URL then you can add another entry with spring.h2.console.path=my_console_path.
The default schema name is testdb.
More details in Spring Boot Documentation.
In order to get the tables all you need to do is create 2 sql files schema.sql(for table creation) and data.sql(data for the created tables). These files to be put in src/main/resources folder. Spring boot auto detects them and takes care of the rest during runtime.
If your using more than 2 DB in your project ensure to use specific files like (schema-h2.sql -- for h2 DB , schema-oracle.sql -- for oracle DB). The same to be followed for data.sql too.
Also ensure that you drop tables by adding drop table statement in your schema.sql as first statement. To avoid appending of duplicate records.
The link for spring boot is here.
My application.properties is as follows.
spring.datasource.url=jdbc:h2:~/file/Shiva;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.platform=h2
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.datasource.initialize=true
spring.error.whitelabel.enabled=true
spring.h2.console.path=/console
spring.datasource.continue-on-error=true
spring.jpa.hibernate.ddl-auto=create
spring.hibernate.hbm2ddl.auto=update
spring.hibernate.show_sql=true
You can follow the steps in the below link.
https://springframework.guru/using-the-h2-database-console-in-spring-boot-with-spring-security/
I found that with spring boot 2.0.2.RELEASE, configuring spring-boot-starter-data-jpa and com.h2database in the POM file is not just enough to have H2 console working. You must configure spring-boot-devtools as below.
Optionally you could follow the instruction from Aaron Zeckoski in this post
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
Use jdbc:h2:mem:testdb as your path when logging into the H2 console.
Obviously if you have altered Spring Boot properties your datasource may be different, but it seems like you're struggling with how to find the default. That's all there is to it! You'll see your schema after logging in to H2.
I had made a very stupid mistake when I had this same problem. I had added H2 DB for running unit test cases and hence I had set the scope to test in pom.xml. While running the application using mvn spring:run I removed the scope and it works fine now.

Categories

Resources