【转】怎么获得数据库的当前transaction ID

本文介绍了如何在Oracle、SQL Server、PostgreSQL、MySQL/MariaDB和HSQLDB中使用SQL查询来获取当前运行的数据库事务ID。对于每种数据库系统,都提供了具体的SQL语句,并解释了这些语句的工作原理。

How to get the current database transaction id

 

Oracle

When using Oracle, you have to execute the following SQL query:

1

2

3

SELECT RAWTOHEX(tx.xid)

FROM v$transaction tx

JOIN v$session s ON tx.ses_addr = s.saddr

The v$transaction view provides information about the currently running database transactions. However, there can be multiple transactions running in our system, and that’s why we are joining the v$transaction with the v$session view.

The v$session view offers information about our current session or database connection. By matching the session address between the v$transaction and v$session views, we can find the current running transaction identifier given by the xid column in the v$transaction view.

Because the xid column is of type RAW, we are using RAWTOHEX to convert the transaction identifier binary value to its hexadecimal representation.

Oracle assigns a transaction identifier only if it needs to assign an undo segment, which implies that an INSERT, UPDATE or DELETE DML statement has been executed.

So, read-only transactions will not have a transaction identifier assigned. For more details about the undo log, check out this article.

SQL Server

When using SQL Server, you just have to execute the following SQL query:

1

SELECT CONVERT(VARCHAR, CURRENT_TRANSACTION_ID())

Because the CURRENT_TRANSACTION_ID function returns a BIGINT column value, we are using CONVERT to get its String representation.

PostgreSQL

When using PostgreSQL Server, you can execute the following SQL query to get the current transaction id:

1

SELECT CAST(txid_current() AS text)

Because the txid_current function returns a BIGINT column value, we are using CAST to get its String representation.

MySQL and MariaDB

When using MySQL or MariaDB, you can execute the following SQL query to get the current transaction id:

1

2

3

SELECT tx.trx_id

FROM information_schema.innodb_trx tx

WHERE tx.trx_mysql_thread_id = connection_id()

The innodb_trx view in the information_schema catalog provides information about the currently running database transactions. Since there can be multiple transactions running in our system, we need to filter the transaction rows by matching the session or database connection identifier with the currently running session.

Just like it was the case with Oracle, since MySQL 5.6, only read-write transactions will get a transaction identifier.

Because assigning a transaction id has a given overhead, read-only transactions skip this process. For more details, check out this article.

This read-only transaction optimization works the same way in MariaDB, meaning that a transaction id is only assigned for read-write transactions only.

HSQLDB

When using the HyperSQL database, you can execute the following SQL query to get the current transaction id:

1

VALUES (TRANSACTION_ID())

That’s it!

<think>好的,我现在需要帮用户解决如何在中望CAD中将DWG文件换到数据库后获取当前视口的方法,以及相关的API实现。首先,我需要回忆用户之前的问题和提供的引用内容,确保回答符合他们的需求。 用户提到他们想获取当前视口的方法或代码实现,特别是在DWG换到数据库后。根据引用[3],CAD视口分为模型空间和图纸空间,创建多个视口的步骤。这可能意味着用户需要区分这两种视口,并在换后从数据库中读取视口信息。 首先,我需要考虑中望CAD的API结构。通常,CAD软件的API会有文档对象模型,比如Database、BlockTable、Layout等类。视口可能存储在布局或模型空间中。用户需要访问当前活动视口,这可能在当前布局或模型空间视口中。 接下来,参考引用[4],用户可能已经将DWG换为数据库,所以可能需要通过Database对象来访问布局和视口信息。视口在数据库中可能以BlockReference或Viewport对象存在。 然后,我需要确定如何遍历数据库中的布局,找到当前活动的视口。通常,每个布局(包括Model和Paper Space)都有自己的视口集合。当前活动视口可能是在当前布局中激活的那个。 可能涉及的API类包括Database、Transaction、BlockTable、BlockTableRecord、LayoutManager、Viewport等。代码实现可能需要使用事务来安全地访问数据库对象,遍历布局,检查视口属性,如Viewport.On是否为true,表示当前激活的视口。 需要注意的是,图纸空间中的视口可能是多个,而模型空间通常只有一个视口,但用户可能分割为多个。所以代码可能需要处理这两种情况。 最后,确保代码示例正确使用中望CAD的.NET API,比如使用using语句处理事务,正确获取当前数据库和布局管理器,并过滤出视口实体。 现在,我需要将这些思考整理成结构化的回答,分步骤解释,并提供代码示例,同时确保引用相关引用内容,如引用[3]关于视口类型和创建方法的信息,引用[4]中提到的中望CAD插件可能相关的API使用。</think>在中望CAD二次开发中,通过ObjectARX API获取当前视口的实现可分为以下步骤: ### 一、视口类型区分 根据视口特性需区分模型空间视口和图纸空间视口[^3],关键API方法: ```csharp // 获取当前空间类型 using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord; // 判断是否在图纸空间 if (db.TileMode == false) { LayoutManager.Current.CurrentLayout = "Layout1"; } } ``` ### 二、获取当前视口核心代码 ```csharp using ZwSoft.ZwCAD.ApplicationServices; using ZwSoft.ZwCAD.DatabaseServices; using ZwSoft.ZwCAD.EditorInput; using ZwSoft.ZwCAD.Geometry; public Viewport GetActiveViewport() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; using (Transaction tr = db.TransactionManager.StartTransaction()) { // 获取当前视口ID ObjectId vpId = ed.CurrentViewportObjectId; // 验证是否为有效视口 if (vpId.IsValid && !vpId.IsErased) { Viewport vp = tr.GetObject(vpId, OpenMode.ForRead) as Viewport; if (vp != null && vp.On) { return vp; // 返回激活视口对象 } } // 备选方案:遍历所有视口 ViewportTable vpTable = tr.GetObject(db.ViewportTableId, OpenMode.ForRead) as ViewportTable; foreach (ObjectId id in vpTable) { Viewport tmpVp = tr.GetObject(id, OpenMode.ForRead) as Viewport; if (tmpVp != null && tmpVp.On) { return tmpVp; } } tr.Commit(); } return null; } ``` ### 三、视口关键属性提取 获取视口对象后可访问以下关键信息: ```csharp Viewport vp = GetActiveViewport(); if (vp != null) { Point2d center = vp.CenterPoint; // 视口中心坐标 double height = vp.Height; // 视口高度 double width = vp.Width; // 视口宽度 double scale = vp.CustomScale; // 显示比例 Extents3d view = vp.ViewBound; // 视口边界范围 } ``` ### 四、数据库换后处理 将DWG换到数据库后,需通过事务访问数据库对象[^4]: ```csharp using (Database db = new Database(false, true)) { db.ReadDwgFile("input.dwg", FileOpenMode.OpenForReadAndAllShare, false, null); using (Transaction tr = db.TransactionManager.StartTransaction()) { // 获取布局管理器 LayoutManager layoutMgr = LayoutManager.Current; // 获取当前布局 Layout layout = tr.GetObject( layoutMgr.GetLayoutId(layoutMgr.CurrentLayout), OpenMode.ForRead) as Layout; // 获取布局内视口 ObjectIdCollection vpIds = layout.GetViewports(); foreach (ObjectId id in vpIds) { Viewport vp = tr.GetObject(id, OpenMode.ForRead) as Viewport; // 处理视口数据... } tr.Commit(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值