Multiple application instances in electron will have indexedDB multiple open exceptions due to session sharing. Roughly, you will encounter an error like the following.
When indexedDB is used in an application, this is a problem that will definitely be faced and needs to be solved.
- If it is not necessary, you can detect and disable multiple application sessions at startup.
- If there is a need for multiple applications, resource isolation can be achieved by detecting multiple applications and setting up separate sessions.
Single-instance mode: disable electron application multi-segmentation
In most cases, you can create multiple windows to satisfy most of your application’s simulation needs. This is the official recommendation of electron, and an example of a solution to keep the application in singleton mode is given. Example.
Related APIs are:
- Get single instance lock：app.requestSingleInstanceLock()
- Does it have a lock：app.hasSingleInstanceLock()
- Release the lock：app.releaseSingleInstanceLock()
Use separate sessions to allow multiple openings for electron applications
In our real-world application, which allows users to open multiple clients to log in to different accounts, indexedDB is used to cache large amounts of underlying data to reduce memory usage and reduce GC pressure. Cached data persistence is not necessary in this case. The data that needs to be persisted locally is not too much and can be achieved by creating different local files for different users with encrypted storage.
When creating a BrowserWindow, electron can use a different session by setting the session or partition parameter. session has a higher priority than partition, but partition specifies a string in a more concise way. When the value of partition starts with
persist:, the created session is persisted in the
app.getPath('userData')/Partitions directory, otherwise it is created in memory only.
One thing to note is that only BrowserWindow with the same partition can share session (various stores, MessageChannel, etc. can only share and communicate with each other), so when creating multiple different windows, if you need to share session with each other, you need to set the same parameter value.
So we can implement the basic scheme of electron application multiplication in the following way.
- detect if there is an application multiplier
- If there is a multi-partition, set a different
partitionvalue when creating the form to use a separate session
Methods for detecting whether an application is multipartitioned
wmic command on Windows and the
ps command on Linux and Mac OS to get information about all running applications on the system. By using the pid and ppid information of the application process, you can identify whether the application has multiple openings. Example.
electron itself provides the
app.requestSingleInstanceLock API, you can also simply use it to determine if the current start is a multiple start.
Setting up separate sessions for multiple applications
If the data stored in indexedDB needs to be persistent in order to optimize data loading performance for secondary starts, you can specify a separate persistent session for each instance, otherwise it can be created as an in-memory temporary session.
Note that the value of the partition parameter should be the same when creating multiple BrowserWindow.