首页 > 其他分享 >长事务 (Long Transactions)

长事务 (Long Transactions)

时间:2022-11-05 10:55:35浏览次数:64  
标签:事务 Transactions 对象 数据库 Long rb AcDbLongTransaction AcDb pEntity

长事务

长事务用于支持 AutoCAD 参照编辑功能,对于 ObjectARX 应用程序非常有用。这些类和函数为应用程序提供了一种方案,用于签出实体以进行编辑并将其签回其原始位置。此操作会将原始对象替换为已编辑的对象。有三种类型的长期交易结帐:

  • 从同一图形中的普通块
  • 从图形的外部参照 (外部参照)
  • 从不相关的临时数据库

长事务类和函数概述

主要的长事务类和函数是

  • AcDbLongTransaction.class
  • AcDbLongTransWorkSetIterator.class
  • AcApLongTransactionReactor.class
  • AcApLongTransactionManager.class
  • wblockCloneObject.class

AcDbLongTransaction Class

AcDbLongTransaction是包含跟踪长事务所需信息的类。该类AcDbLongTransactionManager负责创建AcDbLongTransaction对象并将其追加到数据库。然后它返回对象AcDbLongTransactionAcDbObjectId。与所有其他驻留在数据库的对象一样,其销毁由数据库处理。

注意AcDbLongTransaction对象在处于活动状态时添加到数据库中,并在事务完成后擦除。它们不存储在 DWG 或 DXF 文件中,因此不是持久性的。 

AcDbLongTransWorkSetIterator Class

AcDbLongTransWorkSetIterator提供对工作集中对象的只读访问权限。在构造AcDbLongTransaction::newWorkSetIterator()期间,可以将其设置为仅包含活动工作集,或者包括添加到工作集的对象,因为它们被工作集中的对象(辅助对象)引用。它还可以处理从工作集中移除的对象,无论是通过擦除AcDbLongTransaction::removeFromWorkSet()还是被擦除。

AcApLongTransactionReactor Class

AcApLongTransactionReactor提供特定于长事务操作的通知。它旨在与也将发送的深层克隆通知结合使用,但会因正在执行的签出/签入类型而异。要将这些通知与深层克隆通知连接起来,可以通过调用AcDbLongTransaction::activeIdMap()函数来检索用于克隆的对象AcDbIdMapping

AcApLongTransactionManager Class

AcApLongTransactionManager是用于启动和控制多头事务的管理器。每个 AutoCAD 会话只有一个acapLongTransactionManager,可通过对象返回的指针进行访问。

AcDbDatabase::wblockCloneObjects() Function

wblockCloneObjects()函数是AcDbDatase的成员。它将对象从一个数据库深度克隆到另一个数据库,并遵循硬引用,以便所有依赖对象也被克隆。当发现重复项时,符号表记录的行为由类型参数确定。下图显示了符号表类型 (enum DuplicateRecordCloning) 和深层克隆类型 (enum DeepCloneType) 之间的关系。

 

Relationship between DeepCloneTypes and DuplicateRecordCloning for different commands and functions 

Command or API Function

DeepCloneType

DuplicateRecordCloning

命令或 API 函数

深度克隆类型

复制记录克隆

copy

kDcCopy

kDrcNotApplicable

explode

kDcExplode

kDrcNotApplicable

block

kDcBlock

kDrcNotApplicable

INSERT/BIND

kDcXrefInsert

kDrcIgnore

XRESOLVE

kDcSymTableMerge

kDrcXrefMangleName

INSERT

kDcInsert

kDrcIgnore

insert()

kDcInsertCopy

kDrcIgnore

WBLOCK

kDcWblock

kDrcNotApplicable

deepCloneObjects()

kDcObjects

kDrcNotApplicable

wblockObjects()

kDcObjects

kDrcIgnore

wblockObjects()

kDcObjects

kDrcReplace

wblockObjects()

kDcObjects

kDrcMangleName

