在PostgreSQL,你可以簡單地運行這個本地查詢來獲取當前事務ID:
Number transactionId = (Number) session
.createSQLQuery("select txid_current()")
.uniqueResult();
對於MySQL,you need to run 5.7 or later:
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();
'INNODB_TRX'給出所有活動事務。我需要獲得當前的交易。 –