Skip to main content

Fall 2007

Go Search
Fall 2007
Customer Resource Site
  
Fall 2007 > Announcements > Backend Usage Guidelines  

Announcements: Backend Usage Guidelines

Title

Backend Usage Guidelines 

Body

Recently a number of bugs have been cropping up with the backend throwing exceptions about connections and transactions being closed. I believe that this stems from a misunderstanding about how to actually use the backend. I'll try to rectify that here. This information should go in a document later on, but I think people will be more likely to read it if I just post here.
(as a side note, I've committed a change that (I think) will prevent all of these exceptions from being thrown; instead, I'm throwing my own BackendDisposedExceptions now. If you get this you're doing the following thing wrong.)
I think that everyone has been very good about putting any use of the backend inside a "using" statement in the form of "using (IBackend backend = BackendFactory.Create()) { ... }" What happens here is that when the BackendFactory creates an IBackend, it opens connections to the database that are only open within the curly braces. This is good because it prevents us from having lots of unused connections sitting around.
However, what most people probably don't know is that anything you get from the backend (content, entities, tags, etc) internally retains a reference to the backend that created it. This means that if you have a reference to a piece of content that you hold onto after the end of the using block, that content holds a reference to a backend with closed connections.
This hasn't been a problem for most of the time because all object that you retrieve from the backend should cache all of their data internally, so you can perform "get" operations regardless of if the backend is open or closed.
The problem comes with trying to modify the piece of content (or entity or whatever), which people are just now starting to do. Since the content no longer has a connection to the database, you can't change it, and trying to do so is causing those exceptions. There's simply no way to get around this.
To understand this better, think of a very similar situation. You open a File. From that File, you create a FileWriter. You write to the File for a while through the FileWriter, but then you decide to close the File. Once you've done that, the FileWriter essentially becomes useless, and even though you still have a reference to it, trying to call any of its methods will throw exceptions.
The way to fix this is to think of each "using" statement as starting a session where you interact with data. As long as you want to use that data, be within the using statement.

Expires

 

Links

 
Attachments
Created at 11/8/2007 10:46 PM  by Aaron D. Cottle 
Last modified at 11/8/2007 10:46 PM  by Aaron D. Cottle