I'm wondering if in c# I should create new instance of Google.Cloud.Datastore.V1.DatastoreDb
every time I want to use it or I can keep one global instance as a singleton?
This is in c# .net core 3 on linux with Assembly Google.Cloud.Datastore.V1, Version=2.1.0.0
using Google.Cloud.Datastore.V1;
void DoStuff()
{
var db = DatastoreDb.Create("my-project")
db.Insert(entity);
}
vs.
using Google.Cloud.Datastore.V1;
static db = DatastoreDb.Create("my-project")
void DoStuff()
{
db.Insert(entity);
}
Yes, DatastoreDb
doesn't contain any local mutable state, and is thread-safe. We recommend that you use a single instance of it to avoid potentially opening more network connections than are necessary.
DatastoreTransaction
, however, does contain local state, and is not thread-safe. (It has no thread affinity, but there's no protection against multiple threads attempting to add mutations at the same time, for example.)