Patterns for Binding Non-Transactional Resources into JTA Transactions
Learn more about maintaining consistency when integrating non-transactional systems.
Join the DZone community and get the full member experience.Join For Free
i recently published an article about how to bind non-transactional resources like web services / microservices into global distributed transactions so that recovery is handled automatically. over the years i have often had to integrate "non-transactional" systems into java ee application servers and data consistency was often a topic of discussion or even a non-functional requirement. i've put "non-transactional" into quotes because often the systems contain ways of ensuring data consistency, for example using calls to compensate, but the systems aren't what you might traditionally call transactional. there is certainly no way of configuring a java ee application server to automatically handle recovery for such resources.
the following is a list of patterns that we compiled, showing different ways to maintain consistency when faced with the task of integrating a non-transactional system.
write job to database
- the common scenario whereby you want to send say an email confirmation after a sale is made. you cannot send the email and then attempt to commit the sales transaction to your database, because if the commit fails, the customer receives an email stating that they have bought something and you have no record of it. you cannot send the email after the sales transaction is committed to your database, because if the sending of the email fails (e.g. the mail server is temporarily down), the customer won't get their confirmation, perhaps with a link to the tickets that they bought. one solution is to write the fact that an email needs to be sent, into the database in the same transaction that persists the sale. a batch or
@scheduledejb can then periodically check to see if it should send an email. once it successfully sends an email it changes the state of the record so that the email is not sent again. the same problem applies here that you might only be able to send the email but not update the database. but if you were able to read the database, you are likely to be able to update it, and sending the same email twice because of a database failure isn't as bad as never sending it, as could be the case if you didn't handle sending email asynchronously. one disadvantage of integrating like this is that it means that you cannot integrate a system from which you need the result in order to continue processing your business logic before replying to the user. you must handle the integration asynchronously.
- jms - in a similar scenario to the previous solution, instead of writing a job to the database, you can send a jms message containing the job. jms is transactional, but asynchronous so this solution suffers from the same disadvantages as the solution above. instead of changing the state of the work to be done, if you cannot process the work at that time, you send the message back into the queue with a property so that it is only processed after a certain amount of time, or you send the message to a dead letter queue for manual handling.
- generic connector (jca adapter) - i recently published a blog article describing a generic jca resource adapater that i have created which lets you bind typically non-transactional resources like web services into jta transactions. see the blog article for more details. using the generic connector means that the transaction manager will execute callbacks when the transaction needs to be committed, rolled back or recovered, so that you only need to write application code which responds to these events.
@inject @qualifier event<t>on a field &
field.fire(t);when you want to fire an event &
@observes(during=transactionphase.after_failure) @qualifier ton a method parameter, the method will be called for each fired event, after the transaction fails. this way you can implement some compensation for when the transaction fails. equally, you can use different transaction phases to do different things, like
after_successto perform a call to confirm an initial reservation. we have even used these mechanisms to delay the call to the remote system, for example to post work to a workflow engine just before the commit, so that we are sure that all validation logic in the complex process has completed before the remote system call is made. see number 12 below.
- custom solution - if you can really really justify if, then you could build complex code with timeouts etc. involving batches and scripts which handle committing, rolling back and recovering transactions using the remote resource. the question you need to ask yourself is whether you are an expert in writing business code, or an expert in effectively writing transaction managers.
- business process engine - modern engines can integrate all kinds of remote resources into business processes and they tend to handle things like failure recovery. they typically retry failed calls and they can durably handle process state during the time it takes for remote systems to become online again so that the process can be resumed. rather than commit and rollback, bpel supports compensation to guarantee consistency across the entire landscape.
- atomikos & tcc - a product which is capable of binding web services into jta transactions. so far as i can tell, it is a stand alone transaction manager which can run outside of a java ee application server. but i have no experience with this product.
- ws-at - using proprietary configuration (and/or annotations) you can set up two application servers to do their work within a global transaction. while this sounds promising, i have yet to come across a productive system which implements ws-at. really only supports soap web services, although jboss has something in the pipeline for supporting rest services.
- ejb - remote ejbs: java ee application servers have been able to propgate transaction contexts from one server to another for a relatively long time. if you need to call a service that happens to be implemented using the java ee stack, why not call it using remote ejb rather than calling it say over a web service, so that you get the service bound into a global transaction for free? local ejbs: if the service you are calling happens to be written in java using say ejb technology, why not just deploy it locally instead of going to the extra effort to call it remotely say via a soap web service? you might get brownie points with the enterprise architects, but has scalability and composability been compared to performance, consistency and simplicity? sure, modern architectures with trends like microservices mean that deploying lots of remote services is good, but there's always a trade-off being made and you need to really understand it when making the decision about what parts of the landscape need to be accessed remotely.
- transaction callbacks - like solution 4 but using the transaction synchronisation api to register callbacks which are called at the relevant stage of the transaction. the problem here, unlike with cdi events, is that you don't know the context in which the transaction is being committed or rolled back, because the callback is not passed the relevant data unlike the object which is passed into an observing method in cdi. so if you need to compensate the transaction and call say a web service to cancel what you did during the transaction, where do you get the data that you need to do so?
enlist xa resource into transaction
- add a custom implementation of the
interface, which you enlist into the transaction using the
enlistresourcemethod. unfortunately the commit/rollback methods are only called once and if they should fail, they won't be called again during recovery.
- non-transactional resource last - if no other pattern can be implemented, and you don't need to call the resource at a specific time during the process, e.g. you need to send an email as part of the transaction, but it doesn't matter if you do it as the first or last process step, then always call it right at the end of the process, shortly before the transaction is committed. the chances of the transaction not being able to commit is relatively small (especially if all the sql has been flushed to the database), compared to the chances of your remote system call failing. if the call fails, then rollback the transaction. if the call succeeds, then commit the transaction. if the transaction then fails during commit, and it is important to you to compensate the non-transactional resource, you will need to use one of the patterns described above to add some compensation to the system.
the following table sums up the solutions. the recovery column indicates the level of automated recovery which this solutionsupports.the synchronicity column indicates whether you can use the solution if you need the response in order to continue processing, in which case you need a synchronous solution. synchronicity here has nothing to do with blocking vs. non-blocking, rather it has to do with timing and whether you need a response in order to finish processing an activity.
- manual recovery - you need to program what to do if handling fails, i.e. how often a retry should be attempted before putting work on a "dead letter queue".
- jms will automatically attempt to resend messages if you configure the queue to be durable. but what you do with a failed attempt to handle a message is up to you, the programmer.
- the transaction manager will continuously attempt to commit/rollback incomplete transactions until an administrator steps in to handle long running failures.
- callbacks are only called once so you have just one shot
- a business process engine will repeatedly attempt to re-call failed web service calls. the same is true for compensation. the behaviour is typically configurable.
- remote ejbs: the jta transaction is propagated across to other app servers and as such the coordinating transaction manager will propagate transaction recovery to the other app servers bound into the transaction. local ejbs: using local ejbs means that any calls that they make to the database will be handled in the same transaction as your application code. if the local ejb is using a different database, you should use xa drivers for all databases, message queues, etc., so that the transaction manager can use two phase commit to ensure system-wide consistency.
of all of these, my current favourite is the generic connector . it supports calls from which a response is required, as well as recoverybeing fully automatic. that means that i can concentrate on writing business code, rather than boilerplatey code that really belongs in a framework.
if you know of further ways, please contact me or leave a comment so that i can add them to the list.
Published at DZone with permission of Ant Kutschera, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.