实现自定义JTA XAResource以与Hibernate一起使用 [英] Implement custom JTA XAResource for using with hibernate

查看:112
本文介绍了实现自定义JTA XAResource以与Hibernate一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

Does any one have experience with using custom XAResources?

推荐答案

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

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).

只要您在同一线程中,数据源就足够聪明以确保使用相同的物理连接;每次您请求连接时,它实际上都会向同一物理连接返回一个句柄",并且当所有句柄都已关闭时,该物理连接将返回到池中. (但这取决于DataSource的实现.)

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).

休眠本身不是XA资源:它使用通过数据源获得的基础连接.但是它通过JTA挂接到事务管理器中,特别是在分布式事务提交之前刷新所有未决的更改.

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.

大多数时候,您可以使用特定于实现的API获得EntityManager所使用的基础连接(至少在Hibernate中是可能的).这意味着您可能根本不需要使用JTA和XA就可以满足您的要求:将EntityManager的基础连接用于JDBC.

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.

总结:

  1. 无需麻烦XAResource
  2. 切换到InnoDB
  3. 您可以尝试切换到XA数据源并获得与DataSource.getConnection()
  4. 的连接
  5. 您可以尝试切换到XA数据源并获取基础的EntityManager连接
  6. 您可以尝试使用非XA数据源并获得基础的EntityManager连接
  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.

这篇关于实现自定义JTA XAResource以与Hibernate一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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