springboot do not use the database - spring-boot

I have a springboot project, and I will get the informations if the application.properties is empty.
Description:
Cannot determine embedded database driver class for database type NONE
Action:
If you want an embedded database please put a supported one on the
classpath. If you have database settings to be loaded from a
particular profile you may need to active it (no profiles are
currently active).
and I configuration datasource in the application.properties,it can run of success.
But I don't want to use the datasource,
How can I successfully run

You need to include a dependency in your project. If maven, add this to use in memory database.
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>

Related

Admin client not registering to spring boot admin server

I have created personal microservice and registered it to spring boot admin. Its working fine. But when I applied same dependency and properties in my organisation project then admin clients are not registering to spring boot admin server.
Even though both have same properties and dependencies, why my microservice is not registering
I tried creating spring boot admin server and trying to register admin clients on this server.
I am currently working on a small project where I'm connecting MSs to a spring boot admin server. Had some problems (my clients would not connect to the server) and that's how I ended up here.
From what you provided, it is very difficult to pinpoint the exact problem, so there will be a lot of guessing on my part. I can only share my experience in hope it can help you too. In the end, the problem for me was the configuration parameter
spring.boot.admin.client.url=...
which was misspelled... And since this will not raise any error messages, it took some time to fix.
Go through following steps:
add client dependencies:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
guessing you are using actuator, make sure there is a dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
expose actuator endpoints (probably not all like in my example) in application.properties
management.endpoints.web.exposure.include=*
point your MS client to the admin server (use correct url!)
spring.boot.admin.client.url=http://localhost:9080
make sure your admin server has following dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
make sure your admin server has the following annotation in the main class
#EnableAdminServer
make sure both server and client(s) are running correctly and can bind defined ports (change the default port if they run on the same machine - in the application.properties)
server.port=...
If there is no additional security level, these steps should be enough to connect client to the server. With security I have no experience, but the online documentation did seem straight forward.
In the end, you can always try to build a new MS with nothing except actuator and client, and try to connect it to the server. If nothing else, it will maybe provide insight which side is broken.
Without more details it is very difficult to provide more help.
Oh yeah, I do remember one more detail: I was mixing up versions of the spring boot and JDK and since JDK17 (and spring boot version >= 3.x) moved from javax to jakarta, had some problems with running the MS. But those errors were easy to fix from log info.

How to simply activate redis

I thought that for the design of a mvc project in spring boot it was possible to add later and simply redis to the project.
I have been trying for a few days to add redis to my project and I am having difficulties.
I added this in my pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
I added this in main
#Cacheable
and I have this error message:
Spring Data Redis - Could not safely identify store assignment for repository candidate interface *. If you want this repository to be a Redis repository, consider annotating your entities with one of these annotations: org.springframework.data.redis.core.RedisHash (preferred), or consider extending one of the following types with your repository: org.springframework.data.keyvalue.repository.KeyValueRepository.
then following the explanation on stackoverflow I added:
#spring.data.redis.repositories.type=none
#spring.data.ldap.repositories.enabled=false
spring.main.allow-bean-definition-overriding=true
but I have this message:
Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost/<unresolved>:6379
So yes it can't connect but I thought that by adding #Cacheable everything would be configured automatically ?!
I am clearly lost I thought it was that simple!
Do you have a good link so that I can set up redis on my project? thank you
thank you so much

Using H2 in memory AND H2 file within same application

Is it possible to use essentially two H2 databases within the same application: in-memory for some types of data and file storage for other data?
My application uses spring boot/batch. Spring automatically stores some metadata in H2. I have no control over this and generally don't care about persisting it - which is why I want it stored in memory.
However, I also have application specific data that I wanted persisted - which I want I want to store it in a file.
Is this possible?
You want to define multiple datasources & assign one of them as
primary.
Connect Spring Boot application with H2 : In most situations, just adding the H2 runtime jar into dependencies should be sufficient.
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
A suggested approach to make the configuration easy would be to create separate configuration classes, thus separating the repository packages etc.
Minimal configuration required to run H2 as a persisted database with Spring Boot :
Update you application.properties as :
# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2
# Datasource
spring.datasource.url=jdbc:h2:file:~/spring-boot-h2-db
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=update
Please got through the docs for better understanding : Configure Two Data Sources

Spring Boot: Do I have to add a jdbc pool dependency for Oracle connection?

First off I'm new to Spring Boot, so perhaps there is something simple I'm missing.
I have a small Spring Batch process that relies upon Spring Boot.
By default it uses the embedded H2 database.
I want it to use an oracle database.
So I set the url/username/password in the application.properties file
spring.datasource.url=jdbc:oracle:thin:#host:1521:batch
spring.datasource.username=username
spring.datasource.password=reallyCoolPassword
and add the dependecy to my Maven pom
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>${oracle.client.version}</version>
</dependency>
I still get the embedded database. The only way I've been able to make it work is to add a dependency to a connection pool (for example tomcat).
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
Am I missing something? I was expecting Spring Boot to already have a dependency to tomcat that it would have brought in. Allowing me to override with dbcp or something else if needed.
hopefully someone can tell me what I've done wrong or help me straighten out my thinking.
In addition to providing url, name and password for DB you need to add spring.datasource.driverClassName.
We are using mysql as storage and we are adding this in properties:
spring.datasource.driverClassName: com.mysql.jdbc.Driver
Here is also link to properties for spring batch using oracle db.
Also you need to add your driver to project and oracle due to licence restrictions does not have public maven repository so you need to install driver. Here is useful link with walk through.

Deploying Simple Spring Hibernate Utility Project via Maven to Tomcat

My goal is understanding the J2EE lifecycle at a high-level with Spring, Hibernate, and Maven. From much research, I understand that Spring provides dependency injection and Hibernate provides object-relation mapping with databases. Maven is a tool to improve the build/deployment process from my understanding. With that said, everywhere I search I get more and more lost on configuration files (i.e. pom.xml, server.xml, etc.), terminology, and alternatives such as Gradle. I just want to build and launch the application and be able to see via http://localhost:8080 in tomcat.
At first, I couldn't get the default project (picture attached) built, but after further research found that I needed to Maven clean and Maven install.
I also modified settings in pom.xml changing version numbers and the database to use MySQL.
<properties>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
<spring.framework.version>3.1.1.RELEASE</spring.framework.version>
</properties>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.1.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
The next issue I had was in OrderPersistenceTests.java it used org.hibernate.classic.Session which is no longer the correct class path and found that it needed to be org.hibernate.Session.
Finally, I was able to get it to build but cannot figure out how to deploy to Tomcat from Spring Tool Suite.
I have put together a simple example using Maven, Spring, Hibernate, and ExtJS for the front end at the following link:
https://github.com/drembert/java-webapp-sample
If you are using Spring Source Tool Suite which it looks like you are in the screen shot, you should be able to import everything using the "Import Maven Projects" option. The example uses Hypersonic as the in-memory DB to allow easier deployment. Keep in mind this example generates two different .war files (one is presentation-layer and the other is service-layer) to emulate a simple RESTful service so both will need to be deployed to the Spring tcServer (STS's version of Tomcat), but once they are you should be able to view the GUI at http://localhost:8080/presentation-layer. Another thing to make note of is that this example currently doesn't have a security layer which would normally be implemented using Spring security, but I am working on adding that in the near future.

Resources