using System.Data.SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AIK.DataAccess
{
public class SqlitePooledConnection : IDisposable
{
// 内部持有真实的 SQLite 连接
private readonly SQLiteConnection _innerConnection;
// 标记是否已释放
private bool _disposed = false;
///
/// 暴露内部连接供 Dapper 调用(核心)
///
public SQLiteConnection InnerConnection => _innerConnection;
///
/// 构造函数(仅由 SQLiteCore 内部创建,外部不直接实例化)
///
internal SqlitePooledConnection(SQLiteConnection innerConnection)
{
_innerConnection = innerConnection ?? throw new ArgumentNullException(nameof(innerConnection));
}
///
/// using 结束时自动执行,归还连接到池
///
public void Dispose()
{
if (!_disposed)
{
// 关键:归还连接到连接池,而非销毁
SQLiteCore.ReturnConnection(_innerConnection);
_disposed = true;
}
}
// 可选:暴露常用方法,简化调用(如创建命令)
public SQLiteCommand CreateCommand()
{
if (_disposed) throw new ObjectDisposedException(nameof(SqlitePooledConnection));
return _innerConnection.CreateCommand();
}
}
}