title: Which way is better to implement a thread-safe Counter in a database?
body: <p>I have a table:</p>
  <pre><code>CREATE TABLE COUNTER (ID VARCHAR(256), VALUE INT)
  </code></pre>
  <p>where ID is the primary key.</p>
  <p>I want to implement a mechanism for incrementing and getting the current value of the counter that is thread safe. The counters will be created by the application, there will be many of them. There are many instances of the application with many threads running and they can increment the same counter at the same time.</p>
  <p>Which way is better:</p>
  <ol>
  <li>Serializable transactions with update to increment and then select to get the value</li>
  <li>Optimistic locking (transaction isolation shouldn't matter here) - in a loop: select current value, try to update where value = current value</li>
  </ol>
  <p>Or is there a completely better way to do this?</p>