It depends on what you are using for persistence management. For example, JDO will throw optimistic locking errors named appropriately that you have to catch and do something with. Others, like
JDBC you often have to check for optimistic locking errors (often using a timestamp) yourself.
It sounds like you'd prefer a pessamistic locking error such as programs that lock an entire table or database while others wait... To be frank, pessamistic locking of any kind is not good in practice. The performance impact is detrimental.
The best you can do is row-level pessamistic locking but that only prevents duplicate modification of entries doesn't prevent duplicate entries from being inserted at the same time. For example, two queries might both look for the sum of the prices in the database, perform some mathematics on them, and then insert records based on that math. Since the two new records did not know about each other, and therefore could not lock each other, they could be later inserted with false data. Like checking to see if a user exists while creating a user ID... two separate users creating the same id would not neccessarily block each other.