简体   繁体   English

Datomic 的长寿命数据库连接是如何为 sql Connections 实现的?

[英]How are Datomic's long lived database Connections implemented for sql Connections?

According to Datomic's Connection documentation :根据Datomic 的连接文档

Datomic connections do not adhere to an acquire/use/release pattern.数据连接不遵循获取/使用/释放模式。 They are thread-safe, cached, and long lived.它们是线程安全的、缓存的并且寿命很长。 Many processes (eg application servers) will never call release.许多进程(例如应用程序服务器)永远不会调用 release。

I'm interested to know how this is achieved in practice, specifically for sql connections.我很想知道这是如何在实践中实现的,特别是对于 sql 连接。 From the client/user perspective this is great as you don't need to worry about the thread pool at all which simplifies client code, and what you need to reason about signficantly.从客户端/用户的角度来看,这很棒,因为您根本不需要担心简化客户端代码的线程池,以及您需要推理的内容。 It's something I'd love to replicate in other applications with SQL connections.这是我喜欢在其他具有 SQL 连接的应用程序中复制的东西。

Breaking down the question into smaller parts:把问题分解成更小的部分:

  • What challenges needed to be considered when treating Datomic connections as long lived?将 Datomic 连接视为长期存在时需要考虑哪些挑战?
  • Is the approach suitable in general when dealing with JDBC connections, or is it only suitable for a sub class of problems (including Datomic's)?在处理 JDBC 连接时,该方法是否适用于一般情况,还是仅适用于问题的子类(包括 Datomic 的)?
  • I can see that Tomcat's JDBC connection pool is used under the hood, how is this pooling used to achieve long lived connections from the Datomic connection perspective?我可以看到 Tomcat 的 JDBC 连接池是在幕后使用的,从 Datomic 连接的角度来看,这个池是如何用来实现长连接的?
  • In practice when do you use separate JDBC connections behind the scenes eg do you use separate connections for reads vs writes?在实践中,您何时在幕后使用单独的 JDBC 连接,例如,您是否使用单独的连接进行读取和写入?

"It depends" :) “这取决于” :)

For example, if you have memcached set up, the Datomic peer potentially doesn't have to talk to the sql database at all.例如,如果您设置了 memcached,则 Datomic 对等方可能根本不必与 sql 数据库对话。 Datomic uses sql to fetch chunks of encoded data, not to do structured queries, so if a block is present in memcached, no sql is needed. Datomic 使用 sql 来获取编码数据块,而不是进行结构化查询,因此如果 memcached 中存在块,则不需要 sql。 Also, the peer will get new chunks sent to it by the transactor so if you're particularly lucky, everything is already available in your peer before you run your first query.此外,对等点将获得交易者发送给它的新块,因此如果您特别幸运,在您运行第一个查询之前,您的对等点中的所有内容都已经可用。

If a chunk is not already in the peer, and is not already in memcached, the peer needs to connect to the sql database fetch chunks.如果一个chunk不在peer中,也没有在memcached中,peer需要连接sql数据库获取chunks。 But this all happens under the hood, and is managed by the tomcat connection pool as you mentioned.但这一切都发生在幕后,并由您提到的 tomcat 连接池管理。 Generally, the idea is that for a query to run successfully, the index has to pull any missing chunks from storage (memcached, sql, ...), and this happens in a lazy fashion.一般来说,这个想法是为了使查询成功运行,索引必须从存储(memcached、sql、...)中提取任何丢失的块,这以一种懒惰的方式发生。 But the datomic connection itself "lives forever", ie this is all managed for you, and you don't have to create N connections depending on the amount of traffic your peer has etc etc.但是数据连接本身“永远存在”,即这一切都由您管理,您不必根据对等方的流量等创建 N 个连接。

As for writes, those go through the transactor and does not connect directly to storage.至于写入,那些通过事务器并且不直接连接到存储。 Writes are represented as the EDN datastructure we're all familiar with (the list of lists with db/add etc etc), and is shipped of to the transactor to a queue and processed in sequence.写入被表示为我们都熟悉的 EDN 数据结构(带有 db/add 等的列表列表),并被运送到事务处理者的队列中并按顺序处理。 The transactor then connects directly to storage when it needs to, but that's obviously a separate concern that does not affect the peer in any way.然后,事务处理程序在需要时直接连接到存储,但这显然是一个单独的问题,不会以任何方式影响对等方。

I hope this was clarifying :)我希望这是澄清:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM