objectbox / objectbox-go

Embedded Go Database, the fast alternative to SQLite, gorm, etc.
https://objectbox.io
Apache License 2.0
1.07k stars 46 forks source link

Database access. #12

Closed harisgen79 closed 5 years ago

harisgen79 commented 5 years ago

Hello,

I am making an embedded system on Raspberry Pi 3 where I'm keeping track of arrivals of employees to work. I have two databases, one to store data about arrivals (RFID, time of arrival, etc.) and other to store general data about employees. Additionally, I also want to send data to cloud. For that purpose I have made two Golang programs, let's call them program1 for managing database of arrivals and program2 for database of employees. Is it possible to make these programs access both databases and make changes. If yes, how and in which situations?

vaind commented 5 years ago

ObjectBox Go currently only works in embedded mode, where there is no separate database server. Therefore, a program requires unique access to the database (files).

From what I understand about your situation, you have two options:

Your best bet would be for each program to have its own database. If you really need one, you can have a simple service with both databases, providing an API to the other programs.

In the future, you could use ObjectBox Sync to solve this situation (if you would be fine with asynchronous data propagation between the clients) but that's not readily available yet.

greenrobot commented 5 years ago

There might be a third option: merge program1 and program2 into one. If both programs should access both databases, what is the motivation to separate them? Asking to understand your use case better...

harisgen79 commented 5 years ago

Program1 performs as an RFID card scanner which means it should respond quickly which is the motivation to separate them. Program2 also does POST and GET methods every interval (few hours or so, haven't defined it yet). When it does GET it receives data of 1500+ employees which takes a long time. If it was all in one program, RFID reader would not be responding while doing GET so I had to separate them, otherwise there might be a queue of employees getting angry because the device is not responding instantly.

greenrobot commented 5 years ago

@harisgen79 ObjectBox uses a multi version concurrency model, where a reader (read transactions; usually sufficient for a typical GET) never blocks a writer.

harisgen79 commented 5 years ago

OK then, I'll figure something out then. Thank you for the answers. Cheers.

vaind commented 5 years ago

Also, you can run the employee update job "in the background" as another goroutine, see https://golang.org/pkg/time/#example_NewTicker - that means you can have it all in the single application while not blocking the RFID scanner functionality

harisgen79 commented 5 years ago

I made exactly that, works fine, thanks for the advice.

vaind commented 5 years ago

Glad it worked. Closing the issue.