使用jdbc/hibernate获取当前的数据库事务ID? [英] Get current database transaction id using jdbc/hibernate?
问题描述
为此,我已经在Google上四处浏览,但找不到任何相关内容.基本上,我想掌握长期运行的事务.
I've looked around on Google for this but couldn't find anything relevant. Basically, I want to get hold of long running transactions.
现在,我浏览information_schema.INNODB_TRX
或查看show engine innodb status
的输出以找到trx_id
,然后打开general_logs
来查看所有查询正在运行.
For now, I go through information_schema.INNODB_TRX
or have a look at the output of show engine innodb status
to find the trx_id
and then turn on general_logs
to see what all queries are running.
有没有办法,可以使用jdbc
或hibernate
在代码中保留此transaction_id
,以便可以将其记录在服务器日志中?
Is there a way, I can get hold of this transaction_id
in my code using jdbc
or hibernate
so that I can log it in my server logs?
推荐答案
使用PostgreSQL,您可以简单地运行此本机查询以获取当前交易ID:
With PostgreSQL, you could simply run this native query to get the current transaction id:
Number transactionId = (Number) session
.createSQLQuery("select txid_current()")
.uniqueResult();
对于MySQL,您需要运行5.7或更高版本:
Number transactionId = (Number) session
.createSQLQuery(
"SELECT GTID " +
"FROM events_transactions_current e " +
"JOIN performance_schema.threads t ON e.THREAD_ID = t.THREAD_ID " +
"WHERE t.PROCESSLIST_ID = CONNECTION_ID()")
.uniqueResult();
或对于MySQL 5.5:
or for MySQL 5.5:
Number transactionId = (Number) session
.createSQLQuery(
"SELECT trx_id " +
"FROM information_schema.innodb_trx " +
"WHERE trx_mysql_thread_id = CONNECTION_ID()")
.uniqueResult();
这篇关于使用jdbc/hibernate获取当前的数据库事务ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!