SQLite 连接列表
我正在使用 JDBC 驱动程序与 SQlite 连接。我想从 SQLite 命令提示符检查数据库引擎中打开了多少个连接。谁能告诉我该怎么做?
I am connecting with SQlite using JDBC driver. I want to check from SQLite command prompt how many connections are open within the database engine. Can any one tell me how I can do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SQLite 是一个 嵌入式数据库 - 每个访问数据库文件的进程本质上都有自己的数据库引擎。单独引擎之间的唯一交互是通过底层操作系统的文件锁定设施,这确保各个进程在尝试更新数据库文件时不会互相干扰。
由于这种设计,就 SQLite 而言,不存在单一控制点。 sqlite3 shell 实用程序本质上包含 SQLite 数据库引擎本身的副本,这就是它处理 DB 文件的方式 - 它不会“连接”到另一个应用程序。事实上,SQLite 文档中的术语“连接”只是由于数据库工程师对它的熟悉而被(错误地)使用 - 实际上它只不过是打开一两个文件。
因此,除了在另一个进程修改数据库时偶尔遇到锁定文件之外,
sqlite3
实用程序无法知道任何其他应用程序正在做什么。如果您想知道您的应用程序对底层数据库做了什么,您将必须找到/编写一个围绕基本 SQLite3 库的包装器,该包装器将执行您需要的任何其他簿记工作 - 可能提供一个控制接口。基本 SQLite 库本身没有任何此类设施,因为它们被认为超出了其范围。
包装器方法的问题是您的应用程序仍然不知道任何其他进程正在做什么。您也许可以让所有使用包装器的进程在中央访问点(文件/网络服务/...)收集数据,但仅此而已 - 任何其他进程(例如
sqlite3
shell)仍然会直接访问数据库文件。但是,如果您只需要知道数据库文件的“连接”数量,则可能有一种(绝对不可移植)方法:每个“连接”都会导致数据库文件被打开,因此您可以使用特定于平台的实用程序,用于找出每个时刻哪些进程打开了该文件。例如,在 Linux 上,您可以使用
lsof
实用程序。顺便说一句,如果您从应用程序的单个实例多次打开同一个数据库文件,您可能需要重新考虑您的架构 - 与传统的数据库服务器相反,打开到单个 SQLite 数据库文件的多个“连接”没有性能如果您不小心,甚至可能会导致您的应用程序陷入僵局。
SQLite is an embedded database - each process that accesses a database file essentially has its own DB engine. The only interaction between the separate engines is through the file locking facilities of the underlying operating system, which make sure that the various processes won't step on each other when trying to update the DB file.
Due to this design, there is no single control point as far SQLite is concerned. The
sqlite3
shell utility essentially contains a copy of the SQLite database engine itself and that's how it can handle a DB file - it does not "connect" to another application. In fact, the term "connection" in the SQLite documentation is only (mis)used due to its familiarity to DB engineers - in reality it's nothing more than opening a file or two.Therefore, the
sqlite3
utility has no way to know what any other application is doing, apart from occasionally encountering a locked file when another process is modifying the DB.If you want to know what your application is doing with the underlying DB, you will have to find/write a wrapper around the base SQLite3 library that will perform any additional book-keeping that you need - possibly providing a control interface. The base SQLite library itself does not have any such facilities since they are considered to be outside of its scope.
The problem with the wrapper approach is that your application would still not know what any other process is doing. You might be able to have all processes that use the wrapper collect their data at a central access point (file/network service/...), but that's about it - any other process (e.g. the
sqlite3
shell) would still access the DB file directly.If you only need to know the number of "connections" to a DB file, however, there might be a (definitely non-portable) way: each "connection" results in the DB file being opened, therefore you might be able to use a platform-specific utility to find out which processes have opened the file at each moment. On Linux, for example, you could use the
lsof
utility.By the way, if you are opening the same DB file multiple times from a single instance of your application, you might want to rethink your architecture - contrary to traditional DB servers, opening multiple "connections" to a single SQLite DB file has no performance advantage and might even cause your application to deadlock if you are not careful.
java.sql
包中没有 API 可以执行此操作。您必须在您身边执行此操作,例如使用Singleton
类,该类允许您创建连接并跟踪此类创建的连接的数量。PS:当然这只会统计底层运行的JVM实例的连接数
There is no API to do that in the
java.sql
package. You have to do that at your side for example using aSingleton
class alowing you to create connections and keeping track of the number of such created connections.PS: of course this will only count the number of connections in underlying running JVM instance