clearing out the elixir session.

4 views
Skip to first unread message

DavidG

unread,
Dec 19, 2009, 10:17:04 AM12/19/09
to SQLElixir
Hi--

I am using sqlalchemy version 0.5.5
and elixir version 0.7.1

I am having a problem where I have an application that uses elixir on
top of a database that can be updated by another application. If i
don't do a full restart of the first application it does not seem to
pickup the database changes.

I have tried all of the following:
session.remove()
session.expunge_all()
session.close_all()
session.expire_all()
session.clear()
session.flush()
session.close()

I have also tried to reload the module that is being called...

Any help would be greatly appreciated,
Thanks, David

Gaetan de Menten

unread,
Dec 20, 2009, 6:39:57 AM12/20/09
to sqle...@googlegroups.com
On Sat, Dec 19, 2009 at 16:17, DavidG <dbgr...@gmail.com> wrote:

> I am having a problem where I have an application that uses elixir on
> top of a database that can be updated by another application. If i
> don't do a full restart of the first application it does not seem to
> pickup the database changes.
>
> I have tried all of the following:
>    session.remove()
>    session.expunge_all()
>    session.close_all()
>    session.expire_all()
>    session.clear()
>    session.flush()
>    session.close()
>
> I have also tried to reload the module that is being called...

Do you know that the in-memory objects (within the session or not) are
never automatically updated with the changes in the database? You have
to do session.query or session.merge.

Other than that, does the other application commit its changes?

Hope it helps,
--
Gaëtan de Menten
http://openhex.org

DavidG

unread,
Dec 20, 2009, 11:12:37 AM12/20/09
to SQLElixir
Thank you for your response. I am issuing a new query, that is not
returning updated data.

Thanks, David

On Dec 20, 5:39 am, Gaetan de Menten <gdemen...@gmail.com> wrote:

fboule

unread,
Dec 31, 2009, 2:10:41 AM12/31/09
to SQLElixir
I have the same problem with the snippet hereafter:

from elixir import *

class tt(Entity):
using_options(tablename = 'tt')
d = Field(Integer, primary_key = True)

setup_all()
metadata.bind = 'mysql://localhost/test'

while True:
a = tt.query.filter(None)

for x in a:
print x.d

raw_input('')
a = None
session.expire_all()

Once inserted new records in the table tt by using the command-line
mysql client (and committed), pressing return to loop once shows the
very same set of records, without the newly inserted ones.

On the other hand, note that I found out that moving the metadata.bind
assignment into the while loop does the job. But that means to
reconnect to the engine over and over, which is quite ugly:

from elixir import *

class tt(Entity):
using_options(tablename = 'tt')
d = Field(Integer, primary_key = True)

setup_all()

while True:
metadata.bind = 'mysql://localhost/test'
a = tt.query.filter(None)

for x in a:
print x.d

raw_input('')

Does anyone have a cleaner solution?

Fabien.

Glazner

unread,
Dec 31, 2009, 3:10:17 AM12/31/09
to SQLElixir

this works fine:

write_and_commit.py:

from elixir import *
import time
metadata.bind = "sqlite:///pop.db"
metadata.bind.echo = True
session.configure(autoflush=False)

class Movie(Entity):
title = Field(Unicode(30),primary_key=True)

def __repr__(self):
return '<Movie "%s" (%d)>' % (self.title, self.year)

setup_all(True)

[m.delete() for m in Movie.query.all()]

for i in range(1000):
m=Movie(title=u'pop%d'%i)
session.commit()
time.sleep(3)

sniffer.py:

from elixir import *
import time

metadata.bind = "sqlite:///pop.db"
metadata.bind.echo = False
session.configure(autoflush=False)

class Movie(Entity):
title = Field(Unicode(30),primary_key=True)

def __repr__(self):
return '<Movie "%s">' % (self.title)


setup_all(False)

while True:
time.sleep(1)
print "\n".join(map(str,(Movie.query.all())))


run write_and_commit.py first and then sniffer.py
it works.

Fabien Bouleau

unread,
Dec 31, 2009, 4:17:55 AM12/31/09
to sqle...@googlegroups.com
I have just tested my script using sqlite instead of mysql and it works too. However I need to run it with mysql...

Fabien.

2009/12/31 Glazner <yoavg...@gmail.com>

--

You received this message because you are subscribed to the Google Groups "SQLElixir" group.
To post to this group, send email to sqle...@googlegroups.com.
To unsubscribe from this group, send email to sqlelixir+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlelixir?hl=en.



Gaetan de Menten

unread,
Dec 31, 2009, 6:56:56 AM12/31/09
to sqle...@googlegroups.com
On Thu, Dec 31, 2009 at 08:10, fboule <fabien....@gmail.com> wrote:
> I have the same problem with the snippet hereafter:
>
> from elixir import *
>
> class tt(Entity):
>    using_options(tablename = 'tt')
>    d = Field(Integer, primary_key = True)
>
> setup_all()
> metadata.bind = 'mysql://localhost/test'
>
> while True:
>    a = tt.query.filter(None)
>
>    for x in a:
>        print x.d
>
>    raw_input('')
>    a = None
>    session.expire_all()
>
> Once inserted new records in the table tt by using the command-line
> mysql client (and committed), pressing return to loop once shows the
> very same set of records, without the newly inserted ones.

That's very strange, because your exact same code works fine here. I
just ran it once with a create_all() after the bind (to create the
table) then removed the line and it works perfectly here.

I've tried with both SA 0.5.x and SA trunk, Elixir 0.6.0, Elixir 0.7.1
and trunk and could not reproduce your problem... I'm out of ideas...

On mysql's side, I've simply done:

mysql> insert into tt values(1);
Query OK, 1 row affected (0.00 sec)

I didn't commit explicitly as it is not needed on the command-line
AFAIK (unless you create a transaction explicitly).

FWIW, here are the versions I have installed:
ii mysql-server-5.1 5.1.37-1ubuntu5
MySQL database server binaries
ii python-mysqldb 1.2.2-10
A Python interface to MySQL

> On the other hand, note that I found out that moving the metadata.bind
> assignment into the while loop does the job. But that means to
> reconnect to the engine over and over, which is quite ugly:

Yeah, quite ugly indeed.

Since this problem is most likely not related to Elixir, you might
want to ask on SQLAlchemy's list.

fboule

unread,
Jan 1, 2010, 4:49:12 PM1/1/10
to SQLElixir
Well, I just tested it on Fedora Core and it works. That means it does
not work properly with the mysql wrapper on WinXP (sqlite works)...
which is fine since I intend to use it on Linux.

Thanks a lot for your support, I will post to SQLAlchemy and/or to
mysql-python.

Fabien.

Reply all
Reply to author
Forward
0 new messages