Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file:memory does not appear to work #92

Closed
2 tasks done
dkotik opened this issue Apr 12, 2024 · 4 comments
Closed
2 tasks done

file:memory does not appear to work #92

dkotik opened this issue Apr 12, 2024 · 4 comments

Comments

@dkotik
Copy link
Contributor

dkotik commented Apr 12, 2024

Dear maintainer,

I use the sqlitex.Pool for simulating in-memory testing connections, but it does not appear to work when using file:memory?mode=memory with multiple connections as described below:

func NewPool(uri string, opts PoolOptions) (pool *Pool, err error) {
	if uri == ":memory:" {
		return nil, strerror{msg: `sqlite: ":memory:" does not work with multiple connections, use "file::memory:?mode=memory"`}
	}
       // ...
}
  • Created tables are unique per connection instead of being shared in the Pool. Using PrepareConn option just creates individual tables in fragmented memory space - the data is not shared between connections.
  • There is a small typo there with double :: between file and memory.

I care about this, because my authorization code occasionally may make a database call, which will block the Pool, making this impossible to test. I can work around this by writing my own wrapper around the Pool and returning the same connection when in `file:memory:" mode, but I want to know if I am doing something wrong here, or may be there is a better way. Thank you!

@zombiezen
Copy link
Owner

zombiezen commented Apr 12, 2024

You will also need to pass in OpenSharedCache (not on by default) in order for a pool of in-memory connections to work as you expect.

edit: I believe adding &cache=shared will also work. If that's the case, the error message should probably be amended to include that to avoid future confusion.

@dkotik
Copy link
Contributor Author

dkotik commented Apr 12, 2024

edit: I believe adding &cache=shared will also work. If that's the case, the error message should probably be amended to include that to avoid future confusion.

This worked perfectly! You are amazing!

@dkotik
Copy link
Contributor Author

dkotik commented Apr 12, 2024

I will gladly submit a documentation PR - would you like me to? I write ok docs.

@zombiezen
Copy link
Owner

Sure, sounds good. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants