Using H2 in memory AND H2 file within same application - spring-boot

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

Related

Can I use Spring Data JPA with PostgreSQL?

I would like to be able to use a PostgreSQL database using Spring Data JPA. Can I do this?
To clarify, I'd like to be able to use things just as the CrudRepository or JpaRepository, just instead of hooking to an H2 in-memory database, it hooks into a PostgreSQL database.
I am following this tutorial, which uses an H2 database: https://spring.io/guides/tutorials/rest/
Thanks for any help!
Yes, ofcourse.
You've to perform multiple steps
Add postgresql dependency
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
Configure database details in application.properties or application.yml like below;
spring.datasource.url= jdbc:postgresql://<HOST>:<PORT>/<DATABASE>
spring.jpa.database=POSTGRESQL
spring.datasource.username=<USERNAME>
spring.datasource.password=<PASSWORD>
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL94Dialect #Or any other dialect if you're using updated.
Replace <> with your postgres details

Spring Boot JPA with not a well known database

I am trying to write Spring Boot application to connect to a Teiid database, I want to use JPA layer on it. I have configured the JDBC Data Source, but since this not well-known database in Spring JPA libraries do not autodetect this source. I have manually setup "spring.jpa.*" properties too. I do have a Hibernate dialect for this database, and it is on the classpath.
So, how does one need to configure JPA layer for a not well-known database in Spring Boot? Thank you for your time.
Ramesh..
This is fairly well defined in the Spring Boot documentation.
You can set this explicitly in the application.properties file
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

springboot do not use the database

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>

Spring JPA PostgreSQL + MongoDB

Starting from the Spring example Accessing MongoDB Data with REST (https://spring.io/guides/gs/accessing-mongodb-data-rest/) I'd like to integrate a PostgreSQL data source and link it to the MongoDB repository.By switching from MongoRepository to JpaRepository and accordingly changing the application.properties file I've been able to pass from MongoDB to PostgreSQL and viceversa, but basically having only one active data source at time.
application.properties when using MongoDB
spring.data.mongodb.port=27017
spring.data.mongodb.uri=mongodb://localhost/
spring.data.mongodb.database=myMongoDB_DB
spring.data.mongodb.repositories.enabled=true
application.properties when using PostgreSQL
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/myPostgreSQL_DB
spring.datasource.username=me
spring.datasource.password=mySuperSecretPassword
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
Is there a way to configure Spring (with an Annotation-only way) to link two data sources to the same Repository so that when I access my REST web service via HTTP both MongoDB and PostgreSQL are changed in exactly the same way?I googled around and found something about Spring cross-store support (http://docs.spring.io/spring-data/mongodb/docs/1.5.5.RELEASE/reference/html/mongo.cross.store.html) but it uses xml for the application configuration and AspectJ, is there a simpler way to accomplish this?
In this chapter you can find an answer - (Spring-boot manual - Use Spring Data JPA and Mongo repositories)

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.

Resources