H2 Database multiple connections - h2

I have the following issue:
Two instances of an application on two different systems should share a small database.
The main problem is that both systems can only exchange data through a network-folder.
I don't have the possibilty to setup a database-server somewhere.
Is it possible to place a H2 database on the network-folder and let both instances connect to the database (also concurrently)?
I could connect with both instances to the db using the embedded mode if I disable the file-locking, right?
The instances can perfom either READ or INSERT operations on the db. Do I risk data corruptions using multiple concurrent embedded connections?

As the documentation says; ( http://h2database.com/html/features.html#auto_mixed_mode
)
Multiple processes can access the same database without having to start the server manually. To do that, append ;AUTO_SERVER=TRUE to the database URL. You can use the same database URL independent of whether the database is already open or not. This feature doesn't work with in-memory databases.
// Application 1:
DriverManager.getConnection("jdbc:h2:/data/test;AUTO_SERVER=TRUE");
// Application 2:
DriverManager.getConnection("jdbc:h2:/data/test;AUTO_SERVER=TRUE");

From H2 documentation:
It is also possible to open the database without file locking; in this
case it is up to the application to protect the database files.
Failing to do so will result in a corrupted database.
I think that if your application use always the same configuration (shared file database on network folder), you need to create an application layer that manages concurrency

Related

Elixir/Phoenix - Connect to external database

I connected the main database in the dev.exs and it works fine. But in my project I plan to use several databases. I know that in the file dev.exs can connect multiple databases but this option doesn’t suit me. Databases connections will be stored in the main project database. I want to know: how can I connect to different databases using the elixir code without using a file dev.exs?
You can start multiple instances of your Repo with different connection options.
Then, use the Repo.put_dynamic_repo/1 function to tell the Repo which of the databases should be used for queries in the current process. (The documentation for this function also tells you how to start more of the same repo).
There's also a discussion document that goes more in-depth about this topic: https://hexdocs.pm/ecto/replicas-and-dynamic-repositories.html

Can't find tables and data of my H2 database

A webapp is using following configuration to store some data in DB:
spring.datasource.continueOnError=true
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:mydb
I was trying to reach this DB using H2 Console, but something is wrong. There are no tables even though I know they were created. I can also access any made up JDBC:URL like jdbc:h2:mem:fakeXYZ just as well with same result. What is goning on here? How can I see this DB tables and data?
Your JDBC URL specifies a named in-memory database which by default only will be available for connections from within the same virtual machine.
You can either change the URL to use a file-based database, or you will have to start a TCP server to allow for connections from other processes.

H2 database: not able to copy multiple tables simultaneously from Oracle to H2 using Java concurrency

I am trying to copy 30 tables from Oracle 11g database to H2 database during a process in my Java application. None of the tables are related.
To speed up the process I am creating 30 threads for each table and try to copy the tables simultaneously. I am able to start all threads, but as soon as one of thread start to execute the query, all other threads that hit the H2 db, went into monitor state from running state.
Is it not possible with H2 database to copy multiple table simultaneously or am I doing something wrong and need any special configuration while creating connection
Do anyone has any workaround for this problem.
Is it at least possible to read data from different tables simultaneously.
H2 is single threaded by default. To use the multi-threaded mode, append ;MULTI_THREADED=1 to the database URL. Please note this feature is not fully tested; I suggest to use at least H2 version 1.4.x (and the MVStore) when using this feature.

Can I connect the jdbc connected static Derby data file in remote repository?

I have to load two Derby instances that reference same data Derby file in a remote repository.
Is it possible? How can I achieve this? Would you give detailed information?
Two different connections in the same Derby app is ok because Derby uses Java synchronization features to coordinate their access to the db.
Two different apps cannot use Java synchronization because that only works inside a single Jvm class loader so instead Derby locks the db and only allows one app at a time to access the db.
That is the benefit of the Derby network server because many apps can access the db simultaneously via the network server.
There is a hybrid mode by which one embedded app can also serve as the network server for other apps but it is complex and usually not the best choice.
Update: You can find more information about embedded servers in these docs: http://db.apache.org/derby/docs/10.11/adminguide/cadminov825149.html and http://db.apache.org/derby/docs/10.11/adminguide/radminembeddedserverex.html
or by searching the Internet for "Derby embedded server".

How to load a H2 database into memory?

I have written a set of unit tests using H2 in embedded mode. Whatever changes tests make to DB stay there.
I know that the recommended approach is to create a blank in-memory database and create the schema when opening the connection.
However I am looking for an alternative approach. I would like to -
Initialize an in memory database with an embedded database file.
Or use embedded db in a way that all the changes are discarded as soon as the connection is closed.
How can I achieve this?
What I do in cases similar to this is to write the SQL script that creates the database and populates the tables. Then the application applies a database migration using Flyway DB.
Other possibilities are to create the database and load the tables from CSV files. The other would be to create the database with a different application and create a file with the SCRIPT command to create a backup. Your main application would have to run the RUNSCRIPT command to restore the database.
I use SQL scripts that create tables and other objects and/or populate them, and run these scripts at the beginning of the application.
One could also create a copy of the populated on-disk DB, package it into a ZIP/JAR archive, and open it read only, to be used to recreate and populate the in-memory DB.

Resources