如何通过现有事务征募XAResource? [英] How to enlist XAResource with existing Transaction?

查看:64
本文介绍了如何通过现有事务征募XAResource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用例是:

我有一个现有的JTA TransactionManager和一个进行中的Transaction.我想在此事务中将Neo4j列为XAResource,以便它可以在适当的2PC中准备/提交.

I have an existing JTA TransactionManager and a Transaction in-flight. I'd like to enlist Neo4j as an XAResource in this Transaction such that it may prepare/commit in proper 2PC.

我没有在Neo4j中看到公开的XAResource实现;一切似乎都通过NioNeoDbPersistenceSource> NeoStoreXaDataSource> NeoStoreXaConnection.NeoStoreXaResource进行路由.

I'm not seeing a public XAResource implementation in Neo4j; everything seems to be routed through the NioNeoDbPersistenceSource > NeoStoreXaDataSource > NeoStoreXaConnection.NeoStoreXaResource.

是否有一种首选方法可以使Neo4j参与其自己的TransactionManager提供的事务之外的JTA事务?我正在寻找的所有测试用例都参加了模拟"FakeXAResource" [1]

Is there a preferred way to enlist Neo4j in JTA Transactions outside those provided by its own TransactionManager? All the test cases I'm finding enlist mock "FakeXAResource"[1]

感激!

S, ALR

[1]例如使用JOTMAsTxManagerIT

[1] e.g. UseJOTMAsTxManagerIT

推荐答案

好的,我有Neo4j可以解决的最佳解决方案,尽管我对此并不感到兴奋. :)

OK, I have a solution which I believe is the best that Neo4j can handle, though I'm not thrilled with it. :)

https://gist.github.com/ALRubinger/b584065d0e7da251469a

想法是这样的:

1)实现Neo4j的AbstractTransactionManager

1) Implement Neo4j's AbstractTransactionManager

这个笨拙的类是JTA TransactionManager,Neo4j Lifecycle和其他一些方法的组合.对于我来说,尚不清楚其中的某些内容(例如"getEventIdentifier()"或"doRecovery()")应该如何处理,并且合同感觉过于具体.对于不确定Neo4j不是TransactionManager的权威所有者的情况,我不确定为什么要在这里使用生命周期方法.

This clunky class is a composite of JTA TransactionManager, Neo4j Lifecycle, and some other methods; it's not completely clear to me what some of these (e.g. "getEventIdentifier()" or "doRecovery()") are supposed to, and the contract feels over-specified. I'm not certain why we'd want lifecycle methods in here for the case where Neo4j is not the authoritative owner of the TransactionManager.

2)实现Neo4j的TransactionManagerProvider

2) Implement Neo4j's TransactionManagerProvider

这将使您可以创建AbstractTransactionManager实现的新实例,但受JDK Service SPI的约束,因此您必须提供no-arg构造函数,并找到其他一些将上下文信息传入的智能/hacky方法.

This will let you create a new instance of your AbstractTransactionManager implementation, but it's bound by the JDK Service SPI, so you must supply a no-arg constructor and find some other intelligent/hacky way of passing contextual information in.

3)创建META-INF/services/org.neo4j.kernel.impl.transaction.TransactionManagerProvider文件,其中包含第2步中的TransactionManagerProvider的FQN内容

3) Create a META-INF/services/org.neo4j.kernel.impl.transaction.TransactionManagerProvider file, with contents of the FQN of your TransactionManagerProvider impl from step 2)

4)创建新的GraphDatabaseService时,请像以下那样传递配置:

4) When you create a new GraphDatabaseService, pass in config like:

final GraphDatabaseService graphDatabaseService = new GraphDatabaseFactory().
            newEmbeddedDatabaseBuilder(FILE_NAME_STORAGE).setConfig(
            GraphDatabaseSettings.tx_manager_impl.name(),
            "NAME_OF_YOUR_TXM_PROVIDER")
            .newGraphDatabase();

然后使用不赞成使用的API(GraphDatabaseAPI)访问TransactionManager:

Then you access the TransactionManager using a deprecated API (GraphDatabaseAPI):

// Get at Neo4j's view of the TransactionManager
final TransactionManager tm = ((GraphDatabaseAPI)   graphDatabaseService).getDependencyResolver().resolveDependency(TransactionManager.class);
tm.begin();
final Transaction tx = tm.getTransaction();

这种方法的真正问题在于,我们必须使用Neo4j的TransactionManager实现,该实现包装了我们的真实TM.我想做的是使用我的TM并注册Neo4j作为XAResource.

The real problem I have with this approach is that we have to use the TransactionManager implementation from Neo4j, which is wrapping our real TM. What I want to do is use my TM and enlist Neo4j as an XAResource.

所以我仍然没有找到一种方法来执行此操作,从Neo4j测试套件来看,我认为目前它们提供的任何XAResource支持都是不可能的.

So I still haven't found a way to do that, and judging from the Neo4j test suites I don't think it's possible at the moment with any of their supplied XAResource support.

绝对愿意并希望得到纠正! :)

Absolutely willing and hoping to be corrected! :)

但是没有达到我上面提到的要点,附件的要点起作用并显示Neo4j使用外部TransactionManager(Narayana,来自JBoss,来自我们)作为支持实现.

But failing the points I mention above, the attached gist works and shows Neo4j using an external TransactionManager (Narayana, from us at JBoss) as the backing implementation.

S, ALR

这篇关于如何通过现有事务征募XAResource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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