实现自定义 JTA XAResource 以与 hibernate 一起使用

发布于 2024-08-28 17:34:28 字数 179 浏览 4 评论 0原文

我对数据库有两级访问:第一个使用 Hibernate,第二个使用 JDBC。 JDBC 级别使用非事务表(我使用 MyISAM 来提高速度)。我想让这两个级别在事务中工作。我读到了有关 JTA 的信息,它可以管理分布式事务。但互联网上缺乏关于如何实现和使用自定义资源的信息。

有人有使用自定义 XAResources 的经验吗?

I have two level access to database: the first with Hibernate, the second with JDBC. The JDBC level work with nontransactional tables (I use MyISAM for speed). I want make both levels works within transaction. I read about JTA which can manage distributed transactions. But there is lack information in the internet about how to implement and use custom resource.

Does any one have experience with using custom XAResources?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄离 2024-09-04 17:34:28

我想让两个级别都在事务中工作。

然后你必须将存储引擎更改为 InnoDB,MyISAM 表不支持事务(从技术上讲,你不会收到错误,但回滚不会回滚任何内容)。

有人有使用自定义 XAResources 的经验吗?

我不确定你在说什么。我在这里看到的唯一 XA 资源是您的数据库,您不需要实现任何自定义内容。您需要做的是使用很可能从两个 XA 数据源(受 MySQL Connector/J 5.0.0+ 支持)获得的 XA 连接,使用 JTA API 并让事务管理器完成其工作。

但说实话,你应该明确自己的要求。可能还有比使用 XA 更简单的其他选择。如果以上所有内容听起来都像中文,那么我感觉不要使用 XA 是一个很好的建议。

I want make both levels works within transaction.

Then you'll have to change your storage engine for InnoDB, MyISAM tables do not support transactions (technically, you won't get an error but a rollback won't rollback anything).

Does any one have experience with using custom XAResources?

I'm not sure to understand what you're talking about. The only XA resource I see here is your database and you don't need to implement anything custom. What you need to do is to use XA connections very likely obtained from two XA DataSources (which are supported by MySQL Connector/J 5.0.0+), use the JTA API and let the Transaction Manager do its job .

But to be honest, you should really clarify your requirements. There might be other (and easier) options than using XA. And if all the above sounds like Chinese, then I have the feeling that don't use XA would be a good advice here.

ゃ懵逼小萝莉 2024-09-04 17:34:28

连接是通过可以配置为支持或不支持分布式事务的数据源获得的。要在分布式事务中使用多个连接,必须配置多个 DataSource 以支持 XA 并返回 XA 连接。

也就是说,仅当您连接到不同数据库时才需要多个物理连接,这似乎不符合您的情况(问题中不清楚)。

DataSource 可以足够智能,确保只要处于同一个线程中,就使用相同的物理连接;每次请求连接时,它实际上都会返回同一个物理连接的“句柄”,当所有句柄都已关闭时,该物理连接就会返回到池中。 (但这取决于数据源的实现)。

Hibernate 本身不是 XA 资源:它使用通过 DataSource 获得的底层连接。但它通过 JTA 将自身挂接到事务管理器中,特别是在分布式事务提交之前刷新所有挂起的更改。

大多数时候,您可以使用特定于实现的 API 来获取 EntityManager 使用的底层连接(至少对于 Hibernate 来说是可能的)。这意味着您可以完全不使用 JTA 和 XA 来满足您的要求:使用 EntityManager 的底层连接来处理您的 JDBC 内容。

总结:

  1. 无需搞乱 XAResource
  2. 切换到 InnoDB
  3. 您可以尝试切换到 XA DataSource 并使用 DataSource.getConnection() 获取连接
  4. 您可以尝试切换到 XA DataSource 并获取底层 EntityManager 连接
  5. 您可以尝试坚持使用非 XA 数据源并获取底层 EntityManager 连接

希望我正确理解您的问题并且它有帮助。

Connection are obtained via a DataSource that can be configured to support distributed transaction or not. To use multiple connections in a distributed transaction, you must configure the multiple DataSource to support XA and return XA connections.

That said, you need several physical connections only if you connects to different database, which doesn't seem to be your case (that's not clear in the question).

A DataSource can be smart enough to make sure that the same physical connection is used as long as you are in the same thread; each time you ask for a connection, it actually returns a "handle" to the same physical connection, and the physical connection returns to the pool when all handles have been closed. (But that depends on the DataSource implementation).

Hibernate per se is not an XA resource: it uses underlying a connection obtained via a DataSource. But it hooks itself in the transaction manager via JTA, in particular to flush all pending changes before the distributed transaction commits.

You can most of the time obtain the underlying connection used by the EntityManager using implementation specific API (it's at least possible with Hibernate). This means that you can maybe fulfill your requirement without using JTA and XA at all: use the underlying connection of the EntityManager for your JDBC stuffs.

In summary:

  1. No need to mess with XAResource
  2. Switch to InnoDB
  3. You can try to switch to a XA DataSource and obtain a connection with DataSource.getConnection()
  4. You can try to switch to a XA DataSource and obtain the underlying EntityManager connection
  5. You can try to stick with a non-XA DataSource and obtain the underlying EntityManager connection

Hope I understood your question right and that it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文