Does SQLite support transactions across multiple databases?
Yes transactions works with different sqlite database and even between sqlite and sqlserver. I have tried it couple of times.
Some links and info
From here - Transaction between different data sources.
Since SQLite ADO.NET 2.0 Provider supports transaction enlistement, not only it is possible to perform a transaction spanning several SQLite datasources, but also spanning other database engines such as SQL Server.
Example:
using (DbConnection cn1 = new SQLiteConnection(" ... ") )using (DbConnection cn2 = new SQLiteConnection(" ... "))using (DbConnection cn3 = new System.Data.SqlClient.SqlConnection( " ... ") )using (TransactionScope ts = new TransactionScope() ){ cn1.Open(); cn2.Open(); cn3.Open(); DoWork1( cn1 ); DoWork2( cn2 ); DoWork3( cn3 ); ts.Complete();}
How to attach a new database:
SQLiteConnection cnn = new SQLiteConnection("Data Source=C:\\myfirstdatabase.db");cnn.Open();using (DbCommand cmd = cnn.CreateCommand()){ cmd.CommandText = "ATTACH DATABASE 'c:\\myseconddatabase.db' AS [second]"; cmd.ExecuteNonQuery();cmd.CommandText = "SELECT COUNT(*) FROM main.myfirsttable INNER JOIN second.mysecondtable ON main.myfirsttable.id = second.mysecondtable.myfirstid"; object o = cmd.ExecuteScalar();}
Yes, SQLite explicitly supports multi-database transactions (see https://www.sqlite.org/atomiccommit.html#_multi_file_commit for technical details). However, there is a fairly large caveat. If the database file is in WAL mode, then:
Transactions that involve changes against multiple ATTACHed databases are atomic for each individual database, but are not atomic across all databases as a set.