简体   繁体   中英

Populate h2 in memory database from a db2 database Spring Boot

I'm currently building a Spring Boot Service with a h2 in-memory database. This Database acts as an cache for a part of the data on a central db2 database with a different database schema.

Now when the Spring boot service starts it needs to populate the h2 database with the latest data from the central database.

How can I do this in the best way performance wise? I'm currently looking to create an different data-source in my service to first get the data and then save the data to the h2.

This doesn't feel like a good solution and it would take quite a long time to populate the database.

If you want to use H2 instead of your DB2 database... and if you don't want to re-create the database each time you run your app...

... then consider using an H2 file, instead of in-memory:

http://www.h2database.com/html/features.html

 jdbc:h2:[file:][<path>]<databaseName> jdbc:h2:~/test jdbc:h2:file:/data/sample jdbc:h2:file:C:/data/sample (Windows only)

You can "initialize" the file whenever you want (perhaps just once).

Performance should be excellent.


Per your update:

I still need to access the central db to get the latest data in the fastest way possible. The central db needs to stay for other services also accessing this

The "fastest" way to get the very latest data... is to query the central db directly. Period - no ifs/ands/buts.

But, if for whatever reason, you want to "cache" a subset of "recent" data... then H2 is an excellent choice.

And if you don't want to "rebuild" each time you start your H2 database, then save H2 to a file instead of making it in-memory.

The performance difference between H2:mem and H2:file is small, compared to the network overhead of querying your central db.

'Hope that helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM