搜索
首页数据库mysql教程BDB事务共享区域
BDB事务共享区域Jun 07, 2016 pm 04:26 PM
事务共享区域我们最后

最后我们来看一下事务共享区域。 首先仍然是每个进程都有的一个数据结构__db_txnmgr{}: /** The transaction manager encapsulates the transaction system. It contains* references to the log and lock managers as well as the state that keeps* track

最后我们来看一下事务共享区域。

首先仍然是每个进程都有的一个数据结构__db_txnmgr{}:

<code>/*
* The transaction manager encapsulates the transaction system.  It contains
* references to the log and lock managers as well as the state that keeps
* track of the shared memory region.
*/
struct __db_txnmgr {
/* These fields need to be protected for multi-threaded support. */
    db_mutex_t    *mutexp;    /* Synchronization. */
                    /* list of active transactions */
    TAILQ_HEAD(_chain, __db_txn)    txn_chain;
/* These fields are not protected. */
    REGINFO        reginfo;    /* Region information. */
    DB_ENV        *dbenv;        /* Environment. */
    int (*recover)            /* Recovery dispatch routine */
        __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
    u_int32_t     flags;        /* DB_TXN_NOSYNC, DB_THREAD */
    DB_TXNREGION    *region;    /* address of shared memory region */
    void        *mem;        /* address of the shalloc space */
};
</code>

同样拥有一个指向描述共享区域的reginfo字段和指向共享区域的指针mem。

<code>/*
* Layout of the shared memory region.
* The region consists of a DB_TXNREGION structure followed by a large
* pool of shalloc'd memory which is used to hold TXN_DETAIL structures
* and thread mutexes (which are dynamically allocated).
*/
struct __db_txnregion {
    RLAYOUT        hdr;        /* Shared memory region header. */
    u_int32_t    magic;        /* transaction magic number */
    u_int32_t    version;    /* version number */
    u_int32_t    maxtxns;    /* maximum number of active txns */
    u_int32_t    last_txnid;    /* last transaction id given out */
    DB_LSN        pending_ckp;    /* last checkpoint did not finish */
    DB_LSN        last_ckp;    /* lsn of the last checkpoint */
    time_t        time_ckp;    /* time of last checkpoint */
    u_int32_t    logtype;    /* type of logging */
    u_int32_t    locktype;    /* lock type */
    u_int32_t    naborts;    /* number of aborted transactions */
    u_int32_t    ncommits;    /* number of committed transactions */
    u_int32_t    nbegins;    /* number of begun transactions */
    SH_TAILQ_HEAD(_active) active_txn;    /* active transaction list */
};
</code>

共享区域除了有个公用的头部hdr字段以外,最重要的是有一个所有活动事务的列表active_txn字段。

现在让我们看一下这些数据结构和共享区域的创建或打开以及初始化操作。

在db_appinit()函数中调用txn_open()函数打开或连接一个事务共享区域:

<code>if (LF_ISSET(DB_INIT_TXN) && (ret = txn_open(NULL,
    LF_ISSET(DB_CREATE | DB_THREAD | DB_TXN_NOSYNC),
    mode, dbenv, &dbenv->tx_info)) != 0)
    goto err;
</code>

其中txn_open定义如下:

