使用细粒度锁的代码示例(JCR Jackrabbit?) [英] Code examples that use fine grained locks (JCR Jackrabbit?)

查看:222
本文介绍了使用细粒度锁的代码示例(JCR Jackrabbit?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行一项学术研究,尝试开发一个编程工具,帮助实现细粒度的锁定函数,用于保持树状数据结构的并发程序。

I'm doing an academic research in trying to develop a programming tool that assists in implementing fine-grained locking functions, for concurrent programs that maintain tree-like data structures.

例如,程序员可以编写接收树根节点并修改树(通过遍历某些路径并添加/删除节点)的一些函数,该工具将帮助他找到代码节点中的哪里应该被锁定和它们可以被释放 - 所以这些函数可以在同一棵树上同时执行。

For example, the programmer may write some functions that receive a tree root-node and modify the tree (by traversing on some routes and adding/removing nodes), and the tool will help him to find where in the code nodes should be locked and where they can be released - so the functions could be executed concurrently on the same tree.

我在寻找一些现实的代码示例,使用了粒度锁定,或者它可以用于更好的性能,但是程序员太懒惰地实现它(例如,他在函数调用期间锁定整个树,而不释放无用的节点)。

I am looking for some real-life code examples in which such fine-grained locking is used, or where it could be used for better performance but the programmer was too lazy to implement it (for example, he locked the whole tree during the function-call without releasing useless nodes).

我读了关于JCR& Jackrabbit使用树形数据库,并发现了一篇文章,解释如何在JCR中锁定节点(但没有示例):
http://www.day.com/specs/jcr/2.0/17_Locking.html

I read about JCR & Jackrabbit, which use a tree-shaped database, and found an article that explains how to lock nodes in JCR (but without examples): http://www.day.com/specs/jcr/2.0/17_Locking.html

我在数据库中有一个非常小的背景,我不完全明白什么是允许的,什么是不允许的,当涉及到Jackrabbit数据库和并发。不允许从2个线程访问同一个节点,但不同的存储库呢?如果两个不同的客户端尝试访问同一个节点(例如,一个尝试删除它,另一个尝试修改它 - 会失败?)会发生什么。

I have a really small background in databases, and I don't fully understand what is allowed and what is not allowed when it comes to Jackrabbit databases and concurrency. Accessing the same node from 2 threads is not allowed, but what about different repositories? And what happens if 2 different clients try to access the same node (for example, one tries to delete it, and another one tries to modify it - will the session.save() fail?).

感谢,
Oren

Thanks, Oren

推荐答案

首先,不要混淆数据库/ jackrabbit / locking。

First of all, don't get confused between databases/jackrabbit/locking. Jackrabbit implements its own locking, as do databases.

Jackrabbit允许您使用 LockManager.lock()锁定节点 。将 isDeep 参数设置为true意味着下面的所有节点也将被锁定。

Jackrabbit allows you to lock nodes using LockManager.lock(). Setting the isDeep parameter to true means all the nodes underneath will also be locked. A locked node can be read by another session but can't be modified.

技术上来说,如果两个线程使用相同的会话,则可以编辑同一个节点,但这个节点

Technically speaking, 2 threads COULD edit the same node if they are using the same session but this is rather hazardous and should probably be avoided.

如果一个节点很可能被两个并发会话修改,那么你应该总是锁定该节点。哪个会话最后有应该等待锁被释放。如果你不锁定,至少有一个会话会抛出异常。

If a node is likely to be modified by 2 concurrent sessions then you should always lock the node. Which ever session gets there last should wait for the lock to be released. If you don't lock then at least one of the sessions will throw an exception.

我不知道你的意思是从不同的仓库访问节点。节点只能属于一个存储库。如果你的意思是有两个jackrabbit实例访问同一个数据库,那么这也应该避免,或者你应该考虑使用集群。

I'm not sure what you mean by accessing nodes from different repositories. A node can only belong to one repository. If you mean having 2 jackrabbit instances accessing the same database then this too should be avoided or you should look to using clustering.

当实现锁定它将取决于你的设计和要求。没有点锁定,如果你只有一个会话,反之亦然。是否锁定节点或子树将取决于数据表示的内容。例如,如果一个节点代表一个文件夹,你可能想锁定节点而不是整个子树。如果一个子树表示一个复杂的文档,那么你可能想要锁定子树。

When implementing locking it is going to depend on your design and requirements. There's no point locking if you'll only ever have one session and vice-versa. Whether you lock a node or sub-tree is going to depend on what your data represents. For example, if a node represents a folder you'll probably want to lock just the node and not the whole sub-tree. If a sub-tree represents a complex document then you probably will want to lock the sub-tree.

至于锁定整个树,我希望我不满足有人这样做!

As for locking the whole tree, I hope I don't meet someone who does that!

这篇关于使用细粒度锁的代码示例(JCR Jackrabbit?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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