使用jdbc / hibernate获取当前数据库事务ID?
我已经浏览了Google,但找不到任何相关内容。 基本上,我希望掌握长期运行的交易。
现在,我将浏览information_schema.INNODB_TRX
或查看show engine innodb status
的输出以查找trx_id
,然后打开general_logs
以查看所有查询的运行情况。
有没有办法,我可以使用jdbc
或hibernate
在我的代码中获取此transaction_id
,以便我可以在我的服务器日志中记录它?
使用PostgreSQL,您只需运行此本机查询即可获取当前事务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:
Number transactionId = (Number) session .createSQLQuery( "SELECT trx_id " + "FROM information_schema.innodb_trx " + "WHERE trx_mysql_thread_id = CONNECTION_ID()") .uniqueResult();