简体   繁体   English

如何在C#或VB.NET中使用SqLite连接池?

[英]How to use SqLite connection pool in C# or VB.NET?

The connection string for SqLite can include options for using a connection pool, eg (pseudo code) SqLite的连接字符串可以包含使用连接池的选项,例如(伪代码)

Dim connectionString = "Data Source=" + Path + ";" +
                                    "Version=3;" +
                                    "Pooling=True;" +
                                    "Max Pool Size=100;"

I would expect that there is some extra class or factory method for using connection pools, eg 我希望有一些额外的类或工厂方法来使用连接池,例如

dim connectionPool = new SqLiteConnectionPool(connectionString)

dim firstConnection = connectionPool.getConnection()
...
firstConnection.dispose()

...
dim secondConnection = connectionPool.getConnection()

However, I could not find such a class. 但是,我找不到这样的课程。

=> How do I give back a connection to the connection pool ? => 如何返回连接池的连接

=> How can I reuse a connection that has previously been returned to the pool ? => 如何重用先前已返回池的连接

Searching for "pool" on https://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki did not give any results. https://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki上搜索“池”没有给出任何结果。

a) Do I have to call the constructor for the connection several times? a)我是否必须多次调用连接的构造函数?

dim firstConnection = new SqLiteConnection(connectionString)
...
firstConnection.dispose()


dim secondConnection = new SqLiteConnection(connectionString)

Passing the connection string several times does not seem to be intuitive for me. 多次传递连接字符串对我来说似乎并不直观。

b) Or would I create the connection only once and somehow wake it up after it has been closed/disposed? b)或者我只创建一次连接并以某种方式在关闭/处置后将其唤醒?

dim connection = new SqLiteConnection(connectionString))
using connection 
 ...
end using 

connection.open()
using connection
 ...
end using 

The SQLite driver will manage pools for you, however, there is one key point many don't realize when using connections. SQLite驱动程序将为您管理池,但是,在使用连接时,有许多关键点没有实现。

OPEN LATE, CLOSE EARY OPEN LATE,关闭EARY

A connection can't be returned to the pool if it's open. 如果连接已打开,则无法将该连接返回到池中。 So, you can create the connection, prepare the statements, however, only open the connection immediately before executing the query. 因此,您可以创建连接,准备语句,但是,只在执行查询之前立即打开连接。

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

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