wblockObjects()

kDcObjects

kDrcUnmangleName

长事务示例

这个简单的示例演示如何从另一个数据库中签出实体,在当前数据库中修改它们,然后将其签回原始数据库。作为长事务过程一部分的调用以粗体显示。

void
refEditApiExample()
{
AcDbObjectId transId;
AcDbDatabase* pDb;
TCHAR *fname;
struct resbuf *rb;
    // Get a dwg file from the user.
    //
    rb = acutNewRb(RTSTR);
    int stat = acedGetFileD(_T("Pick a drawing"), NULL, _T("dwg"),
 0, rb);
if ((stat != RTNORM) || (rb == NULL)) {
        acutPrintf(_T("\nYou must pick a drawing file."));        return;    }
fname = (TCHAR*)acad_malloc((_tcslen(rb->resval.rstring) + 1) *
 sizeof(TCHAR));
    _tcscpy(fname, rb->resval.rstring);
    acutRelRb(rb);
// Open the dwg file.
    //
    pDb = new AcDbDatabase(Adesk::kFalse);
    if (pDb->readDwgFile(fname) != Acad::eOk) {
        acutPrintf(_T("\nSorry, that drawing is probably already
          open."));
        return;
    }
    // Get the Block Table and then the model space record.
    //
    AcDbBlockTable *pBlockTable;
    pDb->getSymbolTable(pBlockTable, AcDb::kForRead);
    AcDbBlockTableRecord *pOtherMsBtr;
    pBlockTable->getAt(ACDB_MODEL_SPACE, pOtherMsBtr,
      AcDb::kForRead);
    pBlockTable->close();
    // Create an iterator.
    //
    AcDbBlockTableRecordIterator *pIter;
    pOtherMsBtr->newIterator(pIter);
    // Set up an object ID array.
    //
    AcDbObjectIdArray objIdArray;
    // Iterate over the model space BTR. Look specifically 
    // for lines and append their object ID to the array.
    //
    for (pIter->start(); !pIter->done(); pIter->step()) {
        AcDbEntity *pEntity;
        pIter->getEntity(pEntity, AcDb::kForRead);
        // Look for only AcDbLine objects and add them to the 
        // object ID array.
        //
        if (pEntity->isKindOf(AcDbLine::desc())) {
            objIdArray.append(pEntity->objectId());
        }
        pEntity->close();
    }
    delete pIter;
    pOtherMsBtr->close();
    if (objIdArray.isEmpty()) {
        acad_free(fname);
        acutPrintf(_T("\nYou must pick a drawing file that contains
          lines."));
        return;
    }
    // Now get the current database and the object ID for the
    // current database's model space BTR.
    //
    AcDbBlockTable *pThisBlockTable;
    acdbHostApplicationServices()->workingDatabase()->
      getSymbolTable(pThisBlockTable, AcDb::kForRead);
    AcDbBlockTableRecord *pThisMsBtr;
    pThisBlockTable->getAt(ACDB_MODEL_SPACE, pThisMsBtr,
      AcDb::kForWrite);
    pThisBlockTable->close();
    AcDbObjectId id = pThisMsBtr->objectId();
    pThisMsBtr->close();
    // Create the long transaction. This will check all the entities 
    // out of the external database.
    //
    AcDbIdMapping errorMap;
    acapLongTransactionManagerPtr()->checkOut(transId, objIdArray,
      id, errorMap);
    // Now modify the color of these entities.
    //
    int colorIndex;
    acedGetInt(_T("\nEnter color number to change entities to: "),
      &colorIndex);
    AcDbObject* pObj;
    if (acdbOpenObject(pObj, transId, AcDb::kForRead) == Acad::eOk) {
        // Get a pointer to the transaction.
        //
        AcDbLongTransaction* pLongTrans =
          AcDbLongTransaction::cast(pObj);
        if (pLongTrans != NULL) {
            // Get a work set iterator.
            //
            AcDbLongTransWorkSetIterator* pWorkSetIter;
            pLongTrans->newWorkSetIterator(pWorkSetIter);
            // Iterate over the entities in the work set and change
            // the color.
            for (pWorkSetIter->start(); !pWorkSetIter->done();
               pWorkSetIter->step()) {
                AcDbEntity *pEntity;
                acdbOpenAcDbEntity(pEntity, pWorkSetIter->objectId(),
                  AcDb::kForWrite);
                pEntity->setColorIndex(colorIndex);
                pEntity->close();
            }
            delete pWorkSetIter;
        }
        pObj->close();
    }
    // Pause to see the change.
    //
    TCHAR str[132];
    acedGetString(0, _T("\nSee the new colors. Press return to check
      the object into the original database"), str);
    // Check the entities back in to the original database.
    //
    acapLongTransactionManagerPtr()->checkIn(transId, errorMap);
    // Save the original database, since we have made changes.
    //
    pDb->saveAs(fname);
    // Close/Delete the database
    //
    delete pDb;
    pDb = NULL;
    acad_free(fname);
}

标签:事务,Transactions,对象,数据库,Long,rb,AcDbLongTransaction,AcDb,pEntity
From: https://www.cnblogs.com/HRDK-CADeveloper/p/16859785.html

相关文章

  • 清理分布式事务时hang住
    1.清理分布式事务语句:select'rollbackforce'||''''||local_tran_id||''''||';'"RollBack"fromdba_2pc_pendingwherestate='prepared';select'execdbms_transactio......
  • [LeetCode] 2131. Longest Palindrome by Concatenating Two Letter Words
    Youaregivenanarrayofstrings words.Eachelementof words consistsof two lowercaseEnglishletters.Createthe longestpossiblepalindrome bysele......
  • Redis Desktop Manager连不上服务器的redis; Java 死锁; 泛型; 事务; servlet; Spring事务;
    RedisDesktopManager连不上服务器的redis1、首先有没有设置密码,输入的密码是否正确2、那个IP是内网IP嘛?外网的要加SSH总结一下无论是连MySQL、MongoDB、还是redisip如果......
  • Spring事务简介
    1、事务作用:在数据层保证一系列的数据库操作同成功同失败Spring事务作用:在数据层或业务层保障一系列的数据库操作同成功同失败2、例子:模拟银行账户间转账业务  步骤......
  • String[] 转Long[]
    publicstaticLong[]getLongValues(String[]array){if(array==null)returnnull;return(Long[])ConvertUtils.convert(array,Long.class);}......
  • 分布式事务的性能设计
    分布式事务的性能设计,主要包含吞吐量和相应延迟两方面。分布式事务更适合对吞吐量要求高,对相应延迟要求不苛刻的应用。性能设计有三个层面,分别为代码优化层、算法逻辑层以及......
  • 深入理解 Spring 事务原理
    Spring事务的基本原理Spring事务的本质其实就是数据库对事务的支持,没有数据库的事务支持,spring是无法提供事务功能的。对于纯JDBC操作数据库,想要用到事务,可以按照以下步骤......
  • Spring 源码(七)Spring 事务源码解析
    注册后置处理器开启对事务的支持@EnableTransactionManagement​​@EnableTransactionManagement​​注解的主要作用是开启对事务的支持,源码如下:@Target(ElementType.TYPE)@......
  • 使用MyBatis时需要注意到的事情------执行添加操作时,一定要记得提交事务
    今天在重写添加操作代码时,发现自己写的代码没有任何报错,使用断点进行查询,发现一切正常,但是注册使用的数据就是无法添加到数据库里面然后就去之前看过的视频里面去找错误,就......
  • java实现分布式事务的三种方案
    原子性:执行单元中的操作要么全部执行成功,要么全部失败。如果有一部分成功一部分失败那么成功的操作要全部回滚到执行前的状态。一致性:执行一次事务会使用数据从一个正确的......