<code>int
txn_open(path, flags, mode, dbenv, mgrpp)
    const char *path;
    u_int32_t flags;
    int mode;
    DB_ENV *dbenv;
    DB_TXNMGR **mgrpp;
{
    DB_TXNMGR *tmgrp;
    u_int32_t maxtxns;
    int ret;
    /* Validate arguments. */
    首先仍然是验证参数
    然后用malloc创建每个进程都有的事务管理器结构
    /* Now, create the transaction manager structure and set its fields. */
    if ((ret = __os_calloc(1, sizeof(DB_TXNMGR), &tmgrp)) != 0)
        return (ret);
    并初始化事务管理器结构
    /* Initialize the transaction manager structure. */
    tmgrp->mutexp = NULL;
    tmgrp->dbenv = dbenv;
    tmgrp->recover =
        dbenv->tx_recover == NULL ? __db_dispatch : dbenv->tx_recover;
    tmgrp->flags = LF_ISSET(DB_TXN_NOSYNC | DB_THREAD);
    TAILQ_INIT(&tmgrp->txn_chain);
    现在开始创建或连接到一个事务共享区域
    /* Join/create the txn region. */
    首先还是一样,填充区域描述信息REGINFO{},然后调用__db_rattach()函数创建或打开一个共享区域
    tmgrp->reginfo.dbenv = dbenv;
    tmgrp->reginfo.appname = DB_APP_NONE;
    if (path == NULL)
        tmgrp->reginfo.path = NULL;
    else
        if ((ret = __os_strdup(path, &tmgrp->reginfo.path)) != 0)
            goto err;
    tmgrp->reginfo.file = DEFAULT_TXN_FILE;
    tmgrp->reginfo.mode = mode;
    tmgrp->reginfo.size = TXN_REGION_SIZE(maxtxns);
    tmgrp->reginfo.dbflags = flags;
    tmgrp->reginfo.addr = NULL;
    tmgrp->reginfo.fd = -1;
    tmgrp->reginfo.flags = dbenv->tx_max == 0 ? REGION_SIZEDEF : 0;
    if ((ret = __db_rattach(&tmgrp->reginfo)) != 0)
        goto err;
    /* Fill in region-related fields. */
    tmgrp->region = tmgrp->reginfo.addr;
    tmgrp->mem = &tmgrp->region[1];
    if (F_ISSET(&tmgrp->reginfo, REGION_CREATED)) {
        tmgrp->region->maxtxns = maxtxns;
        if ((ret = __txn_init(tmgrp->region)) != 0)
            goto err;
    } else if (tmgrp->region->magic != DB_TXNMAGIC) {
        /* Check if valid region. */
        __db_err(dbenv, "txn_open: Bad magic number");
        ret = EINVAL;
        goto err;
    }
    // 如果这是一个新的事务共享区域,初始化之。这里又和MPOOL一样,
    // 使用__shmalloc函数管理共享区域内存
    if (LF_ISSET(DB_THREAD)) {
        if ((ret = __db_shalloc(tmgrp->mem, sizeof(db_mutex_t),
            MUTEX_ALIGNMENT, &tmgrp->mutexp)) == 0)
            /*
            * Since we only get here if threading is turned on, we
            * know that we have spinlocks, so the offset is going
            * to be ignored.  We put 0 here as a valid placeholder.
            */
            __db_mutex_init(tmgrp->mutexp, 0);
        if (ret != 0)
            goto err;
    }
    UNLOCK_TXNREGION(tmgrp);
    *mgrpp = tmgrp;
    return (0);
err:    if (tmgrp->reginfo.addr != NULL) {
        if (tmgrp->mutexp != NULL)
            __db_shalloc_free(tmgrp->mem, tmgrp->mutexp);
        UNLOCK_TXNREGION(tmgrp);
        (void)__db_rdetach(&tmgrp->reginfo);
        if (F_ISSET(&tmgrp->reginfo, REGION_CREATED))
            (void)txn_unlink(path, 1, dbenv);
    }
    if (tmgrp->reginfo.path != NULL)
        __os_freestr(tmgrp->reginfo.path);
    __os_free(tmgrp, sizeof(*tmgrp));
    return (ret);
}
</code>

这个函数就是txn_open()调用来初始化事务共享区域的函数:

