Python flask.g._database() Examples

The following are 13 code examples of flask.g._database(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module flask.g , or try the search function .
Example #1
Source File: server.py    From pyRecommender with MIT License 5 votes vote down vote up
def get_db():
    db = getattr(g, "_database", None)
    if db is None:
        db = g._database = sqlite3.connect(DATABASE)
        cur = db.cursor()
        cur.execute(
            """create table if not exists {} (
            Telephone char(11) PRIMARY KEY NOT NULL,
            Password  char(20) NOT NULL,
            Username  char(20),
            Sex       char(1),
            Location  char(50))""".format(TABLE)
        )
        db.isolation_level = None
    return db 
Example #2
Source File: server.py    From pyRecommender with MIT License 5 votes vote down vote up
def close_db_connection(exception):
    db = getattr(g, "_database", None)
    if db is not None:
        db.close()


############################
######## Flask apps ########
############################ 
Example #3
Source File: db.py    From isdi with MIT License 5 votes vote down vote up
def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        print("Creating new db connection {}".format(DATABASE))
        db = g._database = sqlite3.connect(DATABASE)
        db.row_factory = make_dicts
    return db 
Example #4
Source File: tipi_cache.py    From tipi with GNU General Public License v3.0 5 votes vote down vote up
def get_context_conn():
    """
    Flask needs a database connection per request context, and that
    is registered to close in route.py
    """
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = sqlite3.connect(db_name)
    return db 
Example #5
Source File: htc_api.py    From syntheticmass with Apache License 2.0 5 votes vote down vote up
def get_db_con():
    global pool
    max_attempts = 10
    con = getattr(g, '_database', None)
    if con is None:
        #Need to get a connection, use a try loop to handle pool depletions a bit better 
        #Otherwise psycopg2.pool throws exception and server returns 500 error to client
        for attempt in range(1, max_attempts):
            try:
                 con = g._database = pool.getconn()
                 if (attempt > 1):
                     log.debug("connection newly acquired from pool, attempt=%s" % attempt)
                 return con
            except:
                 #On any errors, add exponentially increasing time delays.
                 #This seems to handle at least 30X the pool size in requests without hard errors.
                 e = sys.exc_info()[0]
                 log.error("exception during connection attempt=%s: %s" % (attempt, e))
                 if (attempt == max_attempts):
                     #give up!
                     raise
                 time.sleep(attempt**2)
    else:
      log.debug("connection reused from session variable.")
    con.autocommit = True
    return con


#Automatically return db connections 
Example #6
Source File: htc_api.py    From syntheticmass with Apache License 2.0 5 votes vote down vote up
def return_db_con(exception):
    global pool
    con = getattr(g, '_database', None)
    if con is not None:
        pool.putconn(con)
        #log.debug("connection returned to pool.")


#format simple data for return as JSON 
Example #7
Source File: joseki_query.py    From training with Apache License 2.0 5 votes vote down vote up
def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = sqlite3.connect(DATABASE)
    return db 
Example #8
Source File: joseki_query.py    From training with Apache License 2.0 5 votes vote down vote up
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close() 
Example #9
Source File: server.py    From tavern with MIT License 5 votes vote down vote up
def get_db():
    db = getattr(g, "_database", None)
    if db is None:
        db = g._database = sqlite3.connect(DATABASE)

        with db:
            try:
                db.execute(
                    "CREATE TABLE numbers_table (name TEXT NOT NULL, number INTEGER NOT NULL)"
                )
            except Exception:
                pass

    return db 
Example #10
Source File: server.py    From tavern with MIT License 5 votes vote down vote up
def close_connection(exception):
    db = getattr(g, "_database", None)
    if db is not None:
        db.close() 
Example #11
Source File: server.py    From tavern with MIT License 5 votes vote down vote up
def get_db():
    db = getattr(g, "_database", None)
    if db is None:
        db = g._database = sqlite3.connect(DATABASE)

        with db:
            try:
                db.execute(
                    "CREATE TABLE numbers_table (name TEXT NOT NULL, number INTEGER NOT NULL)"
                )
            except:
                pass

    return db 
Example #12
Source File: server.py    From tavern with MIT License 5 votes vote down vote up
def close_connection(exception):
    db = getattr(g, "_database", None)
    if db is not None:
        db.close() 
Example #13
Source File: server.py    From tavern with MIT License 5 votes vote down vote up
def get_cached_db():
    db = getattr(g, "_database", None)
    if db is None:
        db = g._database = get_db()

    return db