问题
Is it possible to make any type of custom resource transactioal with JTA? Let's assume a basic example and take a Folder creation that should be rolled back in case of an error.
Can such custom resources be handled?
回答1:
Yes. That resource needs to provide an implementation of XAResource, and an instance of that implementation needs to be enlisted with the transaction when the resource is used. The resource can get the current transaction from the transaction manager in order to carry out the registration when it is accessed (i think).
You could look at the code to XADisk, which is apparently an XA-transactional file manager, to see how it's done there.
However, a common approach to doing this in the J2EE world is not to deal with JTA directly, but to write a resource adapter using JCA, which lets the J2EE container handle not only transactions, but pooling, naming, and various other enterprisey details. However, the JCA API is not exactly straightforward, and as difficult as it is, it is defended by a mountain of even more difficult documentation, whose main purpose seems to be to prevent understanding of it!
来源:https://stackoverflow.com/questions/6736241/using-arbitrary-resources-with-jta