<code>/*
* This file contains the top level routines of the transaction library.
* It assumes that a lock manager and log manager that conform to the db_log(3)
* and db_lock(3) interfaces exist.
*
* Initialize a transaction region in shared memory.
* Return 0 on success, errno on failure.
*/
static int
__txn_init(txn_region)
    DB_TXNREGION *txn_region;
{
    time_t now;
    (void)time(&now);
    /* maxtxns is already initialized. */
    txn_region->magic = DB_TXNMAGIC;
    txn_region->version = DB_TXNVERSION;
    txn_region->last_txnid = TXN_MINIMUM;
    /*
    * XXX
    * If we ever do more types of locking and logging, this changes.
    */
    txn_region->logtype = 0;
    txn_region->locktype = 0;
    txn_region->time_ckp = now;
    ZERO_LSN(txn_region->last_ckp);
    ZERO_LSN(txn_region->pending_ckp);
    SH_TAILQ_INIT(&txn_region->active_txn);
    __db_shalloc_init((void *)&txn_region[1],
        TXN_REGION_SIZE(txn_region->maxtxns) - sizeof(DB_TXNREGION));
    return (0);
}
</code>

其中DB_TXN{}是每个事务的描述符(定义在db_int.h文件中):

<code>/* The structure allocated for every transaction. */
struct __db_txn {
    DB_TXNMGR    *mgrp;        /* Pointer to transaction manager. */
    DB_TXN        *parent;    /* Pointer to transaction's parent. */
    DB_LSN        last_lsn;    /* Lsn of last log write. */
    u_int32_t    txnid;        /* Unique transaction id. */
    size_t        off;        /* Detail structure within region. */
    TAILQ_ENTRY(__db_txn) links;    /* Links transactions off manager. */
    TAILQ_HEAD(__kids, __db_txn) kids; /* Child transactions. */
    TAILQ_ENTRY(__db_txn) klinks;    /* Links child transactions. */
#define    TXN_MALLOC    0x01        /* Structure allocated by TXN system. */
    u_int32_t    flags;
};
typedef struct __txn_detail {
    u_int32_t txnid;        /* current transaction id
                    used to link free list also */
    DB_LSN    last_lsn;        /* last lsn written for this txn */
    DB_LSN    begin_lsn;        /* lsn of begin record */
    size_t    last_lock;        /* offset in lock region of last lock
                    for this transaction. */
    size_t    parent;            /* Offset of transaction's parent. */
#define    TXN_UNALLOC    0
#define    TXN_RUNNING    1
#define    TXN_ABORTED    2
#define    TXN_PREPARED    3
#define    TXN_COMMITTED    4
    u_int32_t status;        /* status of the transaction */
    SH_TAILQ_ENTRY    links;        /* free/active list */
#define    TXN_XA_ABORTED        1
#define    TXN_XA_DEADLOCKED    2
#define    TXN_XA_ENDED        3
#define    TXN_XA_PREPARED        4
#define    TXN_XA_STARTED        5
#define    TXN_XA_SUSPENDED    6
    u_int32_t xa_status;        /* XA status */
    /*
    * XID (xid_t) structure: because these fields are logged, the
    * sizes have to be explicit.
    */
    DB_XID xid;            /* XA global transaction id */
    u_int32_t bqual;        /* bqual_length from XID */
    u_int32_t gtrid;        /* gtrid_length from XID */
    int32_t format;            /* XA format */
} TXN_DETAIL;
</code>
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Lock wait timeout exceeded; try restarting transaction - 如何解决MySQL报错:事务等待超时Lock wait timeout exceeded; try restarting transaction - 如何解决MySQL报错:事务等待超时Oct 05, 2023 am 08:46 AM

Lockwaittimeoutexceeded;tryrestartingtransaction-如何解决MySQL报错:事务等待超时在使用MySQL数据库时,有时可能会遇到一个常见的错误:Lockwaittimeoutexceeded;tryrestartingtransaction,该错误表示事务等待超时。这个错误通常发生在并

MySQL事务处理:自动提交与手动提交的区别MySQL事务处理:自动提交与手动提交的区别Mar 16, 2024 am 11:33 AM

MySQL事务处理:自动提交与手动提交的区别在MySQL数据库中,事务是一组SQL语句的集合,要么全部执行成功,要么全部执行失败,保证了数据的一致性和完整性。在MySQL中,事务可以分为自动提交和手动提交,其区别在于事务提交的时机以及对事务的控制范围。下面将详细介绍自动提交和手动提交的区别,并给出具体的代码示例来说明。一、自动提交在MySQL中,如果没有显示

