zoukankan      html  css  js  c++  java
  • sqlalchemy之create_engine和session


    #sqlalchemy之create_engine和session

    orm

    实质上,一个O/R Mapping会为你生成DAL。(即为数据访问层(Data Access Layer)。其功能主要是负责数据库的访问。)用O/R Mapping保存,删除,读取对象,O/R Mapping负责生成SQL,你只需要关心对象就好。

    一般的ORM包括以下四部分:

    • 一个对持久类对象进行CRUD操作的API;// crud:增删查改
    • 一个语言或API用来规定与类和类属性相关的查询;
    • 一个规定mapping metadata的工具;
    • 一种技术可以让ORM的实现同事务对象一起进行dirty checking, lazy association fetching以及其他的优化操作

    SQLAlchemy介绍

    Python中最有名的ORM架构就是SQLAlchemy,我们主要就是来学习SQLAlchemy的使用

    初始化连接

    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker
    self.engine = create_engine(conn['conn_str'],
                                        pool_size=16,
                                        pool_pre_ping=True)
    self.session = sessionmaker(bind=self.engine)
    

    create_engine()用来初始化数据库连接。SQLAlchemy用一个字符串表示连接信息:

    '数据库类型+数据库驱动名称://用户名:口令@机器地址:端口号/数据库名'
    

    数据库连接池的使用

    from sqlalchemy.orm import sessionmaker
    from sqlalchemy import create_engine
    from sqlalchemy.orm import scoped_session
    from models import Student,Course,Student2Course
    
    engine = create_engine(
            "mysql+pymysql://root:123456@127.0.0.1:3306/s9day120?charset=utf8",
            max_overflow=0,  # 超过连接池大小外最多创建的连接
            pool_size=5,  # 连接池大小
            pool_timeout=30,  # 池中没有线程最多等待的时间,否则报错
            pool_recycle=-1  # 多久之后对线程池中的线程进行一次连接的回收(重置)
        )
    SessionFactory = sessionmaker(bind=engine)
    session = scoped_session(SessionFactory)
    
    
    def task():
        """"""
        # 方式一:
        """
        # 查询
        # cursor = session.execute('select * from users')
        # result = cursor.fetchall()
    
        # 添加
        cursor = session.execute('INSERT INTO users(name) VALUES(:value)', params={"value": 'wupeiqi'})
        session.commit()
        print(cursor.lastrowid)
        """
        # 方式二:
        """
        # conn = engine.raw_connection()
        # cursor = conn.cursor()
        # cursor.execute(
        #     "select * from t1"
        # )
        # result = cursor.fetchall()
        # cursor.close()
        # conn.close()
        """
    
        # 将连接交还给连接池
        session.remove()
    
    
    from threading import Thread
    
    for i in range(20):
        t = Thread(target=task)
        t.start()
    

    转自http://www.cnblogs.com/lianggege123/articles/9210147.html

    session 是ORM和db交互的方式

    1. 包装了engine的连接
    2. 包装了对象的identity map(类似cache, 保存了由表名和主键确定的唯一对象列表)
    3. 包装了transaction

    使用sessionmaker创建session
    sessionmaker类保证创建出同样配置的session. 应当全局被使用一次.

    from sqlalchemy import create_engine 
    from sqlalchemy.orm import sessionmaker
    engine = create_engine('sqlite:///:memory:')
    Session = sessionmaker(bind=engine)
    session = Session()
    

    Session状态
    https://stackoverflow.com/questions/8645250/how-to-close-sqlalchemy-connection-in-mysql
    http://docs.sqlalchemy.org/en/latest/orm/session_state_management.html
    一个实例在session中可能有以下状态:

    Transient
    Pending
    Persiste
    Deleted
    Detached

    session.identity_map保存了一个WeakInstanceDict

    key是database_identity(model的class, primary_key); value是InstanceState
    Expunge:

    将对象从session中移除,
    sending persistent instances to the detached state, and pending instances to the transient state

    对象实例会有一个dict对象保存属性, 这些属性类似缓存.
    对象过期的时候(比如commit之后, 由于不知道事务的执行情况), dict内容会作废, 再次访问对象实例的属性, 会触发lazy load. 从数据库加载这些属性. 这个时候如果session已经close就会报错.
    可以在commit之前, 用expunge, 将对象实例从session移除. 这是对象实例可以正常使用. 但是无法加载未加载的属性, 也无法加载expired的属性.

    mysql gone away

    http://docs.sqlalchemy.org/en/latest/faq/connections.html?highlight=gone%20away
    http://docs.sqlalchemy.org/en/latest/core/pooling.html#pool-disconnects

    The primary cause of this error is that the MySQL connection has timed out and has been closed by the server. The MySQL server closes connections which have been idle a period of time which defaults to eight hours. To accommodate this, the immediate setting is to enable the create_engine.pool_recycle setting
    For the more general case of accommodating database restarts and other temporary loss of connectivity due to network issues, connections that are in the pool may be recycled in response to more generalized disconnect detection techniques.

    这时有两种方式解决

    • 悲观方式: 每次执行语句前先执行select
      1 或者通过pre_ping参数
    engine = create_engine("mysql+pymysql://user:pw@host/db", pool_pre_ping=True)
    
    • 乐观方式: 执行失败后重试
    from sqlalchemy import create_engine, exc
    e = create_engine(...)
    c = e.connect()
    
    try:
        # suppose the database has been restarted.
        c.execute("SELECT * FROM table")
        c.close()
    except exc.DBAPIError, e:
        # an exception is raised, Connection is invalidated.
        if e.connection_invalidated:
            print("Connection was invalidated!")
    
    # after the invalidate event, a new connection
    # starts with a new Pool
    c = e.connect()
    c.execute("SELECT * FROM table")
    

    转自(https://www.jianshu.com/p/96e393afd721

    添加点web里的session,对比一下

    cookie是浏览器保存在用户电脑上的一小段文本,用来保存用户在网站上的必要的信息。Web页面或服务器告诉浏览器按照一定的规范存储这些信息,并且在以后的所有请求中,这些信息就会自动加在http请求头中发送给服务器,服务器根据这些信息判断不同的用户。并且cookie本身是安全的。


    比如:
    Cookie: name=value; name1=value1; name2=value2/pre>

    Session

    session的作用和cookie差不多,也是用来解决Http协议不能维持状态的问题。但是session只存储在服务器端的,不会在网络中进行传输,所以较cookie来说,session相对安全一些。但是session是依赖cookie的,当用户访问某一站点时,服务器会为这个用户产生唯一的session_id,并把这个session_id以cookie的形式发送到客户端,以后的客户端的所有请求都会自动携带这个cookie(前提是浏览器支持并且没有禁用cookie)。

    禁用cookie时如何使用session

    有些时候,为了安全浏览器会禁用cookie,这时可以用传参的方式将session_id发送到服务器,session可以照常工作.

  • 相关阅读:
    Castle 1.0 RC2 尝鲜
    关注 Web Client Software Factory [Weekly Drop 08]
    ASP.NET AJAX入门系列
    Castle 1.0 Release Candidate 2发布
    ASP.NET AJAX入门系列(2):使用ScriptManager控件
    ASP.NET AJAX 1.0 Beta 发布相关文章总结及推荐
    关于ASP.NET AJAX的三个视频
    企业库2.0培训系列课程大纲[意见征询]
    Visual Studio“Orcas”October 2006 CTP版下载
    ASP.NET AJAX入门系列(4):使用UpdatePanel控件(一)
  • 原文地址:https://www.cnblogs.com/wangjiale1024/p/10331627.html
Copyright © 2011-2022 走看看