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

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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值