MySQL事务的原理及应用场景MySQL事务的原理及应用场景Mar 02, 2024 am 09:51 AM

MySQL事务的原理及应用场景在数据库系统中,事务是一组SQL操作的集合,这些操作要么全部成功执行,要么全部失败回滚。MySQL作为一种常用的关系型数据库管理系统,支持事务的特性,能够确保数据库中的数据在一致性、隔离性、持久性和原子性方面得到保证。本文将从MySQL事务的基本原理入手,介绍其应用场景,并提供具体的代码示例供读者参考。MySQL事务的原理:My

PHP PDO教程:从基础到精通的进阶指南PHP PDO教程:从基础到精通的进阶指南Feb 19, 2024 pm 06:30 PM

1.PDO简介PDO是PHP的一个扩展库,它提供了一个面向对象的方式来操作数据库。PDO支持多种数据库,包括Mysql、postgresql、oracle、SQLServer等。PDO使开发人员能够使用统一的api来操作不同的数据库,这使得开发人员可以在不同的数据库之间轻松切换。2.PDO连接数据库要使用PDO连接数据库,首先需要创建一个PDO对象。PDO对象的构造函数接收三个参数:数据库类型、主机名、数据库用户名和密码。例如,以下代码创建了一个连接到mysql数据库的对象:$dsn="mysq

Java数据库连接如何处理事务和并发?Java数据库连接如何处理事务和并发?Apr 16, 2024 am 11:42 AM

事务确保数据库数据完整性,包括原子性、一致性、隔离性和持久性。JDBC使用Connection接口提供事务控制(setAutoCommit、commit、rollback)。并发控制机制协调并发操作,使用锁或乐观/悲观并发控制来实现事务隔离性,以防止数据不一致。

掌握 PHP PDO 的力量:高级查询和更新掌握 PHP PDO 的力量:高级查询和更新Feb 20, 2024 am 08:24 AM

PHP数据对象(PDO)扩展提供了与数据库服务器高效且面向对象的交互。其高级查询和更新功能使开发人员能够执行复杂的数据库操作,从而提高性能和代码可维护性。本文将深入探讨PDO的高级查询和更新功能,指导您掌握其强大功能。高级查询:使用占位符和绑定参数占位符和绑定参数是提高查询性能和安全性的重要工具。占位符使用问号(?)表示查询中可替换的参数,而绑定参数则允许指定每个参数的数据类型和值。通过使用这些方法,您可以避免sql注入攻击并提高性能,因为数据库引擎可以提前优化查询。//使用占位符$stmt=$

MongoDB技术开发中遇到的事务管理问题解决方案分析MongoDB技术开发中遇到的事务管理问题解决方案分析Oct 08, 2023 am 08:15 AM

MongoDB技术开发中遇到的事务管理问题解决方案分析随着现代应用程序变得越来越复杂和庞大,对数据的事务处理需求也越来越高。作为一种流行的NoSQL数据库,MongoDB在数据管理方面有着出色的性能和扩展性。然而,MongoDB在数据一致性和事务管理方面相对较弱,给开发人员带来了挑战。在本文中,我们将探讨在MongoDB开发中遇到的事务管理问题,并提出一些解

Java EJB架构详解,构建稳定可扩展的系统Java EJB架构详解,构建稳定可扩展的系统Feb 21, 2024 pm 01:13 PM

什么是EJB?EJB是一种Java平台企业版(JavaEE)规范,定义了一组用于构建服务器端企业级Java应用程序的组件。EJB组件封装了业务逻辑,并提供了一组用于处理事务、并发、安全性和其他企业级关注点的服务。EJB体系结构EJB体系结构包括以下主要组件:企业Bean:这是EJB组件的基本构建块,它封装了业务逻辑和相关的数据。EnterpriseBean可以是无状态的(也称为会话bean)或有状态的(也称为实体bean)。会话上下文:会话上下文提供有关当前客户端交互的信息,例如会话ID和客户端

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具