开发者

Sqlite Connection list

I am connecting with SQlite using JDBC driver. I want to check from SQLite command prompt how many connectio开发者_Python百科ns are open within the database engine. Can any one tell me how I can do it?


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.


There is no API to do that in the java.sql package. You have to do that at your side for example using a Singleton 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