tags:

views:

361

answers:

4

I'm currently working on a large implementation of Class::DBI for an existing database structure, and am running into a problem with clearing the cache from Class::DBI. This is a mod_perl implementation, so an instance of a class can be quite old between times that it is accessed. From the man pages I found two options:

Music::DBI->clear_object_index();

And:

Music::Artist->purge_object_index_every(2000);

Now, when I add clear_object_index() to the DESTROY method, it seems to run, but doesn't actually empty the cache. I am able to manually change the database, re-run the request, and it is still the old version. purge_object_index_every says that it clears the index every n requests. Setting this to "1" or "0", seems to clear the index... sometimes. I'd expect one of those two to work, but for some reason it doesn't do it every time. More like 1 in 5 times.

Any suggestions for clearing this out?

+5  A: 

The "common problems" page on the Class::DBI wiki has a section on this subject. The simplest solution is to disable the live object index entirely using:

$Class::DBI::Weaken_Is_Available = 0;
John Siracusa
A: 

I've used remove_from_object_index successfully in the past, so that when a page is called that modifies the database, it always explicitly reset that object in the cache as part of the confirmation page.

zigdon
+2  A: 

$obj->dbi_commit(); may be what you are looking for if you have uncompleted transactions. However, this is not very likely the case, as it tends to complete any lingering transactions automatically on destruction.

When you do this:

Music::Artist->purge_object_index_every(2000);

You are telling it to examine the object cache every 2000 object loads and remove any dead references to conserve memory use. I don't think that is what you want at all.

Furthermore,

Music::DBI->clear_object_index();

Removes all objects form the live object index. I don't know how this would help at all; it's not flushing them to disk, really.

It sounds like what you are trying to do should work just fine the way you have it, but there may be a problem with your SQL or elsewhere that is preventing the INSERT or UPDATE from working. Are you doing error checking for each database query as the perldoc suggests? Perhaps you can begin there or in your database error logs, watching the queries to see why they aren't being completed or if they ever arrive.

Hope this helps!

Sean
A: 

I should note that Class::DBI is deprecated and you should port your code to DBIx::Class instead.

Shlomi Fish