If you want to handle this in application logic itself, instead of letting database handle this. There is no such specific lock for your scenario. And you can use pessimistic locking in rails and specify which lock you will use. Relations using lock are usually wrapped inside a transaction for preventing deadlock conditions.
I believe you should give advisory locks a look. It makes sure the same block of code isn't executed on two machines simultaneously, while still keeping the table open for other business. If you are using postgress, maybe Sequenced can help you out without defining a sequence at the DB level. You probably don't want to lock the table, and even if you lock the row you're currently updating the row you're basing your maxval on will be available for another update to read and generate its sequence.
Unless you have a huge table and lots of updates every millisecond in the order of thousands this shouldn't be an issue in real life. The DB error will propagate to a Rails exception you can deal with within the application.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Postgres back end Ask Question.
Asked 4 years, 3 months ago. Active 4 years, 3 months ago. Viewed 2k times. GGizmos GGizmos 2, 1 1 gold badge 17 17 silver badges 43 43 bronze badges. Is there a reason you can't create a Postgres sequence and let the DB handle it? I could I guess, but I don't like spreading the application logic all over the place i. Seems like a maintenance headache. PS to anybody who has an answer I don't need really need a "credible source" I just need a solution that works.
Add a comment. Active Oldest Votes. Gaurav Sachan Gaurav Sachan 31 4 4 bronze badges. Shovon Shovon 71 3 3 bronze badges. The row lock will not help him. Even if the row is locked his application will read the maxval from that row or any other. The application will only get the value if the lock is successful, when used like this AnswerHeader. Are you saying something else? Let's assume that the rails 3.
However, we didn't specifically ask to update rack-cache , which may not be compatible with rack 1. And while an update from rack 1. In order to avoid this problem, when you update a gem, bundler will not update a dependency of that gem if another gem still depends on it.
In this example, since rack-cache still depends on rack , bundler will not update the rack gem. This ensures that updating rails doesn't inadvertently break rack-cache. Since rails 3. Since you originally declared a dependency on rails 3. As described above, the bundle install command always does a conservative update, refusing to update gems or their dependencies that you have not explicitly changed in the Gemfile.
If rails 3. If you update your Gemfile , and your system already has all of the needed dependencies, bundler will transparently update the Gemfile. For instance, if you add mysql to your Gemfile , and have already installed it in your system, you can boot your application without running bundle install , and bundler will persist the "last known good" configuration to the Gemfile.
This can come in handy when adding or updating gems with minimal dependencies database drivers, wirble , ruby-debug. It will probably fail if you update gems with significant dependencies rails , or that a lot of gems depend on rack. If a transparent update fails, your application will fail to boot, and bundler will print out an error instructing you to run bundle install.
Sometimes, you want to update a dependency without modifying the Gemfile. For example, you might want to update to the latest version of rack-cache. Because you did not declare a specific version of rack-cache in the Gemfile , you might want to periodically get the latest version of rack-cache.
0コメント