Open and Close a database connection in a request

182 views
Skip to first unread message

ProfessionalIT

unread,
Sep 19, 2012, 3:54:12 PM9/19/12
to we...@googlegroups.com
How to implement this ?

When I init the request I need open a connection with the database (and execute a query for example) and in the end of the request I need close the database connection. 

Another question is: How I know that a database connection is open ?

ps: sorry by my 'dummie questions' and by my terrible english !.





   

ProfessionalIT

unread,
Sep 19, 2012, 4:54:30 PM9/19/12
to we...@googlegroups.com
Well,

   I'm trying implement this then in my code.py I have this code:

from model import setDB, unsetDB

def db_handler(handler):
    setDB()
    result=handler()
    unsetDB()
    return result

webapp = web.application(urls, globals())
webapp.add_processor(db_handler)


im my model.py I have this code:

db = web.database(dbn=DBN, host=HOST, db=DB, user=USER, pw=PWD)

def unsetDB():
    print '>>>>>>>>> in the unset <<<<<<<<<<<'
    db = None
    print str(db)

def setDB():
    print '>>>>>>>>> in th SET <<<<<<<<<<<'
    unsetDB()
    db = web.database(dbn=DBN, host=HOST, db=DB, user=USER, pw=PWD)
    print str(db)

but when I look in my system process monitor I still have a opened connection with database

any idea ?

Francois Dion

unread,
Sep 19, 2012, 8:46:28 PM9/19/12
to we...@googlegroups.com


On Wednesday, September 19, 2012 3:54:12 PM UTC-4, ProfessionalIT wrote:
How to implement this ?

When I init the request I need open a connection with the database (and execute a query for example) and in the end of the request I need close the database connection. 


Por que fechar a conexão?

Normally, for performance, you want to open the connection as soon as you get a chance and _keep_ it open. What problem are you trying to solve?

Tchau,
Francois

--
Francois Dion
http://solarisdesktop.blogspot.com

ProfessionalIT

unread,
Sep 19, 2012, 9:51:20 PM9/19/12
to we...@googlegroups.com
Por que fechar a conexão?

Normally, for performance, you want to open the connection as soon as you get a chance and _keep_ it open. What problem are you trying to solve?

Tchau,
Francois

Francois,
   
    Well, I think that when many visitants access the website (where each access is a new browser session and a new database connection correct ? )  this can leave the website slow. 
   
     Actually I still don't know how...I don't understand how the connections works in web.py, for example, I don't know when the connection is closed.

    But, thank you so much for your replay.
    
    Best regards,
    Leandro.

ProfessionalIT

unread,
Sep 21, 2012, 9:04:57 AM9/21/12
to we...@googlegroups.com
 Friends,
 
   Any idea ?, tip ? or suggestion ?

  

Francois Dion

unread,
Sep 21, 2012, 10:11:04 AM9/21/12
to we...@googlegroups.com
Bom dia,

I dont see what the problem is. What database are you using? What kind
of load are you talking about?

Francois



--
Francois Dion
http://solarisdesktop.blogspot.com

On Fri, Sep 21, 2012 at 9:04 AM, ProfessionalIT <lsev...@gmail.com> wrote:
> Friends,
>
> Any idea ?, tip ? or suggestion ?
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "web.py" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/webpy/-/WgK-kI1cAEQJ.
>
> To post to this group, send email to we...@googlegroups.com.
> To unsubscribe from this group, send email to
> webpy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/webpy?hl=en.

Tomas Schertel

unread,
Sep 21, 2012, 1:35:18 PM9/21/12
to we...@googlegroups.com
Hi Leandro,

Have you monitored your database to see how many connections does it has?
I may be wrong, but I think db connections number aren't related to sessions number.
Reply all
Reply to author
Forward
0 new messages