实现自定义 JTA XAResource 以与 hibernate 一起使用
我对数据库有两级访问:第一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
然后你必须将存储引擎更改为 InnoDB,MyISAM 表不支持事务(从技术上讲,你不会收到错误,但回滚不会回滚任何内容)。
我不确定你在说什么。我在这里看到的唯一 XA 资源是您的数据库,您不需要实现任何自定义内容。您需要做的是使用很可能从两个 XA 数据源(受 MySQL Connector/J 5.0.0+ 支持)获得的 XA 连接,使用 JTA API 并让事务管理器完成其工作。
但说实话,你应该明确自己的要求。可能还有比使用 XA 更简单的其他选择。如果以上所有内容听起来都像中文,那么我感觉不要使用 XA 是一个很好的建议。
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).
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.
连接是通过可以配置为支持或不支持分布式事务的数据源获得的。要在分布式事务中使用多个连接,必须配置多个 DataSource 以支持 XA 并返回 XA 连接。
也就是说,仅当您连接到不同数据库时才需要多个物理连接,这似乎不符合您的情况(问题中不清楚)。
DataSource 可以足够智能,确保只要处于同一个线程中,就使用相同的物理连接;每次请求连接时,它实际上都会返回同一个物理连接的“句柄”,当所有句柄都已关闭时,该物理连接就会返回到池中。 (但这取决于数据源的实现)。
Hibernate 本身不是 XA 资源:它使用通过 DataSource 获得的底层连接。但它通过 JTA 将自身挂接到事务管理器中,特别是在分布式事务提交之前刷新所有挂起的更改。
大多数时候,您可以使用特定于实现的 API 来获取 EntityManager 使用的底层连接(至少对于 Hibernate 来说是可能的)。这意味着您可以完全不使用 JTA 和 XA 来满足您的要求:使用
EntityManager
的底层连接来处理您的 JDBC 内容。总结:
DataSource.getConnection()
获取连接希望我正确理解您的问题并且它有帮助。
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 theEntityManager
for your JDBC stuffs.In summary:
DataSource.getConnection()
EntityManager
connectionEntityManager
connectionHope I understood your question right and that it helps.