On Dec 6, 2006, at 6:40 PM, Dennis Muhlestein wrote:
>
> This is more of a best practice question:
>
> I find having the framework manage the database session for you OK
> 95%
> of the time. What do you do in the other 5% of the time?
>
> Example 1: I have certain credit card processing methods that require
> commits to the database before and after the card is processed.
> Example 2: I have some methods that require no database interaction.
> (So why create a session? .. also.. how much overhead is there in
> creating the session?)
>
> Anyone have any best practice ideas for situations like this?
>
> Here is what I did on previous websites:
> Background:
> 1- When I 1st started Using SQLa, TG had no support for it but it
> worked great by manually managing it.
> 2- SQLalchemy support didn't work with TG.run_with_transaction for a
> time when SQLA moved to version 2.x
> Solutions:
> 1-Manually manage SQLAlchemy transations
> 2-Hack TG in my start script to avoid run_with_transaction
> altogether:
>
> ==snip==
> # hack for sa 0.2.x
> def nothing(func,*args,**kw):
> return func(*args,**kw)
> turbogears.database.run_with_transaction=nothing
> ==end snip==
>
> Anyway, I'm doing some new work and it would be the perfect time to
> get
> rid of that hack but I don't have a real good solution for the 2
> scenarios I mentioned above.
>
> Thoughts?
> -Dennis
run_with_transaction is a generic function you can specialize to fit
your needs, for example:
from turbogears.database import run_with_transaction, _use_sa
@run_with_transaction.when("getattr(func, '_no_trans', False) and
_use_sa()")
def _no_trans_for_this_method(func, *args, **kw):
return func(*args, **kw)
Now in any controller method you can set a _no_trans attribute to
True so this specialized version which doesn't start a session or
begins any transaction runs when run_with_transaction is called:
@expose()
def fooo(self):
#do something, calling this method will not run under a transaction.
fooo._no_trans = True
Much more flexible than monkey-patching (http://tinyurl.com/uz3s9),
isn't it? :)
Alberto
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" 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/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---