A while ago I had a look at Clojure (http://www.clojure.org). What I
especially liked is the software transactional memory. I figured that
the same functionality should be doable with SQLAlchemy, although it
will probably be slower.
I am now working on an interactive application which keeps a lot of
objects in memory. Given that python does not offer protection of member
variables, I fear that a reference to a list escapes and is overriden
inadvertently.
In Clojure, changes to transactional memory are disallowed outside of a
transaction:
-----
user=> (def data (ref "foo"))
#'user/data
user=> (deref data)
"foo"
user=> (ref-set data "bar")
java.lang.IllegalStateException: No transaction running
(NO_SOURCE_FILE:0)
user=> (dosync (ref-set data "bar"))
"bar"
user=> (deref data)
"bar"
-----
I was expecting the same of SQLAlchemy with autocommit=True sessions.
However, the following code shows that this does not work:
-----
import sqlalchemy as sa
import sqlalchemy.orm as orm
metadata = sa.MetaData()
class Data(object): pass
data_table = sa.Table("data", metadata,
sa.Column("name", sa.String, primary_key=True))
orm.mapper(Data, data_table)
engine = sa.create_engine("sqlite:///:memory:", echo=True)
metadata.create_all(engine)
maker = orm.sessionmaker(bind=engine, autocommit=True)
session = maker()
assert not session.is_active
d1 = Data()
d1.name = "an_entry"
session.add(d1)
# -> I would expect an exception here as we are outside of any
# transaction!
with session.begin():
d2 = Data()
d2.name = "another_entry"
session.add(d2)
# This will fail as there are two entries now.
session.query(Data).one()
-----
Is there a supported way to enforce explicitly starting a transaction
before adding or updating any persistent objects?
My ultimate goal is:
- have the working set of persistent objects in memory (read only by
default)
- when user requests a change, start a transaction and update the db
- by default, don't expire the in-memory objects (this is a local DB
using SQLite, usually only a single app will have access at a time)
- when the user hits f5 (File->Refresh), expire all persistent objects
and refresh the GUI
Greetings, Torsten
--
DYNAmore Gesellschaft fuer Ingenieurdienstleistungen mbH
Torsten Landschoff
Büro Dresden
Tel: +49-(0)351-4519587
Fax: +49-(0)351-4519561
mailto:[email protected]
http://www.dynamore.de
Registergericht: Mannheim, HRB: 109659, Sitz: Karlsruhe,
Geschäftsführer: Prof. Dr. K. Schweizerhof, Dipl.-Math. U. Franz
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.