"meaning that a single commit() should address all the objects that
are related to a particular operation."

i commited it to my mind. :)

2013/7/18 Michael Bayer <[email protected]>:
>
> On Jul 18, 2013, at 6:52 PM, Victor Reichert <[email protected]> wrote:
>
>> HI All,
>>
>> I'm working on my first SQL Alchemy project.  I'm thinking I'm going to 
>> define a commit method for all the objects I want persist, I'm thinking 
>> something like:
>>
>>     def commit(self):
>>         session = Session() #Session is a global sessionmaker
>>         session.add(self)
>>         session.commit()
>>         session.close()
>>
>> Is that a good practice?
>
> This is an antipattern.   You should keep the means of persistence separate 
> from the objects that you are persisting, and you should be thinking in terms 
> of use cases as far as how to structure transactions, meaning that a single 
> commit() should address all the objects that are related to a particular 
> operation.
>
> A bad metaphor might be, suppose we wish to write a collection of sentences 
> to a file.   The antipattern approach to me looks like this:
>
> class Sentence(object):
>     def __init__(self, text):
>         self.text = text
>
>     def write(self):
>         handle = open(self.file, "a")
>         handle.write(self.text)
>         handle.close()
>
> file = "myfile.txt"
> for sentence in sentences:
>     sentence.write()
>
> While thinking in terms of operations instead of objects looks like this:
>
> class Sentence(object):
>     def __init__(self, text):
>         self.text = text
>
> handle = open(self.file, "w")
> for sentence in sentences:
>     handle.write(sentence.text)
> handle.close()
>
> besides the obvious efficiency, we don't force each sentence to deal with the 
> target file in isolation of all the other sentences.  Dealing with the file's 
> lifespan is outside of the scope of the thing we're putting in the file.
>
> By "use case", this depends a lot on what kind of application this is.   If 
> it's a web application, you want a transaction per request.  If it's a short 
> console script, you want a transaction for the life of the script itself.  
> There's a lot more written about this here:   
> http://docs.sqlalchemy.org/en/rel_0_8/orm/session.html#session-frequently-asked-questions
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



-- 
Mauricio de Abreu Antunes
Mobile: (51)930-74-525
Skype: mauricio.abreua

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to