Comments (2)
Thanks for your interest in the library!
How often should I be creating a lock?
The lock objects in DistributedLock are very light-weight and are stateless (unless you construct with a specific SqlConnection instead of a connection string). Therefore, you can construct them as frequently or as infrequently as you'd like; do whatever is convenient for the design of your code.
Does a new SqlDistributedLock require a separate sqlserver connection?
If you pass a SqlConnection or SqlTransaction to the lock constructor, then it will use that specific connection/transaction for all locking requests. Because SqlConnection/SqlTransaction are not thread-safe, then that lock instance won't be thread-safe either.
However, the more typical approach (which is what your code sample appears to do) is to pass a connection string. With this approach, each acquire will open a new SqlConnection under the hood (except with multiplexing; see below). Unless your connection string explicitly disables connection pooling, most of the time this will draw an existing connection from the .NET connection pool rather than actually creating a new one. When the lock handle is released, the underlying connection is closed (released back to the pool).
I should be using OptimisticConnectionMultiplexing
The main resource risk with SqlDistributedLocks is that holding/waiting for many locks simultaneously occupies many connections. This can lead to connection pool exhaustion (if this happens, you'll see specific error messages thrown that relate to it). The default connection pool size is 100 per unique connection string; so one quick fix that works for most applications is to just up the pool size.
OptimisticConnectionMultiplexing lets you scale even further in some scenarios by allowing one connection to service multiple lock requests. This works best when you TryAcquire locks with zero timeout or with a small timeout or when contention is very low (hence "optimistic"), since fundamentally one connection can only block waiting on one lock at a time.
But I need the Azure strategy, because sometimes one call to the method may run for an hour or more. Would it be possible to combine the two strategies?
If you need the Azure strategy, I would recommend sticking with that. I'm currently working on a 2.0 release of the library, and one of the new features is that optimistic multiplexing and keepalive are both on by default. So once 2.0 comes out you can get this for free.
Until then, I would suggest that you continue using Azure and just bump up the connection pool size if you are experiencing connection pool exhaustion.
from distributedlock.
Great, thanks for getting back. I'll stick with Azure strategy and try to bump the connection pool
from distributedlock.
Related Issues (20)
- Postgres lock is not released in specific multi-threaded scenarios HOT 13
- Timeouts seems not work in DistributedLock.Redis HOT 5
- Update minimum SqlClient versions to avoid vulnerability HOT 2
- Postgres: consider leveraging batching for efficiency (newer versions only)
- Postgres: consider switching to positional parameter style
- Postgres: consider constructor with NpgsqlDatasource
- ZooKeeperNetEx connection loss issue: an acquired lock seems is not released HOT 50
- Azure: create the container on the fly if it does not exist HOT 4
- Increase in lost lock after upgrading to .NET 7, EF Core 7 and Microsoft.Data.SqlClient 5.0.1 HOT 21
- Add custom data to AzureBlobLeaseDistributedLock HOT 10
- Redis: RedisScript (LuaScript) execution error - CultureInfo problem HOT 7
- DistributedLock.Azure: Remove MaxNonInfiniteLeaseDuration HOT 2
- Use Distributed lock to manage multiple calls to the same Azure function HOT 2
- Postgres distributed lock using PgBouncer connection pooler HOT 8
- About Distribute Locking System using Redis HOT 2
- RobiniaDocs API Explorer HOT 8
- Postgres - Multiple threads successfully acquire the same lock HOT 3
- Concurrency with Redis Semaphore HOT 2
- Error acquiring lock on PostgreSQL when using Npgsql 8.0 HOT 5
- PostgreSQL — Exception while using "AcquireAsync" or "TryAcquireAsync" multiple times HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from distributedlock.