Forgive me if this is a silly question (I'm new to databases and SQL), but is it possible to lock a table, similar to the `lock` keyword in C#, so that I can query the database to see if a condition is met, then insert a row afterwards while ensuring the state of the table has not changed between the two actions?

In this case, I have a table `transactions` which has two columns: `user` and `product`. This is a many-to-one relationship; multiple users can have the same product. However, the number of `product`s is limited.

When a user adds a product to their account, I want to first check if the total number of items with the same `product` value to see if it is under a certain threshold, then add the transaction afterwards. However, since this is a multithreaded application, multiple transactions can come in at the same time. I want to make sure that one of these is rejected, and one succeeds, such that the number of transactions with the same `product` value can never be higher than the limit.

Rough pseudo-code for what I am trying to do:

```
my_user, my_product = ....
my_product_count = 0
for each transaction in transactions:
    if product == my_product:
        my_product_count += 1
if my_product_count < LIMIT:
    insert my_user, my_product into transactions
    return SUCCESS
else:
    return FAILURE

```

I am using SQLAlchemy with SQLite3, if that matters.