在类之间共享 LDAP 连接 - 最佳实践?
我需要做的是连接到 LDAP,然后将此连接传递给几个类,这些类执行各种处理步骤。
我面临的问题是我是否应该通过构造函数将连接传递给这些类,或者每个类是否应该管理自己的连接。
我看到第一种方法的问题是调用者可能不知道他负责自行关闭流。第二种方法似乎也不合适,因为打开/关闭/重新打开连接也没有意义。
对此有什么想法吗?
What I need to do is connect to an LDAP, and then pass this connection to several classes, which do various steps of processing.
The problem I face is if I should pass the connection to these classes via the constructor, or if every class should manage his own connection.
The problem I see with the first approach is that the caller may not know that he is responsible for closing the stream by itself. The second approach also doesn't seem appropriate because opening/closing/reopening the connection also makes no sense.
Any ideas on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道为什么会有几个处理 LDAP 的类。也许您应该考虑将这些分散的操作合并到一个类中,该类负责 LDAP 操作的所有操作。
如果那不可能,那么你的直觉是正确的。打开连接的类应该在finally 块中关闭它。那应该是基于接口的 POJO 服务类,它了解该用例的工作单元。责任在哪里应该是毫无疑问的。如果您没有这样的服务,请创建一个。
如果操作不是单个工作单元的一部分,那么它们应该由单独的服务管理。上一段的评论仍然适用。
您是否正在集中 LDAP 连接?我希望如此。
我建议您查看 Spring LDAP 模块,特别是如果您已经是 Spring 用户的话。它使处理 LDAP 资源变得容易,就像处理 JDBC 一样。
I don't know why you'd have several classes dealing with an LDAP. Maybe you should consider combining those scattered operations into a single class that has all responsibility for LDAP operations.
If that's not possible, your instincts are correct. The class that opens the connection should close it in a finally block. That should be the interface-based POJO service class that knows about the unit of work for that use case. There should be no doubt about where the responsibility lies. If you don't have such a service, create one.
If the operations aren't part of a single unit of work, then they should be managed by separate services. The comments from the previous paragraph still apply.
Are you pooling your LDAP connections? I hope so.
I'd recommend looking at the Spring LDAP module, especially if you're already a Spring user. It makes dealing with LDAP resources easy, the same way it does JDBC.
构建实用程序类或任何其他提供各种服务的类是一种不好的做法。类应该提供单个服务或一组严格控制的服务,否则您可能会回到 FORTRAN 垃圾公共块。要在类之间共享 LDAP 连接,请封装该连接(这也可以达到隐藏 API 详细信息的目的)。然后根据需要通过使用目录服务器上的帐户进行身份验证来保护方法。例如,应该需要
close()
方法来对具有关闭权限的帐户进行身份验证,或者是关闭组的成员,或者您喜欢的任何 authn/authz。您应该使用 UnboundID LDAP SDK 来完成此类工作。另请参阅“LDAP:编程实践”。It is a poor practice to construct a utility class, or any other class that provides a wide variety of services. Classes should provide a single service or group of tightly controlled services, else you may as well go back to FORTRAN garbage common blocks. To share an LDAP connection between classes, encapsulate the connection (this will also serve the purpose of hiding the API details). Then protect the methods as necessary by authenticating using accounts on the directory server. For example, a
close()
method should be required to authenticate to an account that has close privileges, or is the member of a close group, or whatever authn/authz you prefer. You should use the UnboundID LDAP SDK for this type of work. See also "LDAP: Programming Practices".