JTA和本地交易之间有什么区别? [英] What is the difference between JTA and a local transaction?

查看:73
本文介绍了JTA和本地交易之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JTA与本地交易之间有什么区别?

一个显示何时使用JTA以及何时使用本地事务的示例很好.

解决方案

JTA是用于管理Java中事务的通用API.它允许您以资源中立的方式启动,提交和回滚事务.事务状态通常存储在TLS(线程本地存储)中,并且可以传播到调用堆栈中的其他方法,而无需传递某些显式上下文对象.交易资源可以加入正在进行的交易.如果参与此事务的资源不止一个,则其中至少一个必须是所谓的XA资源.

resource local transaction是使用特定的单个资源使用其自己的特定API进行的事务.这样的事务通常不会传播到调用堆栈中的其他方法,因此您需要传递一些显式的上下文对象.在大多数资源本地事务中,不可能有多个资源参与同一事务.

您将在Java SE中的低级JDBC代码中使用资源本地事务.在这里,上下文对象由java.sql.Connection的实例表示.资源本地事务的其他示例是开发人员在2002年左右创建企业应用程序.由于事务管理器(由JTA使用)是昂贵的,封闭源代码的,并且在那个时代需要设置复杂的东西,因此人们选择了更便宜,更容易获得资源本地变体的产品. /p>

基本上在所有其他情况下,您都将使用JTA事务.非常简单,小型,免费和开源的服务器,例如TomEE(25MB)或GlassFish(35MB),都具有开箱即用的JTA支持.没有任何设置,它们可以正常工作.

最后,通过提供declarative transactions,诸如EJB和Spring之类的技术甚至使JTA更加易于使用.在大多数情况下,建议使用它们,因为它们更容易,更清洁且更不易出错. EJB和Spring都可以在后台使用JTA.

What is the difference between JTA and a local transaction?

An example that shows when to use JTA and when to use a local transaction would be great.

解决方案

JTA is a general API for managing transactions in Java. It allows you to start, commit and rollback transactions in a resource neutral way. Transactional status is typically stored in TLS (Thread Local Storage) and can be propagated to other methods in a call-stack without needing some explicit context object to be passed around. Transactional resources can join the ongoing transaction. If there is more than one resource participating in such a transaction, at least one of them has to be a so-called XA resource.

A resource local transaction is a transaction that you have with a specific single resource using its own specific API. Such a transaction typically does not propagate to other methods in a call-stack and you are required to pass some explicit context object around. In the majority of the resource local transactions it's not possible to have multiple resources participating in the same transaction.

You would use a resource local transaction in for instance low-level JDBC code in Java SE. Here the context object is expressed by an instance of java.sql.Connection. Other examples of resource local transactions are developers creating enterprise applications around 2002. Since transaction managers (used by JTA) were expensive, closed source and complicated things to setup around that era, people went with the cheaper and easier to obtain resource local variants.

You would use a JTA transaction in basically every other scenario. Very simple, small, free and open-source servers like TomEE (25MB) or GlassFish (35MB) have JTA support out of the box. There's nothing to setup and they Just Work.

Finally, technologies like EJB and Spring make even JTA easier to use by offering declarative transactions. In most cases it's advised to use those as they are easier, cleaner and less error prone. Both EJB and Spring can use JTA under the covers.

这篇关于JTA和本地交易之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