3

I have a Java SE(!) scenario with JMS and JPA where I might need distributed transactions as well as "regular" JDBC transactions. I have to listen to a queue which sends service requests, persist log on receiving, process the request and update the log after the request has been processed. The message shall only be acknowledged if the request has been processed successfully.

The first idea was to only use JTA (provided by Bitronix). But there I face two problems:

  1. no log will be persisted if the request can't be processed
  2. the request won't be processed if the log can't be updated (unlikely but yet possible)

So the other idea is to create and update the log with regular JDBC transactions. Only the entitymanager(s) for the request transaction(s) would join the user transactions and the entity managers for creating and updating the log would commit directly.

Is it possible to "mix" JTA and JPA on a single persistence unit? Or do we already have patterns for those kinds of JMS and JDBC transactions?

4

1 に答える 1

2

私は実際に少し異なるアプローチで問題を解決しました。JTAトランザクションとJDBCトランザクションを「混合」する代わりに、サスペンドレジュームを使用して、さまざまなユーザートランザクションを処理しました。

タスクは同じです。いくつかのJMSおよびJDBCトランザクションを含む(JTA)ユーザートランザクションを開始します(メッセージの受信、いくつかのデータベース操作の実行)。そして、そのワークフローの途中で、メッセージログを書きたいのですが、「外部」トランザクションが失敗したときに、そのログはロールバックされません。

したがって、解決策は、擬似コードで次のようになります。

 transactionManager.begin()
 doSomeJdbcStuff();
 Transaction main = transactionManager.suspend();

 // do the logging
 transactionManager.begin()  // <- now a new transaction is created and active!
 doSomeLogging();
 transactionManager.commit()

 // continue
 transactionManager.resume(main);
 doSomeMoreJdbcStuff();
 transactionManager.commit();
于 2013-01-08T14:17:38.277 に答える