I want to save the data exactly in in-memory mode using h2
as database.
So I have application.properties
file with configurations like:
spring.h2.console.enabled=true
spring.jpa.show-sql=true
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=...
spring.datasource.password=...
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.session.jdbc.initialize-schema=always
using specific parameters:
DB_CLOSE_DELAY=-1
- to keep the database open/to keep the content of an in-memory database as long as the virtual machine is alive
DB_CLOSE_ON_EXIT=FALSE
- to disable database closing on exit
as mentioned in documentation of H2 database.
At the same time, I'm using Entity
for saving in database:
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity(name = "address_entity")
@Table(name = "addresses")
public class GeolocationAddress {
public GeolocationAddress(GeolocationAddressDTO geolocationAddressDTO) {
this.displayName = geolocationAddressDTO.getDisplayName();
this.lat = geolocationAddressDTO.getLat();
this.lon = geolocationAddressDTO.getLon();
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "address_id")
private Integer id;
private String displayName;
private String lat;
private String lon;
}
After sending the request, I'm getting in console information from Hibernate
:
Hibernate: insert into addresses (address_id, display_name, lat, lon) values (null, ?, ?, ?)
Hibernate: insert into addresses (address_id, display_name, lat, lon) values (null, ?, ?, ?)
but after refreshing H2
database in Data Source
using Ctrl+F5 combination or via:
key, I don't see there any data.
To research this behavior, I've checked formulations in documentation:
"For certain use cases (for example: rapid prototyping, testing, high performance operations, read-only databases), it may not be required to persist data, or persist changes to the data. This database supports the in-memory mode, where the data is not persisted."
and from some articles, e.g.:
"By design, the in-memory database is volatile, and data will be lost when we restart the application."
or this one:
"H2 is an in memory database. Its not a persisted database. H2 is a great tool for learning because you need zero setup."
but I still don't fully understand, can I save data in this mode while the application is running?
if I change to another mode, e.g.:
spring.datasource.url=jdbc:h2:file:./data/demo;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE
Then, yes, I see data in database after refreshing and data will be saved in format like:
but can I do the same with in-memory
or not?
I would be grateful for any clarification on this.
UPD #1:
For instance, it's indicated here:
"Most in-memory database systems offer persistence, at least as an option."
UPD #2:
From H2-console
I see the data using in-memory
mode, but from Intellij Idea
- not:
UPD #3:
As mentioned @Turing85, I've tested url: jdbc:h2:tcp://localhost/mem:db1
to access the database over TCP/IP
or TLS
, but this link was invalid in Data Source
.
After some investigation, I've found useful information:
":mem will not work for TCP connections. so remove :mem from connection url"
So alternative way like: jdbc:h2:./data/testdb;AUTO_SERVER=TRUE;AUTO_SERVER_PORT=10990
also works.
UPD #4: Probably, this answer also is related to my question.