I’m trying to create a sequence with no gaps in my db (contrived
example, but should work out the same as my real one), and believe
db-managed transactions are the way to go about this.
It’s my first real ‘go’ at transactions, so I may be mistaken about
how things really work.
Anyway… Consider this example code:
Class Invoice < ActiveRecord::Base
self.connection.execute(“set transaction isolation level
serializable, read write”)
last = Invoice.find(:first, :order => “invoice_no desc”)
invoice = Invoice.new
invoice.invoice_no = last.invoice_no + 1
I couldn’t find an ‘AR way’ to set the isolation level, thus drop down
to execute(). Any better approaches?
If i understand correctly, under the serializable transaction level i
shouldn’t be able to read data that’s being updated by another
transaction. Still, running Invoice.create_new from two different
consoles at the same time seems to create two new Invoices, both with
the same invoice_no.
Am I mistaken about how transactions or the serializable isolation
level really work, or is it my code or environment that’s fubar?
Am using Postgres 8.1.something, so the DB should be up to the task.
Any feedback appreciated,